|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
今天做c语言习题是做到这个,要求输出输入字符中各类符号的个数(函数章节)
我自己打的运行时总是无法转入循环里的else部分,为什么?
#include "stdafx.h"
#include "conio.h"
int main()
{
int num[10] = { 0 };
int blank = 0, other = 0, a, b, c;
char d = '0';
while (1)
{
d = _getch();
printf("%c", d);
a = int(d);
if (a == 27)
{
printf("\n");
break;
}
else if (48 <= a <= 57)
{
switch (a)
{
case 48:num[0] = num[0] + 1;
case 49:num[1] = num[1] + 1;
case 50:num[2] = num[2] + 1;
case 51:num[3] = num[3] + 1;
case 52:num[4] = num[4] + 1;
case 53:num[5] = num[5] + 1;
case 54:num[6] = num[6] + 1;
case 55:num[7] = num[7] + 1;
case 56:num[8] = num[8] + 1;
case 57:num[9] = num[9] + 1;
}
}
else if (d == ' '||d=='\n')
{
blank = blank + 1;
}
else
{
other = other + 1;
}
}
for (c = b = 0; b < 10; b++, c++)
{
printf("数字%d出现了%d次\n", c, num[b]);
}
printf("空格有%d个\n", blank);
printf("其他有%d个\n", other);
return 0;
}
根据你的代码改的
- #include "stdio.h"
- #include "conio.h"
- int main()
- {
- int num[10] = { 0 };
- int blank = 0, other = 0, i;
- char d = '0';
- while (1)
- {
- d = getchar();
- printf("%c", d);
- //a = int(d);
- if (d == '\n')
- {
- printf("\n");
- break;
- }
- else if (d <= '9' && d >= '0')
- {
- switch (d)
- {
- case '0':num[0] = num[0] + 1;break;
- case '1':num[1] = num[1] + 1;break;
- case '2':num[2] = num[2] + 1;break;
- case '3':num[3] = num[3] + 1;break;
- case '4':num[4] = num[4] + 1;break;
- case '5':num[5] = num[5] + 1;break;
- case '6':num[6] = num[6] + 1;break;
- case '7':num[7] = num[7] + 1;break;
- case '8':num[8] = num[8] + 1;break;
- case '9':num[9] = num[9] + 1;break;
- }
- }
- else if (d == 32 ||d == 9 || d == '\n')
- {
- blank = blank + 1;
- }
- else
- {
- other = other + 1;
- }
- }
- for (i = 0; i < 10; i++)
- {
- printf("数字%d出现了%d次\n", i, num[i]);
- }
- printf("空格有%d个\n", blank);
- printf("其他有%d个\n", other);
-
- return 0;
- }
复制代码
|
|