I am a teacher!

导航

计算机等级考试二级C语言上机题集(第21~25套)

第21套

1.程序填空题

给定程序中,函数fun的功能是:进行数字字符转换。若形参ch中是数字字符“0”~“9”,则0转换成9,1转换成8,…,9转换成0;若是其他字符则保持不变;并将转换后的结果作为函数值返回。

请在下划线处填入正确的内容并将下划线删除,使程序得出正确的结果。

注意:不得增行或删行,也不得更改程序的结构!

#include    <stdio.h>
/**********found**********/
___1___ fun(char  ch)
{
    /**********found**********/
    if (ch>='0' && ___2___)
    /**********found**********/
      return  '9'- (ch-___3___);
   return  ch ;
}
int main()
{  
    char  c1, c2;
    printf("\nThe result  :\n");
    c1='2';   c2 = fun(c1);
    printf("c1=%c    c2=%c\n", c1, c2);
    c1='8';   c2 = fun(c1);
    printf("c1=%c    c2=%c\n", c1, c2);
    c1='a';   c2 = fun(c1);
    printf("c1=%c    c2=%c\n", c1, c2);
    return 0;
}

2.程序修改题

已知一个数列从第0项开始的前三项分别为0,0,1,以后的各项都是其相邻的前三项之和。给定程序中,函数fun的功能是:计算并输出该数列前n项的平方根之和sum。

例如,当n=10时,程序的输出结果应为:23.197745。

请改正函数fun中指定部位的错误,使它能得出正确的结果。

注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。

#include <stdio.h>
#include <math.h>
/************found************/
fun(int n)
{
    double  sum, s0, s1, s2, s;
    int k;
    sum = 1.0;
    if (n <= 2) sum = 0.0;
    s0 = 0.0;  s1 = 0.0;  s2 = 1.0;
    for (k = 4; k <= n; k++)
    {
        s = s0 + s1 + s2;
        sum += sqrt(s);
        s0 = s1; s1 = s2; s2 = s;
    }
    /************found************/
    return sum
}
int main()
{
    int n;
    printf("Input N=");
    scanf("%d", &n);
    printf("%f\n", fun(n));
    return 0;
}

3.程序设计题

编写函数fun,它的功能是:计算下列级数的和。

例如,当n=10,x=0.3时,函数值为1.349859。
注意:请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。

#include <stdio.h>
#include <math.h>
void NONO(void);
double fun(double x , int  n)
{

}
int main()
{
    printf("%f\n", fun(0.3,10));
    NONO();
    return 0;
}
void NONO()
{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
    FILE *fp, *wf ;
    int i, n ;
    double s, x ;
    fp = fopen("in.dat","r") ;
    wf = fopen("out.dat","w") ;
    for(i = 0 ; i < 10 ; i++)
    {
       fscanf(fp, "%lf,%d", &x, &n) ;
       s = fun(x, n) ;
       fprintf(wf, "%f\n", s) ;
    }
    fclose(fp) ;
    fclose(wf) ;
}
1.(1char2)ch<='9'3'0'
2double fun(int n)
     return sum;
3double fun(double x , int  n)
     {
          double  f, t;
          int  i;
          f = 1.0;
          t = 1.0;
          for (i=1; i<n; i++)
          {
               t *= x/i;
               f += t;
          }
          return  f;
      }
第21套参考答案

第22套

1.程序填空题

甲乙丙丁四人同时开始放鞭炮,甲每隔t1秒放一次,乙每隔t2秒放一次,丙每隔t3秒放一次,丁每隔t4秒放一次,每人各放n次。给定程序中,函数fun的功能是:根据形参提供的值,求出共听到多少次鞭炮声作为函数值返回。注意:当几个鞭炮同时炸响,只算一次响声,第1次响声是在第0秒。

例如,若t1=7,t2=5,t3=6,t4=4,n=10,则总共可听到28次鞭炮声。

请在下划线处填入正确的内容并将下划线删除,使程序得出正确的结果。

注意:不得增行或删行,也不得更改程序的结构!

