二级c语言第七套上机练习.docVIP

  • 11
  • 0
  • 约2.23千字
  • 约 5页
  • 2018-06-26 发布于河南
  • 举报
二级c语言第七套上机练习

1 给定程序中,函数fun的功能是:将形参指针所指结构体数组中的3个元素,按num成员进行升序排序。 #include stdio.h typedef struct { int num; char name[10]; }PERSON; /**********found**********/ void fun(PERSON ___std[]___) { /**********found**********/ ___PERSON_ temp; if(std[0].numstd[1].num) { temp=std[0]; std[0]=std[1]; std[1]=temp; } if(std[0].numstd[2].num) { temp=std[0]; std[0]=std[2]; std[2]=temp; } if(std[1].numstd[2].num) { temp=std[1]; std[1]=std[2]; std[2]=temp; } } main() { PERSON std[ ]={ 5,Zhanghu,2,WangLi,6,LinMin }; int i; /**********found**********/ fun(___std___); printf(\nThe result is :\n); for(i=0; i3; i++) printf(%d,%s\n,std[i].num,std[i].name); } 2 给定程序中,函数fun的功能是:通过某种方式实现两个变量值的交换,规定不允许增加语句和表达式,例如:变量a中的值为3,b中的值为8,程序运行后,a中的值为8,b中的值为3。 #include conio.h #include stdio.h #include windows.h int fun(int *x,int y) { int t; /************found************/ t=*x;*x=y; /*原来是t=x; x=y*/ /************found************/ return(t); /*原来是return (y)*/ } main() { int a=3,b=8; system(cls); printf(%d %d\n,a,b); b=fun(a,b); printf(%d %d\n,a,b); } 3 学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中 ,请编写函数fun它的功能是:把高于等于平均分的学生数据放在h所指的数组中,高于等于平均分的学生数据放在h所指的数组中,高于等于平均分的学生人数通过形参放在传回,平均分通过函数值传回 #include stdio.h #define N 12 typedef struct { char num[10]; double s; } STREC; double fun(STREC *a,STREC *b,int *n) { int i, j ; double aver=0; *n=0; for(i=0 ; iN; i++) aver=aver+a[i].s; /*求和*/ aver=aver/N; /*计算平均分*/ for(i=0;iN; i++) if(a[i].s=aver) { b[*n].s=a[i].s; /*将高于等于平均分的学生放在另一个数组b中*/ for(j=0 ; j10; j++) b[*n].num[j]=a[i].num[j]; *n=*n+1;/*统计高于等于平均分的学生人数*/ } return (aver) /*返回平均分*/ } 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];FILE *out; int i,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;in;i++) printf(%s %4.1f\n,h[i].num,h[i].s); p

文档评论(0)

1亿VIP精品文档

相关文档