实验8-11:判断字符类型
【问题描述】
从键盘输入任意的字符,按下列规则进行分类:
(1)数字(例如:‘0’‘5’);(2)字母(例如:‘a’‘b’);(3)算术运算符(+、-、*、/、%);(4)转义字符(\n、\a、\b、\t)。但输入一个字符‘#’时,停止统计,并打印统计的结果。
【输入形式】
【输出形式】
【样例输入】
01a\b+#
【样例输出】
数字个数:2
字母个数:1
算术运算符:1
转义字符:1
#include<stdio.h>
#include<string.h>
int main(void)
{
int count_shuzi=0;
int count_zimu=0;
int count_yunsuan=0;
int count_zhuanyi=0;
char str[10];
gets(str);
for(int i=0;str[i]!='#';i++)
{
if(str[i]>='0' && str[i]<='9')
{
count_shuzi++;
}
else if(str[i]=='+' || str[i]=='-' || str[i]=='*' || str=='/ '|| str[i]=='%')
{
count_yunsuan++;
}
else if(str[i]>='a' && str[i]<='z')
{
count_zimu++;
}
else
{
count_zhuanyi++;
}
}
count_zimu = count_zimu - count_zhuanyi;
printf("数字个数:%d\n字母个数:%d\n算术运算符:%d\n转义字符:%d\n",count_shuzi,count_zimu,count_yunsuan,count_zhuanyi);
return 0;
}
下载源文件 得分10.00 最后一次提交时间:2023-05-02 14:29:30 成功编译,但有警告信息. Main.c: In function 'main': Main.c:10:5: warning: implicit declaration of function 'gets' [-Wimplicit-function-declaration] gets(str); ^ Main.c:17:67: warning: multi-character character constant [-Wmultichar] else if(str[i]=='+' || str[i]=='-' || str[i]=='*' || str=='/ '|| str[i]=='%') ^ Main.c:17:65: warning: comparison between pointer and integer [enabled by default] else if(str[i]=='+' || str[i]=='-' || str[i]=='*' || str=='/ '|| str[i]=='%') ^ /tmp/ccMyH5oV.o: In function `main': Main.c:(.text.startup+0xe): warning: the `gets' function is dangerous and should not be used. 共有测试数据:2 平均占用内存:1.211K 平均CPU时间:0.00292S 平均墙钟时间:0.00292S
|