#include    <stdio.h>
/**********found**********/
#define  OK(i, t, n)  ((___1___%t==0) && (i/t<n))
int fun(int  t1, int  t2, int  t3, int  t4, int  n)
{  
    int  count, t , maxt=t1;
    if (maxt < t2) maxt = t2;
    if (maxt < t3) maxt = t3;
    if (maxt < t4) maxt = t4;
    count=1;    
    /**********found**********/
    for(t=1; t< maxt*(n-1); ___2___)
    {
      if(OK(t, t1, n) || OK(t, t2, n)|| OK(t, t3, n) || OK(t, t4, n) )
         count++;
    }
    /**********found**********/
    return ___3___;
}
int main()
{  
    int  t1=7, t2=5, t3=6, t4=4, n=10, r;
    r = fun(t1, t2, t3, t4, n);
    printf("The sound  :  %d\n", r);
    return 0;
}

2.程序修改题

给定程序中,函数fun的功能是:对N名学生的学习成绩,按从高到低的顺序找出前m(m<=10)名学生来,并将这些学生数据存放在一个动态分配的连续存储区中,此存储器的首地址作为函数值返回。

请改正函数fun中指定部位的错误,使它能得出正确的结果。

注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。

#include <stdio.h>
#include <alloc.h>
#include <string.h>
#define    N  10
typedef  struct  ss
{  
    char  num[10];
    int  s;
} STU;
STU *fun(STU  a[], int  m)
{  
    STU  b[N], *t;
    int  i,j,k;
    /**********found**********/
    t=(STU *)calloc(sizeof(STU),m)
    for(i=0; i<N; i++)  b[i]=a[i];
      for(k=0; k<m; k++)
      {  
          for(i=j=0; i<N; i++)
             if(b[i].s > b[j].s)  j=i;
    /**********found**********/
          t(k)=b(j);
          b[j].s=0;
      }
    return  t;
}
void outresult(STU  a[], FILE  *pf)
{  
    int  i;
    for(i=0; i<N; i++)
      fprintf(pf,"No=%s Mark=%d\n",a[i].num,a[i].s);
    fprintf(pf,"\n\n");
}
int main()
{  
    STU  a[N]={ {"A01",81},{"A02",89},
                {"A03",66},{"A04",87},
                {"A05",77},{"A06",90},
                {"A07",79},{"A08",61},
                {"A09",80},{"A10",71}};
    STU  *pOrder;
    int  i, m;
    printf("***** The Original data *****\n");
    outresult(a, stdout);
    printf("\nGive the number of the students who have better score:  ");
    scanf("%d",&m);
    while( m>10 )
    { 
        printf("\nGive the number of the students who have better score:  ");
        scanf("%d",&m);
    }
    pOrder=fun(a,m);
    printf("***** THE  RESULT *****\n");
    printf("The top  :\n");
    for(i=0; i<m; i++)
      printf("%s  %d\n",pOrder[i].num , pOrder[i].s);
    free(pOrder);
    return 0;
}

3.程序设计题

编写函数fun,它的功能是:删除一维数组中所有相同的数,使之只剩一个。数组中的数已按由小到大的顺序排列,函数返回删除后数组中数据的个数。

例如,一维数组中的数据为:2 2 2 3 4 4 5 6 6 6 6 7 7 8 9 9 10 10 10,删除后,数组中的内容应该是:2 3 4 5 6 7 8 9 10。

注意:请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。

#include <stdio.h>
#define N 80
void NONO(void);
int  fun(int  a[], int  n)
{

}
int main()
{
    int  a[N]={2,2,2,3,4,4,5,6,6,6,6,7,7,8,9,9,10,10,10,10},i,n=20;
    printf("The original data :\n");
    for (i=0; i<n; i++) printf("%3d",a[i]);
    n=fun(a,n);
    printf("\n\nThe data after deleted :\n");
    for (i=0;i<n;i++) printf("%3d",a[i]);
    printf("\n\n");
    NONO();
    return 0;
}
void NONO(void)
{/* 请在此函数内打开文件,输入测试数据,调用 fun 函数,输出数据,关闭文件。 */
    FILE *rf, *wf;
    int a[N], n, i, j ;
    rf = fopen("in.dat","r") ;
    wf = fopen("out.dat","w") ;
    for (i = 0 ; i < 5 ; i++)
    {
       fscanf(rf, "%d", &n) ;
       for (j = 0 ; j < n ; j++)
          fscanf(rf, "%d", &a[j]) ;
       n = fun(a, n) ;
       for (j = 0 ; j < n ; j++)
          fprintf(wf, "%4d", a[j]) ;
       fprintf(wf, "\n") ;
    }
    fclose(rf) ;
    fclose(wf) ;
}
1.(1)i   (2)t++   (3)count
2. t=(STU *)calloc(sizeof(STU),m);
     t[k]=b[j];
