#include "Stdio.h"
#include "Conio.h"
#define N 3
struct stu{
int num;
char name[20];
char sex;
int age;
float score[4];
float avgscore;
};
void input(struct stu *pt,int m)
{ int i;
for(i=0;i<m;i++)
{
printf("please enter student's message:\n");
printf("enter the number:\n");
scanf("%d",&(pt+i)->num);
printf("\n enter the name:\n ");
scanf("%s",&(pt+i)->name);
printf("\n enter the age:\n");
scanf("%d",&(pt+i)->age);
printf("\n enter the first score:\n");
scanf("%f",&(pt+i)->score[0]);
printf("\n enter the second score:\n");
scanf("%f",&(pt+i)->score[1]);
printf("\n enter the third score:\n");
scanf("%f",&(pt+i)->score[2]);
printf("\n enter the forth score:\n");
scanf("%f",&(pt+i)->score[3]);
}
}
void output(struct stu *ps,int n)
{ struct stu *temp;
int i,j,k;
float avg;
k=0;
avg=0;
printf("\n num\tname\tage\tavgscore\t\n\n");
for(j=0;j<n;j++)
{ (ps+j)->avgscore=((ps+j)->score[0]+(ps+j)->score[1]+(ps+j)->score[2]+(ps+j)->score[3])/4;
if((ps+j)->avgscore<60)
k++;
avg+=(ps+j)->avgscore;
}
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{ if((ps+i)->avgscore<(ps+j)->avgscore)
{ *temp=*(ps+i);
*(ps+i)=*(ps+j);
*(ps+j)=*temp;
}
}
for(j=0;j<n;j++)
printf("%-d\t%-s\t%-d\t%-f\t\n",(ps+j)->num,(ps+j)->name,(ps+j)->age,(ps+j)->avgscore);
avg/=n;
printf("the avg score is:%f\n",avg);
printf("the unpassed student's nunmber is:%d\n",k);
}
main()
{ struct stu student[N];
input(student,N);
output(student,N);
getch();
}
一开始我没有在结构体里定义avgscore这个变量,而是在output里重新定义了一个avgscore[N]的数组,运行可以通过,可是定义了这个变量后,编译通过,但是输入数据时,输入第一个score数据,DOS框就自动退出...
不知道怎么回事?