• C语言-switch语句的使用。对文件的输出处理。for循环和if的结合使用。


    //函数fun功能:统计字符串中各元音字母的个数,注意:不区分大小写。

    //重难点:switch语句的使用。

     1 #include  <stdlib.h>
     2 #include  <conio.h>
     3 #include  <stdio.h>
     4 /*************found**************/
     5 void fun(char *s, int num[5])
     6 { int k, i=5;
     7 for (k = 0; k < i; k++)
     8     /*************found**************/
     9     num[k] = 0;//各个位进行清零。
    10   for(;*s;s++)
    11       { i=-1;
    12 /*************found**************/
    13         switch(*s)
    14            { case 'a': case'A':{i=0;break;}
    15              case 'e': case 'E':{i=1;break;}
    16              case 'i': case 'I':{i=2;break;}
    17              case 'o': case 'O':{i=3;break;}
    18              case 'u': case 'U':{i=4;break;}
    19            }
    20        if(i>=0)
    21        num[i]++;//总计数
    22       }
    23 }
    24 void main()
    25 { char s1[81]; int num1[5], i;
    26   system("CLS");
    27   printf("
    Please enter a string: ");
    28 gets(s1);
    29   fun(s1, num1);
    30   for(i=0;i<5;i++) printf("%d ",num1[i]);
    31 printf("
    ");
    32 }

    //函数fun功能是:求出二维数组周边元素之和,作为返回值返回。

    //重难点:对文件的输出处理。for循环和if的结合使用。

     1 #include<conio.h>
     2 #include<stdio.h>
     3 #include<stdlib.h>
     4 #define  M  4
     5 #define  N  5
     6 int fun( int a [M][N])
     7 {
     8     int i, j,s=0;
     9     for (i = 0; i < M; i++)
    10     {
    11         if (i == 0 || i == (M - 1))
    12         {
    13             for (j = 0; j < N; j++)
    14             {
    15                 s += a[i][j];
    16             }
    17         }
    18         else
    19         {
    20             s += a[i][0] + a[i][N - 1];
    21         }
    22     }
    23     return s;
    24 }
    25 void main()
    26 {
    27   FILE *wf;
    28   int aa[M][N]={{1,3,5,7,9},{2,9,9,9,4},{6,9,9,9,8},{1,3,5,7,0}};
    29   int i, j, y;
    30   system("CLS");
    31   printf ("The original data is :
     ");
    32   for(i=0; i<M;i++)
    33      {for (j=0; j<N;j++) 
    34           printf("%6d ",aa[i][j]);
    35       printf("
     ");
    36      }
    37   y=fun(aa);
    38   printf("
    The sun: %d
     ",y);
    39   printf("
     ");
    40 /******************************/
    41   wf=fopen("out.dat","w");
    42   fprintf (wf,"%d",y);
    43   fclose(wf);
    44 /*****************************/
    45 }
  • 相关阅读:
    网站页面性能优化的 34条黄金守则 (雅虎团队经验)
    进程调度算法小结
    玩转TCP连接
    数据包在网络中的流转
    浅入理解JVM虚拟机
    Leecode no.47 全排列 II
    Leecode no.143 重排链表
    关于我用设计模式对公司代码重构的这件事
    进程间通信方式小结
    Leecode no.82 删除排序链表中的重复元素 II
  • 原文地址:https://www.cnblogs.com/ming-4/p/10289163.html
Copyright © 2020-2023  润新知