3int  fun(int  a[], int  n)
     {
          int i,j;
          j=0;
          for (i=1;i<n;i++)
              if (a[i]!=a[j])
                  a[++j]=a[i];
          return j+1;
      }
第22套参考答案

第23套

1.程序填空题

给定程序中,函数fun的功能是:将形参a所指数组中的前半部分元素中的值和后半部分元素中的值对换。形参n中存放数组中数据的个数,若n为奇数,则中间的元素不动。

例如,若a所指数组中的数据依次为:1 2 3 4 5 6 7 8 9,则调换后为:6 7 8 9 5 1 2 3 4。

请在下划线处填入正确的内容并将下划线删除,使程序得出正确的结果。

注意:不得增行或删行,也不得更改程序的结构!

#include  <stdio.h>
#define    N    9
void fun(int  a[], int  n)
{  
    int  i, t, p;
    /**********found**********/
    p = (n%2==0)?n/2:n/2+___1___;
    for (i=0; i<n/2; i++)
    {
        t=a[i];
    /**********found**********/
        a[i] = a[p+___2___];
    /**********found**********/
        ___3___ = t;
    }
}
int main()
{  
    int  b[N]={1,2,3,4,5,6,7,8,9}, i;
    printf("\nThe original data  :\n");
    for (i=0; i<N; i++)  printf("%4d ", b[i]);
    printf("\n");
    fun(b, N);
    printf("\nThe data after moving  :\n");
    for (i=0; i<N; i++)  printf("%4d ", b[i]);
    printf("\n");
    return 0;
}

2.程序修改题

给定程序中,函数fun的功能是:在p所指字符串中找出ASCII码值最大的字符,将其放在第1个位置上,并将该字符前的原字符向后顺序移动。

例如,调用函数前字符串为:GABCDeFGH,调用函数后字符串为:eGABCDFGH。

请改正函数fun中指定部位的错误,使它能得出正确的结果。

注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。

#include <stdio.h>
void fun(char *p)
{   
    char  max,*q;  
    int i=0;
    max=p[i];
    while (p[i]!=0)
    {  
        if (max<p[i])
        {  
            max=p[i];
    /************found************/
            p=q+i;
        }
        i++;
    }
    /************found************/
    while (q<p)
    {
        *q=*(q-1);
        q--;
    }
    p[0]=max;
}
int main()
{
    char str[80];
    printf("Enter a string:");
    gets(str);
    printf("\nThe original string:    ");
    puts(str);
    fun(str);
    printf("\nThe string after moving: ");
    puts(str);
    printf("\n\n");
    return 0;
}

3.程序设计题

M个学生的成绩存放在score数组中,编写函数fun,它的功能是:将低于平均分的成绩存放到数组below中,并将低于平均分的人数作为函数值返回。

例如,当score数组中的数据为:10 20 30 40 50 60 70 80 90时,函数返回值为4,below中的数据为:10 20 30 40。

注意:请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。

