编写一个完整的c语言源程序

2024-12-25 23:36:24
推荐回答(2个)
回答1:

#include
#include
#include
#define MAXTYPE 4
enum { LOW, UP, DIGIT, OTHER };
int count[MAXTYPE];
float percent[MAXTYPE];
int CharTypeCount(char *s)
{
unsigned char *p;
int i;
int n;
memset(count, '\0', sizeof(count));
p = (unsigned char*)s;
n = 0;
while (*p) {
if (*p>='a' && *p<= 'z') {
count[LOW]++;
}
else if (*p>='A' && *p<= 'Z') {
count[UP]++;
}
else if (*p>='0' && *p<= '9') {
count[DIGIT]++;
}
else {
count[OTHER]++;
}
p++;
n++;
}
for (i=0; ipercent[i] = (count[i]*1.0)/n;
}
return n;
}
int main()
{
char s[1000];
int i;
int n;
while(gets(s) != NULL) {
n = CharTypeCount(s);
if (n <=0 ) {
printf("please input a string!\n");
continue;
}
printf("a-z: %d/%d %.2f%%\n", count[LOW], n, percent[LOW]);
printf("A-Z: %d/%d %.2f%%\n", count[UP], n, percent[UP]);
printf("0-9: %d/%d %.2f%%\n", count[DIGIT], n, percent[DIGIT]);
printf("OTHER: %d/%d %.2f%%\n", count[OTHER], n, percent[OTHER]);
}
return 0;
}

回答2:

gip 还挺具体,要不是在上班,就给你答了