zoukankan      html  css  js  c++  java
  • 计算机二级-C语言-对标志位的巧妙使用。对二维数组数据进行处理。对文件进行数据输入。

    //函数fun的功能是:计算形参x所指数组中平均值(规定所有数均为正数),将所指数组中大于平均值的数据移至数组的前部,小于等于的移至后部,平均值作为返回值,在主函数中输出平均值和后移的数据。

    //重难点:对数组的处理。

     1 #include  <stdlib.h>
     2 #include  <stdio.h>
     3 #define   N   10
     4 double fun(double  *x)
     5 { int  i, j;    double  s, av, y[N];
     6   s=0;
     7   for(i=0; i<N; i++)  s=s+x[i];
     8 /**********found**********/
     9   av = s / N;
    10   for(i=j=0; i<N; i++)
    11     if( x[i]>av ){
    12 /**********found**********/
    13       y[j++]=x[i];
    14       x[i]=-1;}
    15   for(i=0; i<N; i++)
    16 /**********found**********/
    17     if( x[i]!= -1) y[j++]=x[i];
    18   for(i=0; i<N; i++)x[i] = y[i];
    19   return  av;
    20 }
    21 void main()
    22 { int  i;     double  x[N];
    23   for(i=0; i<N; i++){ x[i]=rand()%50; printf("%4.0f ",x[i]);}
    24   printf("
    ");
    25   printf("
    The average is: %f
    ",fun(x));
    26   printf("
    The result :
    ",fun(x));
    27   for(i=0; i<N; i++)  printf("%5.0f ",x[i]);
    28   printf("
    ");
    29 }

    //程序功能:读入一个英文文本行,将其中每个单词的第一行字母改成大写,然后输出此文本行(这里的单词指由空格隔开的字符串)。

    //重难点:通过指针对字符串的处理。通过标志位进行完整单词的判断。

     1 #include  <stdlib.h>
     2 #include  <string.h>
     3 #include  <conio.h>
     4 #include  <ctype.h>
     5 #include  <stdio.h>
     6 #include  <string.h>
     7 /*************found**************/
     8 void upfst(char *p)
     9 {
    10   int k=0;//通过k标志位进行判断时候为一个单词了。
    11   for ( ;*p;p++)
    12      if (k)
    13         {
    14          if (*p==' ')  
    15              k=0;
    16         }
    17      else
    18         {
    19             if (*p!=' ')  
    20             {
    21                 k=1;
    22                 *p=toupper(*p);
    23             }
    24         }
    25 }
    26 void main()
    27 {
    28     char  chrstr[81];
    29     system("CLS");    
    30     printf("
    Please enter an English text line: ");
    31     gets(chrstr);
    32     printf("
    Bofore changing:
      %s",chrstr);
    33     upfst(chrstr);
    34     printf("
    After changing:
      %s
    ",chrstr);
    35 }

    //在此程序中,定义了N*N的二维数组,并在主函数中赋值。编写函数fun功能是:求出数组周边元素的平均值并作为函数值返回给主函数中的s。

    //重难点:对二维数组矩阵的数据处理。对文件的处理。

     1 #include<stdio.h>
     2 #include<conio.h>
     3 #include<stdlib.h>
     4 #define  N  5
     5 double fun (int w[][N])
     6 {
     7     int i, j;
     8     double s = 0;
     9     for (i = 0; i < N; i++)
    10     {
    11         if(i==0||i==(N-1))
    12         {
    13             for (j = 0; j < N; j++)
    14             {
    15                 s += w[i][j];
    16             }
    17         }
    18         else
    19         {
    20             s = s + w[i][0] + w[i][N-1];
    21         }
    22     }
    23     return (s / (4 * (double)N - 4));
    24 }
    25 void main()
    26 {
    27   FILE *wf;
    28   int a[N][N]={0,1,2,7,9,1,9,7,4,5,2,3,8,3,1,4,5,6,8,2,5,9,1,4,1};
    29   int i, j;
    30   double s;
    31   system("CLS");
    32   printf("*****The array*****
     ");
    33   for (i=0; i<N; i++)
    34     { for (j=0;j<N;j++)
    35          {printf("%4d ",a[i][j]);}
    36       printf("
     ");
    37     }
    38   s=fun(a);
    39   printf("*****THE RESULT*****
     ");
    40   printf("The sum is : %lf
     ",s);
    41 /******************************/
    42   wf=fopen("out.dat","w");
    43   fprintf (wf,"%lf",s);
    44   fclose(wf);
    45 /*****************************/
    46 }
  • 相关阅读:
    Android之剑法初略:dalvik vm和jvm比较
    人民币阿拉伯数字转换为汉字大写 code
    数据库定时备份方案及实践
    [postfix]添加黑名单
    [php][随机数]曲线式的随机
    记公司服务器维护经历
    批量修改符号链接实现思路
    复杂数组的签名生成方法
    [已解决]ubuntu下chrome和firefox输入框内无法快捷键全选
    [已解决]centos6.4 php连接mysql和memcache提示权限不允许
  • 原文地址:https://www.cnblogs.com/ming-4/p/10283526.html
Copyright © 2011-2022 走看看