#include <stdio.h>
void NONO(void);
int fun(int score[],int m,int below[])
{

}
int main()
{
    int i,n,below[9];
    int score[9]={10,20,30,40,50,60,70,80,90};
    n=fun(score,9,below);
    printf("\nBelow the average score are:");
    for(i=0;i<n;i++) printf("%d ",below[i]);
    NONO();
    return 0;
}
void NONO(void)
{/* 本函数用于打开文件,输入测试数据,调用fun函数,输出数据,关闭文件。*/
    int i, j, n, below[10], score[10];
    FILE *rf, *wf ;
    rf = fopen("in.dat", "r") ;
    wf = fopen("out.dat", "w") ;
    for(i = 0 ; i < 10 ; i++)
    {
       for(j = 0 ; j < 10 ; j++)
          fscanf(rf, "%d", &score[j]) ;
       n = fun(score, 10, below) ;
       for(j = 0 ; j < n ; j++)
          fprintf(wf, "%d ", below[j]) ;
       fprintf(wf, "\n") ;
    }
    fclose(rf) ;
    fclose(wf) ;
}
1.(112)i   (3)a[p+i]
2. q=p+i;
      while (q>p)
3int fun(int score[],int m,int below[])
      {
           int s=0,i,k;
           for (i=0;i<m;i++)
                s+=score[i];
           s=s/m;
           k=0;
           for (i=0;i<m;i++)
               if (score[i]<s)
                   below[k++]=score[i];
           return k;
      }
第23套参考答案

第24套

1.程序填空题

给定程序中,函数fun的功能是:把形参a所指数组中的最大值放在a[0]中,接着求出a所指数组中的最小值放在a[1]中;再把形参a所指数组中的次大值放在a[2]中,次小值放在a[3]中;其余以此类推。

例如,若a所指数组中数据最初排列为:1、4、2、3、9、6、5、8、7,则按规则移动后,数据排列为:9、1、8、2、7、3、6、4、5。

请在下划线处填入正确的内容并将下划线删除,使程序得出正确的结果。

注意:不得增行或删行,也不得更改程序的结构!

#include <stdio.h>
#define N  9
/**********found**********/
void fun(int  ___1___, int  n)
{  
    int  i, j, max, min, px, pn, t;
    /**********found**********/
    for (i=0; i<n-1; i+=___2___)
    {  
        max = min = a[i];
        px = pn = i;
    /**********found**********/
        for (j=___3___; j<n; j++)
        {  
            if (max < a[j])
            {  max = a[j]; px = j;  }
            if (min > a[j])
            {  min = a[j]; pn = j;  }
        }
        if (px != i)
        {  
            t = a[i]; a[i] = max; a[px] = t;
            if (pn == i) pn= px;
        }
        if (pn != i+1)
        {   t = a[i+1]; a[i+1] = min; a[pn] = t; }
    }
}
int main()
{  
    int  b[N]={1,4,2,3,9,6,5,8,7}, i;
    printf("\nThe original data  :\n");
    for (i=0; i<N; i++)  printf("%4d ", b[i]);
    printf("\n");
    fun(b, N);
    printf("\nThe data after moving  :\n");
    for (i=0; i<N; i++)  printf("%4d ", b[i]);
    printf("\n");
    return 0;
}

2.程序修改题

给定程序中,函数fun的功能是:找出一个大于形参m且紧随m的素数,并作为函数值返回。

请改正函数fun中指定部位的错误,使它能得出正确的结果。

注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。

#include <stdio.h>
int fun(int m)
{  
    int i, k ;
    for (i = m + 1 ; ; i++) 
    {
       for (k = 2 ; k < i ; k++)
    /**************found**************/
          if (i % k != 0)
             break ;
    /**************found**************/
         if (k < i)
            return i;
    }
}
int main()
{
    int m,t;
    scanf("%d",&m); 
    t=fun(m);
    printf("%d",t);
    return 0;
}

3.程序设计题

编写函数fun,它的功能是:求出能整除形参x且不是偶数的各整数,并按从小到大的顺序放在pp所指的数组中,这些除数的个数通过形参n返回。

例如,若x=30,则有4个数符合要求,它们是:1,3,5,15。

注意:请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。

