鱼C论坛

 找回密码
 立即注册
查看: 2533|回复: 6

求用数组简化一个程序!!!!!!!!!

[复制链接]
发表于 2014-4-6 17:48:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include<stdio.h>
  2. #include<ctype.h>
  3. int main(void)
  4. {
  5.         int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;
  6.         int blank=0;
  7.         int line_break=0;
  8.         char ch;
  9.         a=b=c=d=e=f=g=h=i=j=k=l=m=n=o=p=q=r=s=t=u=v=w=x=y=z=0;

  10.         printf("input a string chars: \n");
  11.         while((ch=getchar())!='#')
  12.         {
  13.                 if(ch=='a')
  14.                         a++;
  15.                 else if(ch=='b')
  16.                         b++;
  17.                 else if(ch=='c')
  18.                         c++;
  19.                 else if(ch=='d')
  20.                         d++;
  21.                 else if(ch=='e')
  22.                         e++;
  23.                 else if(ch=='f')
  24.                         f++;
  25.                 else if(ch=='g')
  26.                         g++;
  27.                 else if(ch=='h')
  28.                         h++;
  29.                 else if(ch=='i')
  30.                         i++;
  31.                 else if(ch=='j')
  32.                         j++;
  33.                 else if(ch=='k')
  34.                         k++;
  35.                 else if(ch=='l')
  36.                         l++;
  37.                 else if(ch=='m')
  38.                         m++;
  39.                 else if(ch=='n')
  40.                         n++;
  41.                 else if(ch=='o')
  42.                         o++;
  43.                 else if(ch=='p')
  44.                         p++;
  45.                 else if(ch=='q')
  46.                         q++;
  47.                 else if(ch=='r')
  48.                         r++;
  49.                 else if(ch=='s')
  50.                         s++;
  51.                 else if(ch=='t')
  52.                         t++;
  53.                 else if(ch=='u')
  54.                         u++;
  55.                 else if(ch=='v')
  56.                         v++;
  57.                 else if(ch=='w')
  58.                         w++;
  59.                 else if(ch=='x')
  60.                         x++;
  61.                 else if(ch=='y')
  62.                         y++;
  63.                 else if(ch=='z')
  64.                         z++;
  65.                 else if(ch==' ')
  66.                         blank++;
  67.                 else if(ch=='\n')
  68.                         line_break++;
  69.         }
  70.         printf("letter a=%d \n",a);
  71.         printf("letter b=%d \n",b);
  72.         printf("letter c=%d \n",c);
  73.         printf("letter d=%d \n",d);
  74.         printf("letter e=%d \n",e);
  75.         printf("letter f=%d \n",f);
  76.         printf("letter g=%d \n",g);
  77.         printf("letter h=%d \n",h);
  78.         printf("letter i=%d \n",i);
  79.         printf("letter j=%d \n",j);
  80.         printf("letter k=%d \n",k);
  81.         printf("letter l=%d \n",l);
  82.         printf("letter m=%d \n",m);
  83.         printf("letter n=%d \n",n);
  84.         printf("letter o=%d \n",o);
  85.         printf("letter p=%d \n",p);
  86.         printf("letter q=%d \n",q);
  87.         printf("letter r=%d \n",r);
  88.         printf("letter s=%d \n",s);
  89.         printf("letter t=%d \n",t);
  90.         printf("letter u=%d \n",u);
  91.         printf("letter v=%d \n",v);
  92.         printf("letter w=%d \n",w);
  93.         printf("letter x=%d \n",x);
  94.         printf("letter y=%d \n",y);
  95.         printf("letter z=%d \n",z);
  96.         printf("blank=%d \n",blank);
  97.         printf("line_blank=%d \n",line_break);
  98. getchar();
  99. getchar();
  100. return(0);
  101. }
复制代码
这个程序的作用是把用户输入的字符统计,按#退出
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-4-6 18:11:41 | 显示全部楼层
  1. #include<stdio.h>
  2. int main()
  3. {
  4.         char ch[26];
  5.         int n[26];
  6.         int i,t;
  7.         char str;
  8. s:        setbuf(stdin,NULL);//防止前一次的输入的影响
  9.         t = 97;
  10.         for(i = 0;i<26;i++)//只检测 a,b....z
  11.         {
  12.                 ch[i] = t;
  13.                 t++;
  14.         }

  15.         for(i = 0;i<26;i++)//个数清零
  16.         {
  17.                 n[i] = 0;
  18.         }

  19.         while((str = getchar())!='\n')//判断
  20.         {
  21.                 if(str == '#')
  22.                 {
  23.                         return 0;
  24.                 }
  25.                 for(i = 0;i<26;i++)
  26.                 {
  27.                         if(ch[i] == str)
  28.                         {
  29.                                 n[i]++;
  30.                                 break;
  31.                         }
  32.                 }
  33.         }
  34.         for(i = 0;i<26;i++)//输出
  35.         {
  36.                 if(n[i] != 0)
  37.                 {
  38.                         printf("%c : %d\n",ch[i],n[i]);
  39.                 }
  40.         }
  41.         goto s;//再次输入

  42. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-4-6 18:14:55 | 显示全部楼层
你能把这个程序写完我真心佩服你
我估计我会不耐烦的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-4-6 18:53:52 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-4-6 20:14:45 | 显示全部楼层
记得我等级考试时编程题就是统计a~z字母各有多少个
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-4-6 20:28:18 | 显示全部楼层
那你说啊!用数组
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-4-7 23:54:52 | 显示全部楼层

根据你代码的要求改的,还可以更加简单,如果你需要

本帖最后由 谭斌谭斌 于 2014-4-7 23:56 编辑
  1. #include<stdio.h>

  2. void fun()
  3. {
  4.         int arry[26] = { 0 , };
  5.         int val;
  6.         int i = 0;
  7.         char c;
  8.         
  9.         while( ( c = getchar() ) != '#' )
  10.         {
  11.                 val = c - 'a';
  12.                 arry[ val ]++;
  13.         }
  14.         
  15.         c = 'a';
  16.         while( i != 26 )
  17.         {
  18.                 printf( "letter %c=%d \n" , c++ , arry[ i++ ] );
  19.         }
  20. }

  21. int main()
  22. {
  23.         fun();
  24.         return 0;
  25. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-21 05:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表