计分系统
2012-05-16 10:09:02 来源:WEB开发网核心提示:#include "stdio.h"#include "conio.h"#include "stdlib.h"#define SIZE_1 10//#define debugFILE *fp;struct stu_infor{int num;char name
#include "stdio.h" #include "conio.h" #include "stdlib.h" #define SIZE_1 10 //#define debug FILE *fp; struct stu_infor { int num; char name[10]; float score[7]; float max; float min; float aver; int rack; }stu[SIZE_1]; void init() //建立文件并初始化(记录学生姓名并将数据归零) { printf("未建立文件,请初始化……\n\n"); if((fp=fopen("student_information","wb"))==NULL) //建立文件并打开 只写 { printf("error!!can't open the file\n"); return; } printf("现在请输入学生姓名:\n"); for(int i=0;i<SIZE_1;i++) { printf("请输入%3d 号选手姓名:",i+1); scanf("%s",stu[i].name); stu[i].max=0;stu[i].min=0;stu[i].aver=0;stu[i].rack=0;stu[i].num=i; for(int j=0;j<7;j++) stu[i].score[j]=0; fwrite(&stu[i],sizeof(struct stu_infor),1,fp); } fclose(fp); } void load()//读入数据 { if((fp=fopen("student_information","r"))==NULL) { printf("error!!can't open the file\n"); return; } for(int i=0;i<SIZE_1;i++) { fread(&stu[i],sizeof(struct stu_infor),1,fp); } #ifdef debug for(i=0;i<SIZE_1;i++) printf("%s %f %f %f %d \n",stu[i].name,stu[i].max,stu[i].min,stu[i].aver,stu[i].num); #endif fclose(fp); } void score(int num,float score[])//分数从小到大排序并储存 { float t; if((fp=fopen("student_information","wb"))==NULL) //建立文件并打开 只写 { printf("error!!can't open the file\n"); return; } for(int i=0;i<SIZE_1;i++) { fread(&stu[i],sizeof(struct stu_infor),1,fp); } for(int j=0;j<6;j++) for(int i=0;i<6-j;i++) if(score[i]>score[i+1]) { t=score[i]; score[i]=score[i+1]; score[i+1]=t; } stu[num].max=score[6];stu[num].min=score[0]; for(i=0;i<7;i++) { stu[num].score[i]=score[i]; } for(i=1;i<6;i++) { stu[num].aver=stu[num].aver+score[i]; } stu[num].aver=stu[num].aver/5; for(i=0;i<=num;i++) fwrite(&stu[i],sizeof(struct stu_infor),1,fp); fclose(fp); } void put_in()//输入分数 { int num; printf("\n先输入选手编号,再输入分数。当编号为0是结束录入。\n"); while(1) { printf("请输入选手编号:"); A: scanf("%d",&num); if(num>SIZE_1||num<0) { printf("不存在此选手,请重新输入!\n"); goto A; } if(num==0) break; num--; printf("请输入分数(每输入一个分数后回车结束)\n"); for(int i=0;i<7;i++) scanf("%f",&stu[num].score[i]); score(num,stu[num].score); printf("%3d号选手%s的最高分为%3.2f,最低分为%3.2f,平均分为%3.2f\n\n",num+1,stu[num].name,stu[num].max,stu[num].min,stu[num].aver); } printf("结束输入!\n"); } void stu_rank()//选手排名并储存 { int rank[SIZE_1]; float score[SIZE_1]; float t; int tt; struct stu_infor linshi; load(); if((fp=fopen("student_information","wb"))==NULL) //建立文件并打开 只写 { printf("error!!can't open the file\n"); return; } for(int i=0;i<SIZE_1;i++) { rank[i]=i; score[i]=stu[i].aver; } for(int j=0;j<SIZE_1-1;j++) for(i=0;i<SIZE_1-1-j;i++) if(score[i]<score[i+1]) { t=score[i];score[i]=score[i+1];score[i+1]=t; tt=rank[i];rank[i]=rank[i+1];rank[i+1]=tt; } for(i=0;i<SIZE_1;i++) stu[rank[i]].rack=i+1; for(j=0;j<SIZE_1-1;j++) for(i=0;i<SIZE_1-1-j;i++) if(stu[i].rack>stu[i+1].rack) { linshi=stu[i]; stu[i]=stu[i+1]; stu[i+1]=linshi; } for(i=0;i<SIZE_1;i++) fwrite(&stu[i],sizeof(struct stu_infor),1,fp); fclose(fp); } void gonggao() { load(); printf("\n\n姓名\t平均分\t名次\t所有得分\n"); for(int i=0;i<SIZE_1;i++) printf("%s\t%3.2f\t%d\t%3.2f\t%3.2f\t%3.2f\t%3.2f\t%3.2f\t%3.2f\t%3.2f\n", stu[i].name,stu[i].aver,stu[i].rack, stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].score[3],stu[i].score[4],stu[i].score[5],stu[i].score[6]); printf("\n\n"); } void gonggao_win() { load(); for(int i=0;i<SIZE_1;i++) printf("第%2d名是%2d号选手 %s ,平均分为%3.2f,去掉的最高分为%3.2f,去掉的最低分为%3.2f\n", i+1,stu[i].num+1,stu[i].name,stu[i].aver,stu[i].max,stu[i].min); printf("\n\n"); } void main() { char b; printf("\n"); printf("**************************************\n"); printf("************中华颂计分系统************\n"); printf("********铁龙机器人实验室 岳大禹*******\n"); printf("**************************************\n"); if((fp=fopen("student_information","r"))==NULL) { init(); printf("储存完毕!!\n\n"); } load(); printf("\t欢迎进入系统!\n"); B: printf("\n"); printf("按q键直接退出\n"); printf("按t键重新录入名单\n"); printf("按f键查看所有结果\n"); printf("按a键进入计分程序\n"); b=getch(); if(b=='t') { init(); printf("储存完毕,请按任意键重新选择\n"); getch(); goto B; } if(b=='q') { exit(0); } if(b=='f') { gonggao(); printf("按任意键重新选择\n"); getch(); goto B; } if(b=='a') { put_in(); stu_rank(); gonggao_win(); printf("按任意键重新选择\n"); getch(); goto B; } else { printf("不存在的选择,请重新选择!\n"); goto B; } }
- ››计分系统
更多精彩
赞助商链接