康明斯氮氧传感器:初学C语言

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/09 14:55:57
我自己写的代码如下(能运行):
#include <stdio.h>

int main()
{
int c, i, nwhite, nother, ndigit[10],chcter[26];

nwhite = nother = 0;
for (i=0;i<10;i++)
ndigit[i]=0;

for (i=0;i<26;i++)
chcter[i]=0;

while((c=getchar())!= '\n'){

switch (c){

//count digits
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
ndigit[c-'0']++;
printf("%c",c);
break;
//count lowcase char
case 'a': case 'b': case 'c': case 'd': case 'e':
case 'f': case 'g': case 'h': case 'i': case 'j':
case 'k': case 'l': case 'm': case 'n': case 'o':
case 'p': case 'q': case 'r': case 's': case 't': case 'u':
case 'v': case 'w': case 'x': case 'y': case 'z':
chcter[c-'a']++;

break;
//count upercase char

case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
case 'G': case 'H': case 'I': case 'J': case 'K':
case 'L': case 'M': case 'N': case 'O': case 'P':
case 'Q': case 'R': case 'S': case 'T': case 'U':
case 'V': case 'W': case 'X': case 'Y': case 'Z':
chcter[c-'A']++;
break;
// count white spaces
case ' ': case '\t':
nwhite++;
break;

//count other characters
default:
nother++;
break;

}//end switch

}// end while

printf("digits in input:\n");
printf("+-----------------------------------------------------+\n");
printf("| |\n");
//printf("%c",digit);
printf("|");
//printf("%d",ndigit[c-' ']);

for (i=0;i<10;i++){
if(ndigit[i]!=0){
//printf(" %d",digit);
printf(" %d",ndigit[i]);}
else
printf(" ");
}
printf(" |\n");

printf("+-----------------------------------------------------+\n");

printf("Char= \n");
//printf("+-------------------------------------------------------------------+\n");
//printf("| |\n");
//printf("|");
for (i=0;i<26;i++){
if(chcter[i]!=0)
printf(" %d", chcter[i]);
else
printf(" ");
}
//printf(" |\n");

//printf("+-------------------------------------------------------------------+\n");

printf("\nwhite spaces = %d, others = %d ", nwhite, nother);

return 0;
}

但我想生成如下的格式,请问各位高人该怎么办?谢谢:
Digits in input:
+------------+
| 0 1 9 |
| 18 115 5 |
+------------+

Alphabet characters in input:
+--------------------+
| a d f j s z |
| 62 3 2 1 1 11 |
+--------------------+
Space characters in input: 7
Other characters in input: 15

楼上的,nwhite=nother=0;这句并没问题。=是一个赋值运算,将返回=左边的值。也就是说nwhite=nother=0;执行起来相当于:
nother=0;
nwhite=nother;

楼主:不好意思,我没有耐心看你的程序,因为我觉得你的代码太长,而且有点无聊。你在练习switch?
你到底什么地方不懂,直截了当的问就好了,写一个那么长的程序(大部分代码都与问题无关),考验我们的耐心啊?sorry

你的程序能运行???
我有点怀疑。
nwhite = nother = 0; 在C中哪有这种赋值格式的,虽说很久没写C了。我是没有看到过。
看完这句我就不想往下看了,你都说你能运行,可我看到第五行就错了。