I am a teacher!

导航

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

第86套

1.程序填空题

给定程序中,函数fun的功能是:将a所指4*3矩阵中第k行的元素与第0行元素交换。

例如,有下列矩阵

1 2 3

4 5 6

7 8 9

10 11 12

若k为2,程序执行结果为

7 8 9

4 5 6

1 2 3

10 11 12

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

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

#include  <stdio.h>
#define   N   3
#define   M   4
/**********found**********/
void fun(int (*a)[N], int __1__)
{ 
    int i,temp ;
    /**********found**********/
    for(i = 0 ; i < __2__ ; i++)
    { 
        temp=a[0][i] ;
    /**********found**********/
        a[0][i] = __3__ ;
        a[k][i] = temp ;
    }
}
int main()
{ 
    int x[M][N]={ {1,2,3},{4,5,6},{7,8,9},{10,11,12} },i,j;
    printf("The array before moving:\n\n");
    for(i=0; i<M; i++)
    {  
        for(j=0; j<N; j++) 
            printf("%3d",x[i][j]);
        printf("\n\n");
    }
    fun(x,2);
    printf("The array after moving:\n\n");
    for(i=0; i<M; i++)
    {  
        for(j=0; j<N; j++) 
            printf("%3d",x[i][j]);
        printf("\n\n");
    }
    return 0;
}

2.程序修改题

给定程序中,函数fun的功能是:给一维数组a输入任意4个整数,并按下例的规律输出。

例如,输入1、2、3、4,程序运行后将输出以下方阵。

4 1 2 3

3 4 1 2

2 3 4 1

1 2 3 4

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

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

#include <stdio.h>
#define M  4
/**************found**************/
void fun(int  a)
{  
    int  i,j,k,m;
    printf("Enter 4 number :  ");
    for(i=0; i<M; i++)  
        scanf("%d",&a[i]);
    printf("\n\nThe result  :\n\n");
    for(i=M;i>0;i--)
    {  
        k=a[M-1];
        for(j=M-1;j>0;j--)
    /**************found**************/
           aa[j]=a[j-1];
        a[0]=k;
        for(m=0; m<M; m++)  
           printf("%d  ",a[m]);
        printf("\n");
    }
}
int main()
{  
    int  a[M];
    fun(a); 
    printf("\n\n");
    return 0;
}

3.程序设计题

编写函数fun,它的功能是:计算给定整数n的所有因子(不包括1和n自身)之和。规定n的值不大于1000。

例如,若n输入的值为856,则输出为:sum=763。

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

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

}
int main()
{
    int  n,sum;
    printf("Input n:  ");
    scanf("%d",&n);
    sum=fun(n);
    printf("sum=%d\n",sum);
    NONO();
    return 0;
}
void NONO(void)
{/* 请在此函数内打开文件,输入测试数据,调用 fun 函数,输出数据,关闭文件。 */
    FILE *rf, *wf ;
    int i, n, sum ;
    rf = fopen("in.dat","r") ;
    wf = fopen("out.dat","w") ;
    for(i = 0 ; i < 10 ; i++)
    {
       fscanf(rf, "%d", &n) ;
       sum = fun(n) ;
       fprintf(wf, "%d=%d\n", n, sum) ;
    }
    fclose(rf) ;
    fclose(wf) ;
}
1.(1)k    (2)N    (3)a[k][i]
2void fun(int  a[])
    a[j]=a[j-1];
3int fun(int  n)
    {
        int s=0,i;
        for (i=2;i<n;i++)
            if (n%i==0) s+=i;
        return s;
    }
第86套参考答案

第87套

1.程序填空题

给定程序中,通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。函数fun的功能是:将形参a所指结构体变量中的数据赋给函数中的结构体变量b,并修改b中的学号和姓名,最后输出修改后的数据。

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

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

#include  <stdio.h>
#include  <string.h>
struct student 
{
    long  sno;
    char  name[10];
    float  score[3];
};
void fun(struct  student  a)
{ 
    struct student  b;    
    int  i;
    /**********found**********/
    b = __1__;
    b.sno = 10002;
    /**********found**********/
    strcpy(__2__, "LiSi");
    printf("\nThe data after modified :\n");
    printf("\nNo: %ld  Name: %s\nScores:  ",b.sno, b.name);
    /**********found**********/
    for (i=0; i<3; i++)  printf("%6.2f ",  b.__3__);
    printf("\n");
}
int main()
{ 
    struct student s={10001,"ZhangSan",95,80,88};
    int  i;
    printf("\nThe original data :\n");
    printf("\nNo: %ld  Name: %s\nScores:  ",s.sno, s.name);
    for (i=0; i<3; i++)  
        printf("%6.2f ",  s.score[i]);
    printf("\n");
    fun(s);
    return 0;
}

