这是为什么呢
一个很小的程序,就是统计空格,换行符,制表符的小程序,这是我写的代码
[code=C/C++][/code]
#include<stdio.h>
#include<stdlib.h>
int main()
{
int c,nb,nt,nl;
nb = 0;/*the number of blanks */
nt = 0;/*the number of tabs */
nl = 0;/*the number of newlines */
while((c = getchar()) != EOF){
if (c == ' ')
++nb;
if (c == '/t')
++nt;
if (c == '/n')
++nl;
}
printf("the blanks is %d\n",nb);
printf("the tabs is %d\n",nt);
printf("the newlines is %d\n",nl);
system("pause");
}
在输入测试数据之后,发现nt和nl的值还是初始值零,这是为什么呢
[解决办法]
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c,m,n,l;
m=n=l=0;
/* m代表空格个数,n代表制表符个数,l代表换行符个数 */
while((c = getchar()) != EOF)
{
if(c == '\n')
{
++l;
}
else if(c == ' ')
{
++m;
}
else if(c == '\t')
{
++n;
}
}
printf("%d\t%d\t%d",m,n,l);
system("pause");
return 0;
}
注意判断条件
[解决办法]
楼主把'\t'、'\n',写成'/t'、'/n'了,转义字符应该用反斜杠。。
[解决办法]
'\t','\n'
[解决办法]
'/t'、'/n'应该是'\t'和'\n'
[解决办法]
还有,由于你这些条件是排他的,应当用else if,能提高一些效率。
[解决办法]
另外,可以用更飘逸的计数方法,把字符当作数组索引,弄一个长度为256的数组来计数。
[解决办法]
[解决办法]
撇'/'和捺'\'不是一回事!(^_^)