本帖最后由 EchoWill 于 2014-4-28 19:17 编辑
- #include <stdio.h>
- #include <string.h>
- struct appear //定义结构体,存储字母和出现次数
- {
- char c;
- int count;
- };
- //声明函数
- void letter_print(struct appear ptr[]);
- void arrange(struct appear ptr[]);
- int main(int argc, const char * argv[])
- {
- // 定义结构体变量,并初始化
- struct appear countA,countB,countC,countD,arr[4];
- countA.c = 'A';
- countA.count = 0;
- countB.c = 'B';
- countB.count = 0;
- countC.c = 'C';
- countC.count = 0;
- countD.c = 'D';
- countD.count = 0;
-
- printf("请输入字符串(包括A、B、C、D):\n");
- char str[1024];
- scanf("%s",str);
- // 遍历每个字符
- for (int n = 0; str[n]!='\0';++n )
- {
- // 识别字符,并计数
- switch (str[n])
- {
- case 'A':
- ++countA.count;
- break;
- case 'B':
- ++countB.count;
- break;
- case 'C':
- ++countC.count;
- break;
- case 'D':
- ++countD.count;
- break;
-
- }
- arr[0] = countA;
- arr[1] = countB;
- arr[2] = countC;
- arr[3] = countD;
- }
复制代码
参考了大家讨论的代码,ABCD计数仍有错误。 |