一.程序填空题

1.给定程序中,通过定义并赋初值的方式,利用结构体变量存储了一名学生的信息。函数fun的功能是:输出这位学生的信息。

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

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

#include  <stdio.h>

typedef  struct

    int  num;

    char  name[9];

    char  sex;

    struct { int year,month,day;} birthday;

    float  score[3];

}STU;

/**********found**********/

void show(STU  ___1___)

    int  i;

    printf(“\n%d %s %c %d-%d-%d”,tt.num,

       tt.name,tt.sex,tt.birthday.year,

       tt.birthday.month,tt.birthday.day);

    for(i=0; i<3; i++)

    /**********found**********/

       printf(“%5.1f”, ___2___);

    printf(“\n”);

}

int main( )

    STU std={1,”Zhanghua”,’M’,1961,

             10,8,76.5,78.0,82.0};

    printf(“\nA student data:\n”);

    /**********found**********/

    show(___3___);

    return 0;

}

2.人员的记录由编号和出生年、月、日组成,N名人员的数据已在主函数中存入结构体数组std中。函数fun的功能是:找出指定出生年份的人员,将其数据放在形参k所指的数组中,由主函数输出,同时由函数值返回满足指定条件的人数。

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

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

#include    <stdio.h>

#define    N    8

typedef  struct

    int  num;

    int  year,month,day ;

}STU;

int fun(STU  *std, STU  *k, int  year)

    int  i,n=0;

    for (i=0; i<N; i++)

    /**********found**********/

      if(  ___1___==year)

    /**********found**********/

          k[n++]= ___2___;

    /**********found**********/

    return (___3___);

}

int main()

    STU  std[N]={ {1,1984,2,15},{2,1983,9,21}, {3,1984,9,1},{4,1983,7,15},

                  {5,1985,9,28},{6,1982,11,15},{7,1982,6,22},{8,1984,8,19}};

    STU  k[N];        

    int  i,n,year;

    printf(“Enter a year :  “); 

    scanf(“%d”,&year);

    n=fun(std,k,year);

    if(n==0)

        printf(“\nNo person was born in %d \n”,year);

    else

    {  

        printf(“\nThese persons were born in %d \n”,year);

        for(i=0; i<n; i++)

         printf(“%d %d-%d-%d\n”,k[i].num,k[i].year,

                k[i].month,k[i].day);

    }

    return 0;

}

3.给定程序中,人员的记录由编号和出生年、月、日组成,N名人员的数据已在主函数中存入结构体数组std中,且编号唯一。函数fun的功能是:找出指定编号人员的数据,作为函数值返回;若指定编号不存在,返回数据中的编号为空串。

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

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

#include <stdio.h>

#include <string.h>

#define  N  8

typedef  struct

  char  num[10];

  int  year,month,day ;

}STU;

/**********found**********/

___1___ fun(STU  *std, char  *num)

  int  i;      

  STU  a={“”,9999,99,99};

  for (i=0; i<N; i++)

  /**********found**********/

    if (strcmp(___2___,num)==0)

  /**********found**********/

       return (___3___);

  return  a;

}

int main()

  STU std[N]={{“111”,1984,2,15},{“222”,1983,9,21},

     {“333”,1984,9,1},{“444”,1983,7,15},

     {“555”,1984,9,28},{“666”,1983,11,15},

     {“777”,1983,6,22},{“888”,1984,8,19}};

  STU  p;        

  char  n[10]=”666″;

  p=fun(std,n);

  if(p.num[0]==0)

       printf(“\nNot found !\n”);

  else

  {  

    printf(“\nSucceed !\n  “);

    printf(“%s %d-%d-%d\n”,p.num,p.year,p.month,p.day);

  }

  return 0;

}

4.给定程序中,通过定义学生结构体变量,存储了学生的学号、姓名和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;

}

5.给定程序中,通过定义学生结构体变量,存储了学生的学号、姓名和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;

}

6.给定程序中,通过定义学生结构体数组,存储了若干名学生的学号、姓名和3门课的成绩。函数fun的功能是:将存放学生数据的结构体数组,按照姓名的字典序(从小到大)排序。

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

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

#include  <stdio.h>

#include  <string.h>

struct student

{

    long  sno;