#include <stdio.h>
void NONO(void);
void  fun(int x, int pp[], int *n)
{

}
int main()
{
    int  x, aa[1000], n, i ;
    printf( "\nPlease enter an integer number:\n" );
    scanf("%d", &x) ;
    fun(x, aa, &n ) ;
    for( i = 0 ; i < n ; i++ )
         printf("%d ", aa[i]) ;
    printf("\n") ;
    NONO();
    return 0;
}
void NONO(void)
{/* 本函数用于打开文件,输入测试数据,调用fun函数,输出数据,关闭文件。*/
    int x, aa[1000], n, i, j ;
    FILE *fp ;
    fp = fopen("out.dat","w") ;
    for (j = 0 ; j < 10 ; j++)
    {
       x = 30 + j ;
       fun(x, aa, &n) ;
       for (i = 0 ; i < n ; i++) fprintf(fp, "%d ", aa[i]) ;
       fprintf(fp, "\n") ;
    }
    fclose(fp) ;
}
1.(1)a[]  或 *a   (223)i+1
2if (i % k == 0)
      if (k >= i)
3void  fun(int x, int pp[], int *n)
      {
          int k=0,i;
          for (i=1;i<=x;i+=2)
              if (x%i==0)  pp[k++]=i;
          *n=k;
      }
第24套参考答案

第25套

1.程序填空题

给定程序中,函数fun的功能是:将形参std所指结构体数组中年龄最大者的数据作为函数值返回。

请在下划线处填入正确的内容并将下划线删除,使程序得出正确的结果。

注意:不得增行或删行,也不得更改程序的结构!

#include  <stdio.h>
typedef  struct
{  
    char  name[10];
    int  age;
}STD;
STD fun(STD  std[], int  n)
{  
    STD  max;        
    int  i;
    /**********found**********/
    max= ___1___;
    for(i=1; i<n; i++)
    /**********found**********/
      if(max.age<___2___)  max=std[i];
    return max;
}
int main( )
{  
    STD  std[5]={"aaa",17,"bbb",16,
              "ccc",18,"ddd",17,"eee",15  };
    STD  max;
    max=fun(std,5);
    printf("\nThe result: \n");
    /**********found**********/
    printf("\nName:%s, Age:%d\n", __3__,max.age);
    return 0;
}

2.程序修改题

给定程序中,函数fun的功能是:计算

S=f(-n)+f(-n+1)+…+f(0)+f(1)+f(2)+…+f(n)的值。

f(x)函数定义如下:

              (x+1)/(x-2)    x>0且x!=2

f(x)=      0                   x=0或x=2

              (x-1)/(x-2)    x<0

例如,当n=5时,函数值应为:10.407143。

请改正函数fun中指定部位的错误,使它能得出正确的结果。

注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。

#include <stdio.h>
#include <math.h>
/************found************/
f( double x)
{
   if (x == 0.0 || x == 2.0)
     return 0.0;
   else if (x < 0.0)
     return (x -1)/(x-2);
   else
     return (x +1)/(x-2);
}
double fun(int  n)
{  
    int i;  
    double  s=0.0, y;
    for (i= -n; i<=n; i++)
    { y=f(1.0*i); s += y; }
    /************found************/
    return s
}
int main ( )
{
    printf("%f\n", fun(5) );
    return 0;
}

3.程序设计题

编写函数fun,它的功能是:计算

   的值。

例如,若m=20,函数值为:6.506583。

注意:请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。

#include <stdio.h>
#include <math.h>
void NONO(void);
double  fun(int  m)
{

}
int main()
{
    printf("%f\n", fun(20));
    NONO();
    return 0;
}
void NONO(void)
{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
    FILE *fp, *wf ;
    int i, n ;
    double s ;
    fp = fopen("in.dat","r") ;
    wf = fopen("out.dat","w") ;
    for(i = 0 ; i < 10 ; i++)
    {
       fscanf(fp, "%d", &n) ;
       s = fun(n) ;
       fprintf(wf, "%f\n", s) ;
    }
    fclose(fp) ;
    fclose(wf) ;
}
1.(1)std[0]   (2)std[i].age   (3)max.name
2double f(double x)
      return s;
3double  fun(int  m)
      {
          double s=0.0;
          int i;
          for (i=1;i<=m;i++)
              s+=log(i);
          s=sqrt(s);
          return s;
      }
第25套参考答案

posted on 2022-11-06 04:49  aTeacher  阅读(376)  评论(0编辑  收藏  举报