#define M 100
#define N 10
#include <stdio.h>
main()
{ char a[M];
int b[N]={0,0,0,0,0,0,0,0,0,0};
int i=0;
while(getchar(a[i])!='\0')
{if(a[i]=='0')b[0]++;
else if(a[i]=='1')b[1]++;
else if(a[i]=='2')b[2]++;
else if(a[i]=='3')b[3]++;
else if(a[i]=='4')b[4]++;
else if(a[i]=='5')b[5]++;
else if(a[i]=='6')b[6]++;
else if(a[i]=='7')b[7]++;
else if(a[i]=='8')b[8]++;
else if(a[i]=='9')b[9]++;
i++;
}
for(i=0;i<N;i++)printf("%d\n",b[i]);
}
各位高手帮下忙,谢谢了!
----------------解决方案--------------------------------------------------------
while((a[i]=getchar())!='\0') /*这里错了,应为a[i]=getchar(),而你写的是getchar(a[i])*/
[此贴子已经被作者于2007-1-14 20:52:40编辑过]
----------------解决方案--------------------------------------------------------
#define M 100
#define N 10
#include <stdio.h>
int main(void)
{
char a[M];
int b[N]={0,0,0,0,0,0,0,0,0,0};
int i=0;
while((a[i]=getchar())!='\0')
{
switch(a[i])
{
case '0':b[0]++;break;
case '1':b[1]++;break;
case '2':b[2]++;break;
case '3':b[3]++;break;
case '4':b[4]++;break;
case '5':b[5]++;break;
case '6':b[6]++;break;
case '7':b[7]++;break;
case '8':b[8]++;break;
case '9':b[9]++;break;
}
i++;
}
for(i=0;i<N;i++)printf("%d\n",b[i]);
}
用switch比较好看.....if太麻烦了
[此贴子已经被作者于2007-1-14 20:43:05编辑过]
----------------解决方案--------------------------------------------------------
while((a[i]=getchar())!='\n') 这样比较好,getchar()针对字符,不是字符串,所以用'\n'
----------------解决方案--------------------------------------------------------
楼上2位都错了,while((a[i]=getchar())!='\0')注意括号,你们可以试试,如果用你们的方法会出现结果都为0.
因为先算右边,再算左边
----------------解决方案--------------------------------------------------------
谢谢啊!
结果好象不对啊???
----------------解决方案--------------------------------------------------------
哪里错了,把出现的问题发上来
----------------解决方案--------------------------------------------------------
结果全都是0啊!
----------------解决方案--------------------------------------------------------
#define M 100
#define N 10
#include <stdio.h>
int main(void)
{
int b[N]={0},j;
char i=0;
while((i=getchar())!='\n')
{
switch(i)
{
case '0':b[0]++;break;
case '1':b[1]++;break;
case '2':b[2]++;break;
case '3':b[3]++;break;
case '4':b[4]++;break;
case '5':b[5]++;break;
case '6':b[6]++;break;
case '7':b[7]++;break;
case '8':b[8]++;break;
case '9':b[9]++;break;
default :i='\n';break;
}
}
for(j=0;j<=9;j++)
printf("character:%d display:%d times\n",j,b[j]);
return 0;
}
LZ是否是这个意思????
----------------解决方案--------------------------------------------------------
晕!
感觉都没错~
怎么运行出来就错了~
怎么回事啊~
高手指点一下
----------------解决方案--------------------------------------------------------