满意答案
zxq1981n
2014.05.02
采纳率:45% 等级:12
已帮助:7619人
#include
#include
#include
#define N 3
typedef struct stu{
unsigned int num;
char name[20];
int math;
int english;
int computer;
float average;
}STUDENT;
int menu()
{
int choice;
clrscr();
gotoxy(10,20);
printf("\n****************************************************");
printf("\n* Class score process Program *");
printf("\n* *");
printf("\n* 1. Input everyone's score *");
printf("\n* 2. Calculate average of everyone *");
printf("\n* 3. Sort by average *");
printf("\n* 4. Output to file *");
printf("\n* 5. Exit *");
printf("\n****************************************************\n\n\n");
printf("\nPlease input your choice:");
scanf("%d",&choice);
return(choice);
}
void Input(STUDENT *p,int n)
{ int i;
printf("\nPlease input info of students:No Name Math English Computer\n");
for (i=0; i
printf("\n%d:",i+1);
scanf("%u%s%d%d%d",&((p+i)->num),(p+i)->name,&((p+i)->math),&((p+i)->english),&((p+i)->computer));
}
}
void Count(STUDENT *p,int n)
{ int i;
printf("\nAverage score of students:\n");
for (i=0; i
printf("\n%d:",i+1);
(p+i)->average=((p+i)->math+(p+i)->english+(p+i)->computer)/3.0;
printf("%.3f",(p+i)->average);
}
getch();
}
void Sort(STUDENT *p,int n)
{
int i,j;
STUDENT *temp;
for (i=0; i
for(j=i+1; j
if ((p+i)->average < (p+j)->average) {
*temp=*(p+i); *(p+i)=*(p+j);*(p+j)=*temp;
}
}
printf("\nResult of sort.");
for (i=0; i
printf("\n%-3d %s %-3d %-7d %-8d %-.2f",(p+i)->num,(p+i)->name,
(p+i)->math,(p+i)->english,(p+i)->computer,(p+i)->average);
getch();
}
int Output(STUDENT *p,int n)
{ int i;
FILE *fp;
printf("\nOut put to file...");
fp=fopen("d:\\cxh_tc\\sy5.dat","w");
if (fp==NULL) { printf("\nOpen file error.Exit!"); return(0); }
fprintf(fp,"%s"," Class Score Table \n\n");
fprintf(fp,"%s","Num Name Math English Computer Average 名次 \n");
for (i=0; i
fprintf(fp, "%-3d %s %-d %-d %-d %-.2f %d\n",(p+i)->num,(p+i)->name,
(p+i)->math,(p+i)->english,(p+i)->computer,(p+i)->average,i+1);
}
fclose(fp);
getch();
return 1;
}
void main()
{
STUDENT myclass[N],*p;
int flag=1,choice;
p=myclass;
while (flag) {
choice=menu();
clrscr();
switch(choice) {
case 1: Input(p,N); break;
case 2: Count(p,N); break;
case 3: Sort(p,N); break;
case 4: Output(p,N); break;
case 5:
default: flag=0; break;
}
}
}
00分享举报