C语言从键盘输入任意的字符,按下列规则进行分类: (1)数字(例如:‘0’‘5’);(2)字母(例如:‘a’‘b’);(3)算术运算符(+、-、*、/、%);(4)转义字符(\n、\a、\b、\t)。但输入一个字符‘#’时,停止统计,并打印统计的结果。
时间: 2023-07-10 20:19:27 浏览: 171
好的,以下是一个简单的 C 代码实现:
```
#include <stdio.h>
#include <ctype.h>
int main() {
char c;
int count_digit = 0, count_letter = 0, count_operator = 0, count_escape = 0;
while ((c = getchar()) != '#') {
if (isdigit(c)) {
count_digit++;
} else if (isalpha(c)) {
count_letter++;
} else if (c == '+' || c == '-' || c == '*' || c == '/' || c == '%') {
count_operator++;
} else if (c == '\n' || c == '\a' || c == '\b' || c == '\t') {
count_escape++;
}
}
printf("数字:%d\n字母:%d\n算术运算符:%d\n转义字符:%d\n", count_digit, count_letter, count_operator, count_escape);
return 0;
}
```
这个程序会逐个读取输入的字符,并根据字符的类型进行分类统计。当输入字符为 '#' 时,停止统计,并输出统计结果。其中,`isdigit()` 函数和 `isalpha()` 函数用于判断字符是否为数字和字母;算术运算符和转义字符则按照题目要求判断即可。
阅读全文
相关推荐