    char  name[10];

    float  score[3];

};

void fun(struct student  a[], int  n)

{

    /**********found**********/

    __1__ t;

    int  i, j;

    /**********found**********/

    for (i=0; i<__2__; i++)

       for (j=i+1; j<n; j++)

    /**********found**********/

         if (strcmp(__3__) > 0)

         { t=a[i];  a[i]=a[j]; a[j]=t; }

}

int main()

{

    struct student s[4]={{10001,”ZhangSan”,95,80,88},

                    {10002,”LiSi”, 85, 70, 78},

                    {10003,”CaoKai”, 75, 60, 88},

                    {10004,”FangFang”, 90, 82, 87}};

    int  i, j;

    printf(“\n\nThe original data :\n\n”);

    for (j=0; j<4; j++)

    { 

        printf(“\nNo: %ld  Name: %-8s      Scores:  “,s[j].sno, s[j].name);

        for (i=0; i<3; i++) 

            printf(“%6.2f “, s[j].score[i]);

        printf(“\n”);

    }

    fun(s, 4);

    printf(“\n\nThe data after sorting :\n\n”);

    for (j=0; j<4; j++)

    { 

        printf(“\nNo: %ld  Name: %-8s      Scores:  “,s[j].sno, s[j].name);

        for (i=0; i<3; i++) 

            printf(“%6.2f “, s[j].score[i]);

        printf(“\n”);

    }

    return 0;

}

7.给定程序中,函数fun的功能是:将形参指针所指结构体数组中的三个元素按num成员进行升序排列。

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

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

#include <stdio.h>

typedef  struct

{  int  num;

   char  name[10];

}PERSON;

/**********found**********/

void fun(PERSON  ___1___)

{

   /**********found**********/

   ___2___  temp;

   if(std[0].num>std[1].num)

   { temp=std[0]; std[0]=std[1]; std[1]=temp;}

   if(std[0].num>std[2].num)

   { temp=std[0]; std[0]=std[2]; std[2]=temp;}

   if(std[1].num>std[2].num)

   { temp=std[1]; std[1]=std[2]; std[2]=temp;}

}

int main()

   PERSON std[ ]={5,”Zhanghu”,2,”WangLi”,6,”LinMin”};

   int  i;

   /**********found**********/

   fun(___3___);

   printf(“\nThe result is :\n”);

   for(i=0; i<3; i++)

      printf(“%d,%s\n”,std[i].num,std[i].name);

   return 0;

}

8.给定程序中,函数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;

}

9.给定程序通过定义并赋初值的方式,利用结构体变量存储了一名学生的学号、姓名和3门课的成绩。函数fun的功能是将该学生的各科成绩都乘以一个系数a。

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

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

#include    <stdio.h>

typedef  struct

    int  num;

    char  name[9];

    float  score[3];

}STU;

void show(STU  tt)

    int  i;

    printf(“%d  %s  :  “,tt.num,tt.name);

    for(i=0; i<3; i++)

      printf(“%5.1f”,tt.score[i]);

    printf(“\n”);

}

/**********found**********/

void modify(___1___  *ss,float  a)

    int  i;

    for(i=0; i<3; i++)

    /**********found**********/

       ss->___2___ *=a;

}

int main( )

    STU  std={ 1,”Zhanghua”,76.5,78.0,82.0 };

    float  a;

    printf(“\nThe original number and name and scores :\n”);

    show(std);

    printf(“\nInput a number :   “);  scanf(“%f”,&a);

    /**********found**********/

    modify(___3___,a);

    printf(“\nA result of modifying :\n”);

    show(std);

    return 0;

}

10.程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。函数fun的功能是:将形参a中的数据进行修改,把修改后的数据作为函数值返回主函数进行输出。

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

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

#include <stdio.h>

#include <string.h>

struct student {

     long  sno;

     char  name[10];

     float  score[3];

};

/**********found**********/

__1__ fun(struct  student  a)

{

     int  i;

     a.sno = 10002;

     /**********found**********/

     strcpy(__2__, “LiSi”);

     /**********found**********/

     for (i=0; i<3; i++) __3__+= 1;

     return  a;

}

int main()

{

     struct student  s={10001,”ZhangSan”, 95, 80, 88}, t;

     int  i;

     printf(“\n\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”);

     t = fun(s);

     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;

}