2.程序修改题

给定程序中,函数fun的功能是:从s所指字符串中删除所有小写字母c。

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

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

#include <stdio.h>
void  fun( char  *s )
{   
    int  i,j;
    for(i=j=0; s[i]!='\0'; i++)
      if(s[i]!='c')
    /************found************/
      s[j]=s[i];
    /************found************/
    s[i]='\0';
}
int main()
{  
    char  s[80];
    printf("Enter a string:       "); 
    gets(s);
    printf("The original string:  "); 
    puts(s);
    fun(s);
    printf("The string after deleted :  "); 
    puts(s);
    printf("\n\n");
    return 0;
}

3.程序设计题

假定输入的字符串中只包含字母和*号。编写函数fun,它的功能是:将在字符串中的前导*号全部移到字符串的尾部。

例如,字符串中的内容为:*****A*BC*DEF*G***,移动后,字符串的内容应当为:A*BC*DEF*G********。

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

#include <stdio.h>
#include <string.h>
void NONO(void);
void  fun( char *a )
{

}
int main()
{
    char  s[81];
    printf("Enter a string:\n");
    gets(s);
    fun( s );
    printf("The string after moveing:\n");
    puts(s);
    NONO();
    return 0;
}
void NONO(void)
{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
    FILE *in, *out ;
    int i ;
    char s[81] ;
    in = fopen("in.dat","r") ;
    out = fopen("out.dat","w") ;
    for(i = 0 ; i < 10 ; i++)
    {
       fscanf(in, "%s", s) ;
       fun(s) ;
       fprintf(out, "%s\n", s) ;
    }
    fclose(in) ;
    fclose(out) ;
}
1.(1)a   (2)b.name    (3)score[i]
2. s[j++]=s[i];
    s[j]='\0';
3void  fun( char *a )
    {
         int i,j;
         for (i=0;a[i]=='*';i++) ;
         for (j=0;a[i]!='\0';i++)
             a[j++]=a[i];
         while (j<i) a[j++]='*';
         a[j]='\0';
    }
第87套参考答案

第88套

1.程序填空题

给定程序中,通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。函数fun的功能是:将形参b所指结构体变量中的数据进行修改,最后在主函数中输出修改后的数据。

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

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

#include  <stdio.h>
#include  <string.h>
struct student 
{
    long  sno;
    char  name[10];
    float  score[3];
};
void fun(struct student  *b)
{ 
    int  i;
    /**********found**********/
    b__1__ = 10004;
    /**********found**********/
    strcpy(b__2__, "LiJie");
}
int main()
{ 
    struct student t={10002,"ZhangQi",93,85,87};
    int  i;
    printf("\n\nThe original data :\n");
    printf("\nNo: %ld  Name: %s\nScores:  ",t.sno, t.name);
    for (i=0; i<3; i++)  printf("%6.2f ", t.score[i]);
    printf("\n");
    /**********found**********/
    fun(__3__);
    printf("\nThe data after modified :\n");
    printf("\nNo: %ld  Name: %s\nScores:  ",t.sno, t.name);
    for (i=0; i<3; i++)  
        printf("%6.2f ", t.score[i]);
    printf("\n");
    return 0;
}

2.程序修改题

给定程序中,函数fun的功能是:应用递归法求形参a的平方根。求平方根的迭代公式如下:

     X1=1/2(x0+a/x0)

例如,a为2时,平方根的值为:1.414214。

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

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

#include <stdio.h>
#include <math.h>
/**********found**********/
fun(double a, dounle x0)
{   
    double   x1, y;
    x1=(x0+ a/x0)/2.0;
    /**********found**********/
    if( fabs(x1-xo)>0.00001 )
       y=fun(a,x1);
    else  y=x1;
    return  y;
}
int main()
{   
    double  x;
    printf("Enter x: "); 
    scanf("%lf",&x);
    printf("The square root of %f is %f\n",x,fun(x,1.0));
    return 0;
}

3.程序设计题

学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中。编写函数fun,它的功能是:把高于等于平均分的学生数据放在b所指的数组中,高于等于平均分的学生人数通过形参n传回,平均分通过函数值返回。

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

