c语言 结构体和共用体 1. 有5个学生,每个学生的数据包括学号、姓名、3门课的成绩,从键盘输入5个学生数据

1. 有5个学生,每个学生的数据包括学号、姓名、3门课的成绩,从键盘输入5个学生数据,要求:
1) 使用input函数输入5个学生的数据:学号、姓名、3门课的成绩;
2) 使用average函数求每个学生的平均成绩和所有学生的总平均成绩;
3) 使用max函数找出平均分最高的学生数据;
4) 使用output函数输出
全体学生的数据:学号、姓名、3门课的成绩、平均成绩;
总平均分以及平均分最高的学生的数据:学号、姓名、3门课的成绩、平均分数。

#include<stdio.h>
#define max 5 /*设定要输入成绩的学生个数*/

double zpj; /*总平均值*/
struct student /*结构体*/
{
int num;
char name[10];
int score1;
int score2;
int score3;
double pj;
};
struct student stu[max],temp;

void Input() /*输入函数*/
{ int i;
for(i=0;i<max;i++)
{printf("enter number");

scanf("%d",&stu[i].num);

printf("enter name");

scanf("%s",&stu[i].name);

printf("enter score1");

scanf("%d",&stu[i].score1);

printf("enter score2");

scanf("%d",&stu[i].score2);

printf("enter score3");

scanf("%d",&stu[i].score3);
}
}

average() /*求平均值函数*/

{int i;
for(i=0;i<5;i++)
{stu[i].pj=stu[i].score1+stu[i].score1+stu[i].score3;
zpj+=stu[i].pj;
}
for(i=0;i<5;i++)
stu[i].pj/=3;
zpj/=max;
}

MAX() /*找出最高平均值的学生的函数*/
{int i,j;
temp=stu[0];
for(i=0;i<max-1;i++)
for(j=i+1;j<max;j++)
if(stu[i].pj<stu[j].pj)
temp=stu[j];
}

output() /*输出函数*/

{int i;
for(i=0;i<max;i++)
printf("num=%d\n name=%s\n score1=%d\n score2=%d\n score3=%d\n aver=%f\n",
stu[i].num,stu[i].name,stu[i].score1,stu[i].score2,stu[i].score3,stu[i].pj);

printf("总平均分=%f\n\n",zpj);

printf("平均分最高的学生\nnum=%d\n name=%s\n score1=%d\n score2=%d\n score3=%d\n aver=%f\n",
temp.num,temp.name,temp.score1,temp.score2,temp.score3,temp.pj);

}
void main() /*主函数*/
{

Input();
average();
MAX();
output();
}

自己写的 符合你的要求
不足之处还请指教
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-06-17
#include<stdlib.h>
#include<stdio.h>
#define N 3
struct student
{int number;
double score;}a[N]={{20101,96.7},{20102,85.6},{20103,99.5}};
void main()
{
void fun(struct student *b);
struct student *p;
printf("The number and score\n");
for(p=a;p<a+3;p++)
{printf("%d,%lf\n",p->number,p->score);}
fun(a);
}
void fun(struct student *b)
{
FILE *fp;
int j,i;
struct student *p;
struct student t;
printf("The large-low score\n");
for(j=0;j<2;j++)
{ for(i=0;i<2-j;i++)
if(b[i].score<b[i+1].score)
{t=b[i];
b[i]=b[i+1];
b[i+1]=t;}}
for(p=b;p<b+3;p++)
{printf("%d,%lf\n",p->number,p->score);}
if((fp=fopen("filename","w"))==NULL)
{printf("cannot open file\n");
return;
}
for (j=0; j<N; j++)
{
if(fwrite(&a[j], sizeof(struct student),1, fp)!=1)
{ printf("file write error\n");
return;}
fputs("\n",fp);}
fclose(fp);
}
改一改就行,仅作个参考
第2个回答  2009-06-16
个人
相似回答