1.(1)tt       (2)tt.score[i]       (3)std
2.(1)std[i].year       (2)std[i]       (3)n
3.(1)STU       (2)std[i].num      (3)std[i]
4.(1)a       (2)b.name       (3)score[i]
5.(1)->sno      (2)->name      (3)&t
6.(1struct student      (2)n-13)a[i].name,a[j].name
7.(1)std[]      (2)PERSON       (3)std
8.(1)std[0]      (2)std[i].age      (3)max.name
9.(1)STU      (2)score[i]      (3)&std
10.(1struct student     (2)a.name      (3)a.score[i]

程序填空题参考答案

二.程序设计题

1.某学生的记录由学号、8门课程成绩和平均分组成,学号和8门课程成绩已在主函数中给出。编写函数fun,它的功能是:求出该学生的平均分放在记录的ave成员中。

例如,学生的成绩为:85.4,76,69.5,85,91,72,64.5,87.5,平均分应为:78.875。

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

#include <stdio.h>

#define N 8

typedef struct

{

    char num[10];

    double s[N];

    double ave;

}STREC;

void NONO(void);

void fun(STREC *p)

{

}

int main()

{

    STREC s={“GA005”,85.5,76,69.5,85,91,72,64.5,87.5};

    int i;

    fun(&s);

    printf(“The %s’s student data:\n”,s.num);

    for(i=0;i<N;i++)

       printf(“%4.1f\n”,s.s[i]);

    printf(“\nave=%7.3f\n”,s.ave);

    NONO();

    return 0;

}

void NONO(void)

{/* 请在此函数内打开文件,输入测试数据,调用 fun 函数,输出数据,关闭文件。 */

    STREC s={“GA005”,85.5,76,69.5,85,91,72,64.5,87.5};

    int i;

    FILE  *wf;

    wf = fopen(“out.dat”, “w”) ;

    fun(&s);

    fprintf(wf,”The %s’s student data:\n”,s.num);

    for(i=0;i<N;i++)

       fprintf(wf,”%4.1f\n”,s.s[i]);

    fprintf(wf,”ave=%7.3f\n”,s.ave);

    fclose(wf) ;

}


void fun(STREC *p)
{
    int i;
    double sum=0.0;
    for (i=0;i<N;i++)
        sum+=p->s[i];
    p->ave=sum/N;
}

参考程序

2.编写函数fun,其功能是:把a所指的结构体数组中分数最低的学生数据放在h所指的数组中(注意:分数最低的学生可能不止1个),函数返回分数最低的学生的人数。

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

#include <stdio.h>

#define N 16

typedef struct

{

    char num[10];

    int s;

}STREC;

int fun(STREC *a,STREC *b)

{

}

int main()

{

    STREC s[N]={{“GA05”,85},{“GA03”,76},

         {“GA02”,69},{“GA04”,85},{“GA01”,91},

         {“GA07”,72},{“GA08”,64},{“GA06”,87},

        {“GA015”,85},{“GA013”,91},{“GA012”,64},

        {“GA014”,91},{“GA011”,91},{“GA017”,64},

        {“GA018”,64},{“GA016”,72}};

    STREC h[N];

    int i,n;FILE *out;

    n=fun(s,h);

    printf(“The %d lowest score:\n”,n);

    for(i=0;i<n;i++)

      printf(“%s %4d\n”,h[i].num,h[i].s);

    printf(“\n”);

    out=fopen(“out.dat”,”w”);

    fprintf(out,”%d\n”,n);

    for (i=0;i<n;i++)

      fprintf(out,”%s %4d\n”,h[i].num,h[i].s);

    fclose(out);

}


int fun(STREC *a,STREC *b)
{
    int i,n=0,min;
    min=a[0].s;
    for (i=1;i<N;i++)
        if (min>a[i].s) min=a[i].s;
    for (i=0;i<N;i++)
        if (min==a[i].s) b[n++]=a[i];
    return n;
}

参考程序

3.已知学生的记录由学号和学习成绩构成,N名学生的数据已存入结构体数组a中。编写函数fun,它的功能是:找出成绩最低的学生记录,并通过形参返回主函数(规定只有一个最低分)。

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

#include <stdio.h>

#include <string.h>

#define  N  10

void NONO(void);

typedef  struct  ss

{

    char  num[10];

    int  s;

} STU;

void fun(STU a[], STU  *s)

{

}

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} }, m  ;

    int   i;

    printf(“***** The original data *****\n”);

    for ( i=0; i< N; i++ )

        printf(“No=%s  Mark=%d\n”, a[i].num,a[i].s);

    fun ( a, &m );

    printf (“***** THE  RESULT *****\n”);

    printf (“The lowest  :  %s , %d\n”,m.num, m.s);

    NONO( );

    return 0;

}