#include <stdio.h>
#define   N   12
typedef  struct
{
    char  num[10];
    double  s;
} STREC;
double fun(STREC *a, STREC *b, int *n)
{

}
int main()
{
    STREC  s[N]={{"GA05",85},{"GA03",76},
                 {"GA02",69},{"GA04",85},
                 {"GA01",91},{"GA07",72},
                 {"GA08",64},{"GA06",87},
                 {"GA09",60},{"GA11",79},
                 {"GA12",73},{"GA10",90}};
    STREC  h[N], t;FILE *out ;
    int  i,j,n;
    double  ave;
    ave=fun(s,h,&n);
    printf("The %d student data which is higher than %7.3f:\n",n,ave);
    for(i=0;i<n; i++)
      printf("%s  %4.1f\n",h[i].num,h[i].s);
    printf("\n");
    out = fopen("out.dat","w") ;
    fprintf(out, "%d\n%7.3f\n", n, ave);
    for(i=0;i<n-1;i++)
      for(j=i+1;j<n;j++)
         if(h[i].s<h[j].s) {t=h[i] ;h[i]=h[j]; h[j]=t;}
    for(i=0;i<n; i++)
       fprintf(out,"%4.1f\n",h[i].s);
    fclose(out);
    return 0;
}
1.(1)->sno   (2)->name   (3)&t
2double fun(double a, double x0)
    if( fabs(x1-x0)>0.00001)
3double fun(STREC *a, STREC *b, int *n)
    {
         double sum=0,avg;
         int i,j;
         for (i=0;i<N;i++)
              sum+=a[i].s;
         avg=sum/N;
         for (i=j=0;i<N;i++)
              if (a[i].s>=avg) b[j++]=a[i];
        *n=j;
        return avg;
    }
第88套参考答案

第89套

1.程序填空题

给定程序中,函数fun的功能是:计算出形参s所指字符串中包含的单词个数,作为函数值返回。为便于统计,规定各单词之间用空格分隔。

例如,形参s所指的字符串为:This is a C language program.,函数的返回值为6。

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

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

#include <stdio.h>
int fun(char  *s)
{ 
    int  n=0, flag=0;
    while(*s!='\0')
    { 
        if(*s!=' ' && flag==0) 
        {
    /**********found**********/
            __1__ ;  flag=1;
        }
    /**********found**********/
        if (*s==' ')  flag= __2__ ;
    /**********found**********/
        __3__ ;
    }
    return  n;
}
int main()
{ 
    char  str[81];    
    int  n;
    printf("\nEnter a line text:\n");  
    gets(str);
    n=fun(str);
    printf("\nThere are %d words in this text.\n",n);
    return 0;
}

2.程序修改题

给定程序中,函数fun的功能是:从n个学生的成绩中统计出低于平均分的学生人数,作为函数值返回。平均分存放在形参aver所指的存储单元中。

例如,若输入8名学生的成绩:80.5 60 72 90.5 98 51.5 88 64,则低于平均分的学生人数为4(平均分为:75.5625)。

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

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

#include <stdio.h>
#define  N   20
int fun(float  *s, int n, float *aver)
{  
    float  ave, t = 0.0 ;
    int  count = 0, k, i ;
    for ( k = 0 ; k < n ; k++ )
    /**************found**************/
        t = s[k] ;
    ave =  t / n ;
    for (  i = 0 ; i < n ; i++ )
      if ( s[ i ] < ave ) count++ ;
    /**************found**************/
    *aver = &Ave ;
    return  count ;
}
int main()
{  
    float  s[30], aver ;
    int  m, i ;
    printf ( "\nPlease enter m:  " ) ; 
    scanf ("%d", &m ) ;
    printf ( "\nPlease enter %d mark :\n ", m ) ;
    for( i = 0 ; i < m ; i++ ) scanf ( "%f", s + i ) ;
    printf("\nThe number of students : %d\n",fun(s,m,&aver));
    printf("Ave = %f\n", aver ) ;
    return 0;
}

3.程序设计题

编写函数fun,它的功能是:求出数组的最大元素在数组中的下标并存放在k所指的存储单元中。

例如,输入如下整数:876 675 896 101 301 401 980 431 451 777

则输出结果为:6,980

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

