欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

计算机等级考试二级 C 语言在线试题集(第 66~70 套) - 第 68 套

最编程 2024-06-18 10:00:39
...

1.程序填空题

给定程序中,通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。所有学生数据均以二进制方式输出到文件中。函数fun的功能是:从形参filename所指的文件中读入学生数据,并按照学号从小到大排序后,再用二进制方式把排序后的学生数据输出到filename所指的文件中,覆盖原来的文件内容。

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

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

#include <stdio.h>
#define  N  5
typedef struct student 
{
    long  sno;
    char  name[10];
    float  score[3];
} STU;
void fun(char  *filename)
{ 
    FILE  *fp;      
    int  i, j;
    STU  s[N], t;
    /**********found**********/
    fp = fopen(filename, __1__);
    fread(s, sizeof(STU), N, fp);
    fclose(fp);
    for (i=0; i<N-1; i++)
      for (j=i+1; j<N; j++)
    /**********found**********/
         if (s[i].sno __2__s[j].sno)
         {  t=s[i];  s[i]=s[j];  s[j]=t; }
    fp = fopen(filename, "wb");
    /**********found**********/
    __3__(s, sizeof(STU), N, fp);
    fclose(fp);
}
int main()
{ 
    STU  t[N]={ {10005,"ZhangSan", 95, 80, 88}, 
             {10003,"LiSi", 85, 70, 78},
             {10002,"CaoKai", 75, 60, 88}, 
             {10004,"FangFang", 90, 82, 87},
             {10001,"MaChao", 91, 92, 77}}, ss[N];
    int  i,j;      
    FILE  *fp;
    fp = fopen("student.dat", "wb");
    fwrite(t, sizeof(STU), 5, fp);
    fclose(fp);
    printf("\n\nThe original data :\n\n");
    for (j=0; j<N; j++)
    {  
        printf("\nNo: %ld  Name: %-8s      Scores:  ",t[j].sno, t[j].name);
        for (i=0; i<3; i++)  
            printf("%6.2f ", t[j].score[i]);
        printf("\n");
    }
    fun("student.dat");
    printf("\n\nThe data after sorting :\n\n");
    fp = fopen("student.dat", "rb");
    fread(ss, sizeof(STU), 5, fp);
    fclose(fp);
    for (j=0; j<N; j++)
    {  
        printf("\nNo: %ld  Name: %-8s      Scores:  ",ss[j].sno, ss[j].name);
        for (i=0; i<3; i++)  
            printf("%6.2f ", ss[j].score[i]);
        printf("\n");
    }
    return 0;
}

2.程序修改题

给定程序中,函数fun的功能是:依次取出字符串中所有数字字符,形成新的字符串,并取代原字符串。

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

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

#include <stdio.h>
void  fun(char *s)
{  
    int  i,j;
    for(i=0,j=0; s[i]!='\0'; i++)
        if(s[i]>='0' && s[i]<='9')
    /**********found**********/
            s[j]=s[i];
    /**********found**********/
        s[j]="\0";
}
int main()
{  
    char  item[80];
    printf("\nEnter a string  :  ");
    gets(item);
    printf("\nThe  string  is  : \"%s\"\n",item);
    fun(item);
    printf("\nThe string of changing is :\"%s\"\n",item);
    return 0;
}

3.程序设计题

编写函数fun,它的功能是:将M行N列的二维数组中的字符数据,按列的顺序依次放到一个字符串中。

例如,二维数组中的数据为:

W W W W

S S S S

H H H H

则字符串中的内容应为:WSHWSHWSHWSH。

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

#include <stdio.h>
#define  M  3
#define  N  4
void NONO(void);
void  fun(char (*s)[N], char *b)
{

}
int main()
{
    char  a[100],w[M][N]={{'W','W','W','W'},
        {'S','S','S','S'},{'H','H','H','H'}};
    int  i,j;
    printf("The matrix:\n");
    for(i=0; i<M; i++)
    {
        for(j=0;j<N; j++)
            printf("%3c",w[i][j]);
        printf("\n");
    }
    fun(w,a);
    printf("The A string:\n");puts(a);
    printf("\n\n");
    NONO();
    return 0;
}
void NONO(void)
{/* 请在此函数内打开文件,输入测试数据,调用 fun 函数,输出数据,关闭文件。 */
    FILE *rf, *wf ;
    int i,j,k ;
    char a[100],w[M][N], b ;
    rf = fopen("in.dat","r") ;
    wf = fopen("out.dat","w") ;
    for(k = 0 ; k < 10 ; k++)
    {
       for(i = 0 ; i < M ; i++)
       {
           for(j = 0 ; j < N ; j++)
              fscanf(rf, "%c", &w[i][j]) ;
           fscanf(rf, "%c", &b) ;
       }
       fun(w, a) ;
       fprintf(wf, "%s\n", a) ;
    }
    fclose(rf) ;
    fclose(wf) ;
}
1.(1"rb"2)>    (3)fwrite
2. s[j++]=s[i];
    s[j]='\0';
3void  fun(char (*s)[N], char *b)
    {
          int i,j,k=0;
          for (j=0;j<N;j++)
              for (i=0;i<M;i++)
                  b[k++]=s[i][j];
          b[k]='\0';
    }
第68套参考答案