那个 初学…………c
就是输入字符统计那个 谁帮帮我啊……//
#include<stdio.h>
main()
{
char c;
int ncount,ccount,scount,ocount;
ncount = ccount = scount = ocount = 0;
for(;(c = getchar()) <= EOF;)
{
if ((c>='A'&& c<='Z')||(c>='a' && c<='z'))
ccount++;
else if((c>=0 && c<=9))
ncount++;
else if(c == ' ')
scount++;
else
ocount++;
}
printf("the digit of character is:%d\n",ccount);
printf("the digit of number is:%d\n",ncount);
printf("the digit of space is:%d\n",scount);
printf("the digit of other element is:%d\n",ocount);
}
[解决办法]
#include<stdio.h>main(){ char c; int ncount,ccount,scount,ocount; ncount = ccount = scount = ocount = 0; while((c = getchar()) !=EOF) { fflush(stdin); if ((c>='A'&& c<='Z')||(c>='a' && c<='z')) ccount++; else if((c>=0 && c<=9)) ncount++; else if(c == ' ') scount++; else ocount++; } printf("the digit of character is:%d\n",ccount); printf("the digit of number is:%d\n",ncount); printf("the digit of space is:%d\n",scount); printf("the digit of other element is:%d\n",ocount);}
[解决办法]
for(;(c = getchar()) <= EOF;)
这句是有问题的。应该改成:
for(;(c = getchar()) != EOF;) (或者while ((c = getchar() != EOF))输入EOF需要通过按键输入,在linux下是按:ctrl+d,在windows下是按:ctrl+z吧。
[解决办法]
while((c = getchar())!= EOF) //for(;(c = getchar()) <= EOF;)
{
if ((c>='A'&& c<='Z')||(c>='a' && c<='z'))
ccount++;
else if((c>='0' && c<='9'))
ncount++;
else if(c == ' ')
scount++;
else
ocount++;
}
[解决办法]
EOF的值通常是 -1。 ascii码常用的是0-127。 你这么整,他必须全是 0 啊 !!
[解决办法]
[code=C/C++][/code]
#include<stdio.h>
main()
{
char c;
int ncount,ccount,scount,ocount;
ncount = ccount = scount = ocount = 0;
for(;(c = getchar()) !='0' ;)//EOF=》'0'
{
if ((c>='A'&& c<='Z')||(c>='a' && c<='z'))
ccount++;
else if((c>='0' && c<='9'))//要用字符
ncount++;
else if(c == ' ')
scount++;
else
ocount++;
}
printf("the digit of character is:%d\n",ccount);
printf("the digit of number is:%d\n",ncount);
printf("the digit of space is:%d\n",scount);
printf("the digit of other element is:%d\n",ocount);
}