#include <stdio.h>
void NONO(void);
void fun(int *s, int t, int *k)
{

}
int main( )
{
    int a[10]={876,675,896,101,301,401,
               980,431,451,777}, k ;
    fun(a, 10, &k) ;
    printf("%d, %d\n", k, a[k]) ;
    NONO();
    return 0;
}
void NONO(void)
{/* 本函数用于打开文件,输入测试数据,调用fun函数,输出数据,关闭文件。*/
    int a[10], i, k, j ;
    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", &a[j]) ;
       fun(a, 10, &k) ;
       fprintf(wf, "%d,%d\n", k, a[k]) ;
    }
    fclose(rf) ;
    fclose(wf) ;
}
1.(1)n++   (2)s++   (3)s++
2. t += s[k] ;
    *aver = ave ;
3void fun(int *s, int t, int *k)
    {
          int i,max;
          max=0;
          for (i=1;i<t;i++)
              if (s[max]<s[i]) max=i;
         *k=max;
    }
第89套参考答案

第90套

1.程序填空题

给定程序中,函数fun的功能是:在形参ss所指字符串数组中,删除所有串长超过k的字符串,函数返回所剩字符串的个数。

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

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

#include  <stdio.h>
#include  <string.h>
#define   N   5
#define   M   10
int fun(char  (*ss)[M], int  k)
{ 
    int  i,j=0,len;
    /**********found**********/
    for(i=0; i< __1__ ; i++)
    {  
        len=strlen(ss[i]);
    /**********found**********/
        if (len<= __2__)
    /**********found**********/
           strcpy(ss[j++],__3__);
    }
    return  j;
}
int main()
{ 
    char  x[N][M]={"Beijing","Shanghai",
             "Tianjing","Nanjing","Wuhan"};
    int  i,f;
    printf("\nThe original string\n\n");
    for(i=0;i<N;i++) puts(x[i]);  
    printf("\n");
    f=fun(x,7);
    printf("The string witch length is less than or equal to 7 :\n");
    for(i=0; i<f; i++)  puts(x[i]);
    printf("\n");
    return 0;
}

2.程序修改题

给定程序中,函数fun的功能是:根据整型形参n,计算如下公式的值。

 

例如,若n=10,则应输出:0.617977。

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

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

#include <stdio.h>
/************found************/
int  fun(int n)
{  
    float  A=1; 
    int i;
    /************found************/
    for (i=2; i<n; i++)
      A = 1.0/(1+A);
    return A ;
}
int main( )
{  
    int  n ;
    printf("\nPlease enter n: ") ;
    scanf("%d", &n ) ;
    printf("A%d=%f\n", n, fun(n)) ;
    return 0;
}

3.程序设计题

编写函数fun,它的功能是:将N*N的二维数组右上三角元素中的值乘以m。

例如,若m=2,a数组中的值为

1 9 7

2 3 8

4 5 6

则返回主函数后a数组中的值应为

2 18 14

2 6 16

4 5 12

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

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

}
int main()
{
    int  a[N][N], m, i, j;
    printf("***** The array *****\n");
    for ( i =0;  i<N; i++ )
    {
        for ( j =0; j<N; j++ )
        {
            a[i][j] = rand()%20;
            printf( "%4d", a[i][j] );
        }
        printf("\n");
    }
    do m = rand()%10 ; while ( m>=3 );
    printf("m = %4d\n",m);
    fun ( a ,m );
    printf ("THE  RESULT\n");
    for ( i =0;  i<N; i++ )
    {
        for ( j =0; j<N; j++ )
            printf( "%4d", a[i][j] );
        printf("\n");
    }
    NONO();
    return 0;
}
void NONO(void)
{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
    FILE *rf, *wf ;
    int i, j, n, a[5][5] ;
    rf = fopen("in.dat","r") ;
    wf = fopen("out.dat","w") ;
    for(i = 0 ; i < 5 ; i++)
       for(j = 0 ; j < 5 ; j++)
          fscanf(rf, "%d ", &a[i][j]) ;
    fscanf(rf, "%d", &n) ;
    fun(a, n) ;
    for ( i = 0;  i < 5; i++ )
    {
       for ( j = 0; j < 5; j++ )
          fprintf(wf, "%4d", a[i][j] );
       fprintf(wf, "\n");
    }
    fclose(rf) ;
    fclose(wf) ;
}
1.(1)N   (2)k   (3)ss[i]
2float fun(int n)
    for (i=2; i<=n; i++)
3void fun(int a[][N], int m )
    {
         int i,j;
         for (i=0;i<N;i++)
             for (j=i;j<N;j++)
                 a[i][j]*=m;
    }
第90套参考答案

posted on 2022-11-08 05:28  aTeacher  阅读(242)  评论(0编辑  收藏  举报