void NONO(void)

{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */

    FILE *rf, *wf ;

    STU a[N], m ;

    int i ;

    rf = fopen(“in.dat”,”r”) ;

    wf = fopen(“out.dat”,”w”) ;

    for(i = 0 ; i < 10; i++)

        fscanf(rf, “%s %d”, a[i].num, &a[i].s) ;

    fun(a, &m) ;

    fprintf (wf, “The lowest : %s, %d\n”, m.num, m.s) ;

    fclose(rf) ;

    fclose(wf) ;

}


void fun(STU a[], STU  *s)
{
    STU min;
    int i;
    min=a[0];
    for (i=1;i<N;i++)
        if (min.s>a[i].s) min=a[i];
    *s=min;
}

参考程序

4.已知学生的记录由学号和学习成绩构成,N名学生的数据已存入结构体数组a中。编写函数fun,它的功能是:找出成绩最高的学生记录,通过形参返回(规定只有一个最高分)。

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

#include <stdio.h>

#include <string.h>

#define  N  10

typedef  struct  ss

{

    char  num[10];

    int  s;

} STU;

void NONO(void);

void fun(STU  a[], STU  *s)

{

}

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} }, m  ;

    int   i;

    printf(“***** The original data *****\n”);

    for ( i=0; i< N; i++ )

        printf(“No = %s  Mark = %d\n”, a[i].num,a[i].s);

    fun ( a, &m );

    printf (“***** THE  RESULT *****\n”);

    printf (“The top  :  %s , %d\n”,m.num, m.s);

    NONO( );

    return 0;

}

void NONO(void)

{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */

    FILE *rf, *wf ;

    STU a[N], m ;

    int i ;

    rf = fopen(“in.dat”,”r”) ;

    wf = fopen(“out.dat”,”w”) ;

    for(i = 0 ; i < 10; i++)

       fscanf(rf, “%s %d”, a[i].num, &a[i].s) ;

    fun(a, &m) ;

    fprintf (wf, “The top : %s, %d\n”, m.num, m.s) ;

    fclose(rf) ;

    fclose(wf) ;

}


void fun(STU  a[], STU  *s)
{
    int i;
    *s=a[0];
    for (i=1;i<N;i++)
        if (s->s<a[i].s)
           *s=a[i];
}

参考程序

5.学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中。编写函数fun,它的功能是:按分数的高低排列学生的记录,高分在前。

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

#include <stdio.h>

#define  N  16

typedef  struct

{

    char  num[10];

    int   s;

} STREC;

void fun(STREC  a[])

{

}

int main()

{

    STREC  s[N]={{“GA005”,85},{“GA003”,76},

        {“GA002”,69},{“GA004”,85},{“GA001”,91},

             {“GA007”,72},{“GA008”,64},{“GA006”,87},

             {“GA015”,85},{“GA013”,91},{“GA012”,64},

             {“GA014”,91},{“GA011”,66},{“GA017”,64},

             {“GA018”,64},{“GA016”,72}};

    int  i;

    FILE *out ;

    fun(s);

    printf(“The data after sorted :\n”);

    for(i=0;i<N; i++)

    {

        if( (i)%4==0 )printf(“\n”);

        printf(“%s  %4d  “,s[i].num,s[i].s);

    }

    printf(“\n”);

    out = fopen(“out.dat”,”w”) ;

    for(i=0;i<N; i++)

    {

        if( (i)%4==0 && i) fprintf(out, “\n”);

        fprintf(out, “%4d  “,s[i].s);

    }

    fprintf(out,”\n”);

    fclose(out) ;

    return 0;

}


void fun(STREC  a[])
{
    int i,j,k;
    STREC t;
    for (i=0;i<N-1;i++)
    {
        k=i;
        for (j=i+1;j<N;j++)
            if (a[k].s<a[j].s) k=j;
        t=a[i]; a[i]=a[k]; a[k]=t;
    }
}

参考程序

6.学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中。编写函数fun,它的功能是:把指定分数范围内的学生数据放在h所指的数组中,分数范围内的学生人数由函数值返回。

例如,输入的分数为60 69,则应当把分数在60到69的学生数据输出(包含60分和69分的学生数据)。

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

#include <stdio.h>

#define N 16

typedef struct

{

    char num[10];

    int s;

} STREC;

int fun(STREC *a,STREC *b,int l,int h)

{

}

int main()

{

    STREC s[N]={{“GA05”,85},{“GA03”,76},{“GA02”,69},

             {“GA04”,85},{“GA01”,91},{“GA07”,72},

             {“GA08”,64},{“GA06”,87},{“GA015”,85},

             {“GA013”,91},{“GA012”,64},{“GA014”,91},

             {“GA011”,91},{“GA017”,64},{“GA018”,64},

             {“GA016”,72}};

    STREC h[N];FILE *out;

    int i,n,low,heigh,t;

    printf(“Enter 2 interger number low & heigh:”);

    scanf(“%d%d”,&low,&heigh);

    if (heigh<low) {t=heigh;heigh=low;low=t;}

    n=fun(s,h,low,heigh);

    printf(“The student’s data between %d–%d:\n”,low,heigh);

    for(i=0;i<n;i++)

       printf(“%s  %4d\n”,h[i].num,h[i].s);

    printf(“\n”);

    out=fopen(“out.dat”,”w”);

    n=fun(s,h,80,98);

    fprintf(out,”%d\n”,n);

    for(i=0;i<n;i++)

       fprintf(out,”%s  %4d\n”,h[i].num,h[i].s);

    fclose(out);

    return 0;

}


int fun(STREC *a,STREC *b,int l,int h)
{
    int i,j=0;
    for (i=0;i<N;i++)
        if (a[i].s>=l && a[i].s<=h)
           b[j++]=a[i];
    return j;
}

参考程序

7.学生的记录由学号和成绩组成,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;

}


double 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;
}

参考程序

8.学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中。编写函数fun,它的功能是:函数返回指定学号的学生数据,指定的学号在主函数中输入。若没找到指定学号,在结构体变量中给学号置空串,给成绩置-1,作为函数值返回。

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

#include <stdio.h>

#include <string.h>

#define   N   16

typedef  struct

{

    char  num[10];

    int   s;

} STREC;

STREC  fun(STREC  *a, char *b)

{

}

int main()

{

    STREC  s[N]={{“GA005”,85},{“GA003”,76},

        {“GA002”,69},{“GA004”,85},{“GA001”,91},

             {“GA007”,72},{“GA008”,64},{“GA006”,87},

             {“GA015”,85},{“GA013”,91},{“GA012”,64},

             {“GA014”,91},{“GA011”,77},{“GA017”,64},

             {“GA018”,64},{“GA016”,72}};

    STREC  h;

    char  m[10];

    int  i;FILE *out ;

    printf(“The original data:\n”);

    for(i=0; i<N; i++)

    {

        if(i%4==0) printf(“\n”);

        printf(“%s %3d  “,s[i].num,s[i].s);

    }

    printf(“\n\nEnter the number:  “);

    gets(m);

    h=fun( s,m );

    printf(“The data :  “);

    printf(“\n%s  %4d\n”,h.num,h.s);

    printf(“\n”);

    out = fopen(“out.dat”,”w”) ;

    h=fun(s,”GA013″);

    fprintf(out,”%s  %4d\n”,h.num,h.s);

    fclose(out);

    return 0;

}


STREC  fun(STREC  *a, char *b)
{
    STREC t;
    int i;
    t.num[0]='\0';
    t.s=-1;
    for (i=0;i<N;i++)
        if (strcmp(a[i].num,b)==0)
        {
           t=a[i];  break;
        }
    return t;
}

参考程序

原文地址:http://www.cnblogs.com/cs-whut/p/16887880.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性