要求:
能按总分,数学成绩,英语成绩,计算机成绩分别排序
能分别统计数学,英语,计算机中不及格人数
能按学号查找学生,并显示该学生信息。
分成了多个文件
/****************Score.dat************************/
学号 姓名 数学 英语 计算机
20050001 胡树伟 87 71 81
20050002 李冬 75 85 76
20050003 梁若婵 86 75 90
20050004 任栋 88 74 75
20050005 姜长宇 73 91 94
20050006 芦磊 86 91 97
20050007 陈昇 90 88 82
20050008 金谨 93 96 72
20050009 陈平 96 99 74
20050010 张赛 94 97 73
20050011 王澍 70 81 87
20050012 吴可 97 85 90
20050013 陈广辉 76 86 77
20050014 周倩 98 76 75
20050015 吕晨 82 82 98
20050016 陈世达 70 57 89
20050017 边竑晟 74 73 83
20050018 蔡亮 64 55 70
20050019 丛大成 50 57 95
20050020 方星钢 84 70 98
20050021 郭金 81 75 94
20050022 王婷婷 78 94 96
20050023 谢倩翼 97 96 86
20050024 柳裕 54 64 93
20050025 叶力 89 73 87
20050026 孙茂辉 81 75 91
20050027 许琳 83 50 76
20050028 王展鹏 64 77 87
20050029 梁唯飒 51 91 72
20050030 蒋杰 50 73 80
20050031 李莹 92 96 96
20050032 许栩 55 54 98
20050033 齐娜 97 57 72
20050034 郑勇 53 71 89
20050035 丁澜 97 90 63
20050036 李亮 83 83 63
20050037 李永亮 73 75 83
20050038 周秀芬 64 61 68
20050039 张豪 75 89 94
20050040 邓颖 78 63 56
20050041 雷相森 78 75 90
20050042 马文州 99 69 65
20050043 奚雍程 80 88 85
20050044 谢磊 92 51 78
20050045 郭宏涛 75 84 96
20050046 吴澍 93 95 84
20050047 薛峰 99 99 81
20050048 曾静 84 72 98
20050049 冯雷 70 76 68
20050050 程杰 73 89 72
/***************Score.h******************/
#ifndef _SCORE_H
#define _SCORE_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <process.h>
#define N 10
#define STUDENT_COUNT 50 //学生数
#define LESSON_COUNT 3 //课程数
#define SUM_SORT 9
#define MATH_SORT 0
#define ENGLISH_SORT 1
#define COMPUTER_SORT 2
#define SUM_STATISTICS 4
#define MATH_STATISTICS 5
#define ENGLISH_STATISTICS 6
#define COMPUTER_STATISTICS 7
#define SEARCH 8
typedef struct _Student
{
char id[N]; //学号
char name[N]; //姓名
int score[LESSON_COUNT]; //成绩
int sum; //总分
}Student;
void swap(Student *stu1, Student *stu2);
int cmp(char a[N], char b[N]);
void Sort(Student stu[] , int count , int lessonId);
int Search(Student stu[] , int count, char studentId[10], Student * student);
int Sum(int score[] , int count);
int Menu();
void ReadData(Student stu[] , int count);
#endif
/****************Main.c*********************/
#include "Score.h"
void main()
{
Student stu[STUDENT_COUNT];
Student s;
int menuitem;
int i ;
int k = 0;
char n[10]={'/0'};
//清屏
system("cls");
//读入学生信息
ReadData(stu, STUDENT_COUNT);
//计算学生总成绩
for (i=0; i<STUDENT_COUNT; i++)
stu[i].sum = Sum(stu[i].score,3);
while(1)
{
//显示功能菜单,并获得选择的菜单项
menuitem = Menu();
switch(menuitem)
{
//按总分排序
case SUM_SORT:
{
Sort(stu, STUDENT_COUNT, SUM_SORT);
printf ("Id/t/tName/tMath/tEnglish/tComputer Sum/n");
for (i=0; i<STUDENT_COUNT; i++)
printf ("%s/t%s/t%d/t%d/t%d/t %d/n",stu[i].id,stu[i].name ,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].sum);
getchar();
break;
}
//按数学成绩排序
case MATH_SORT:
{
Sort(stu, STUDENT_COUNT, MATH_SORT);
printf ("Id/t/tName/tMath/tEnglish/tComputer Sum/n");
for (i=0; i<STUDENT_COUNT; i++)
printf ("%s/t%s/t%d/t%d/t%d/t %d/n",stu[i].id,stu[i].name ,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].sum);
getchar();
break;
break;
}
//按英语成绩排序
case ENGLISH_SORT:
{
Sort(stu, STUDENT_COUNT, ENGLISH_SORT);
printf ("Id/t/tName/tMath/tEnglish/tComputer Sum/n");
for (i=0; i<STUDENT_COUNT; i++)
printf ("%s/t%s/t%d/t%d/t%d/t %d/n",stu[i].id,stu[i].name ,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].sum);
getchar();
break;
break;
}
//按计算机成绩排序
case COMPUTER_SORT:
{
Sort(stu, STUDENT_COUNT, COMPUTER_SORT);
printf ("Id/t/tName/tMath/tEnglish/tComputer Sum/n");
for (i=0; i<STUDENT_COUNT; i++)
printf ("%s/t%s/t%d/t%d/t%d/t %d/n",stu[i].id,stu[i].name ,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].sum);
getchar();
break;
break;
}
//统计数学成绩不及格人数
case MATH_STATISTICS:
{
k=0;
Sort(stu, STUDENT_COUNT, MATH_SORT);
for (i=0; i<STUDENT_COUNT; i++)
if (stu[i].score[0]<60)
break;
for (;i<STUDENT_COUNT; i++)
printf ("%d/t%s/t%s/t%d/n",k++,stu[i].id,stu[i].name,stu[i].score[0]);
getchar();
break;
}
//按英语成绩统计
case ENGLISH_STATISTICS:
{
k=0;
Sort(stu, STUDENT_COUNT, ENGLISH_SORT);
for (i=0; i<STUDENT_COUNT; i++)
if (stu[i].score[1]<60)
break;
for (;i<STUDENT_COUNT; i++)
printf ("%d/t%s/t%s/t%d/n",k++,stu[i].id ,stu[i].name,stu[i].score[1]);
getchar();
break;
}
//按计算机成绩统计
case COMPUTER_STATISTICS:
{
k=0;
Sort(stu, STUDENT_COUNT, COMPUTER_SORT);
for (i=0; i<STUDENT_COUNT; i++)
if (stu[i].score[2]<60)
break;
for (;i<STUDENT_COUNT; i++)
printf ("%d/t%s/t%s/t%d/n",k++,stu[i].id,stu[i].name,stu[i].score[2]);
getchar();
break;
}
//按学号搜索学生,并显示学生成绩
case SEARCH:
{
printf ("Please enter an ID:");
scanf ("%s",n);
if (Search(stu,STUDENT_COUNT,n,&s))
{
printf ("Id/t Name/tMath/tEnglish/tComputer Sum/n");
printf ("%s %s/t%d/t%d/t%d/t %d",s.id,s.name,s.score [0],s.score [1],s.score [2],s.sum);
getchar();
}
else
{
printf ("No such student! %s/n",n);
getchar();
}
break;
}
}
printf("/nPress AnyKey to Continue... ");
getchar();
}
}
/*****************Menu.c******************/
#include "Score.h"
//显示功能菜单,返回选择的菜单项
int Menu()
{
int menuitem ;
int item;
while(1)
{
menuitem = -1;
item = -1;
while ( menuitem != 0 && menuitem != 1 && menuitem != 2 && menuitem != 3 )
{
system("cls");
printf("/n/t/t Main Menu/n") ;
printf("/n/t/t1. Sort/n");
printf("/t/t2. Statistics/n");
printf("/t/t3. Search/n");
printf("/t/t0. Exit/n/n");
printf("/tPlease choose an item(0-3):");
scanf("%d",&menuitem);
if (menuitem == 0 || menuitem == -1) exit(0);
}
switch(menuitem)
{
case 1:
{
while(item != 0 && item != 1 && item != 2 && item != 3 && item != 4)
{
system("cls");
printf("/n/t/t Sort Menu/n");
printf("/n/t/t1. Sort by Math score/n");
printf("/t/t2. Sort by English score/n");
printf("/t/t3. Sort by Computer score/n");
printf("/t/t4. Sort by Sum score/n");
printf("/t/t0. return/n/n");
printf("/tPlease choose an item(0-4):");
scanf("%d",&item);
if (item == 0 || item == -1) break;
}
switch(item)
{
case 1:
return MATH_SORT;
case 2:
return ENGLISH_SORT;
case 3:
return COMPUTER_SORT;
case 4:
return SUM_SORT;
}
break;
}
case 2:
{
while(item != 0 && item != 1 && item != 2 && item != 3 && item != 4)
{
system("cls");
printf("/n/t/t Statitics Menu/n");
printf("/n/t/t1. Statitics by Math score/n");
printf("/t/t2. Statitics by English score/n");
printf("/t/t3. Statitics by Computer score/n");
printf("/t/t0. return/n/n");
printf("/tPlease choose an item(0-4):");
scanf("%d",&item);
if (item == 0) break;
}
switch(item)
{
case 1:
return MATH_STATISTICS;
case 2:
return ENGLISH_STATISTICS;
case 3:
return COMPUTER_STATISTICS;
case 4:
return SUM_STATISTICS;
}
break;
}
case 3:
{
return SEARCH;
}
}
}
}
/**************ReadData.c******************/
#include "Score.h"
void ReadData(Student stu[] , int count)
{
FILE *fp;
int i;
char ch[10];
if((fp = fopen("Score.dat","r")) == NULL)
{
printf("/tCan't open the file: Score.dat./n/tPress AnyKey to Exit... ");
getchar();
exit(0);
}
fscanf(fp,"%s%s%s%s%s",ch,ch,ch,ch,ch);
for(i = 0 ; i < count ; i++)
fscanf(fp,"%s %s%d%d%d",stu[i].id,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
}
/***************code.c****************/
#include "Score.h"
void swap(Student *stu1, Student *stu2)
{
int temp;
char s[N];
strcpy(s,(*stu1).id);
strcpy((*stu1).id , (*stu2).id);
strcpy((*stu2).id,s);
strcpy(s,(*stu1).name );
strcpy((*stu1).name,(*stu2).name );
strcpy((*stu2).name, s);
temp = (*stu1).score [0];
(*stu1).score [0] = (*stu2).score[0];
(*stu2).score[0] = temp;
temp = (*stu1).score [1];
(*stu1).score [1] = (*stu2).score[1];
(*stu2).score[1] = temp;
temp = (*stu1).score [2];
(*stu1).score [2] = (*stu2).score[2];
(*stu2).score[2] = temp;
temp = (*stu1).sum ;
(*stu1).sum = (*stu2).sum;
(*stu2).sum = temp;
}
int cmp(char a[N], char b[N])
{
int i = 0;
for (i=0; i<9; i++)
if (a[i]!=b[i])
return 0;
return 1;
}
/************************************************************************************************/
//Sort function
//功能:排序
//参数:Student stu[]
// int count:要排序的学生数目
// int lessonId:要排序的课程编号
// 数学:MATH_SORT; 英语:ENGLISH_SORT;计算机:COMPUTER_SORT;总分:SUM_SORT
/************************************************************************************************/
void Sort(Student stu[] , int count , int lessonId)
{
int i,k;
if (lessonId == SUM_SORT)//sort by sum
{
for (i=0; i<count-1; i++)
for (k = i+1; k<count; k++)
{
if (stu[i].sum < stu[k].sum )
{
swap(&stu[i],&stu[k]);//swap
}
}
}
else
{
for (i=0; i<count-1; i++)
for (k = i+1; k<count; k++)
{
if (stu[i].score[lessonId] < stu[k].score[lessonId] )
{
swap(&stu[i],&stu[k]);//sweet
}
}
}
}
/************************************************************************************************/
//功能:按学号搜索学生
//参数:Student stu[]
// int count:学生数目
// int lessonId:要搜索的学号
// Student * student: 搜索到的学生
//返回值:如果搜索到的学生返回1,否则返回0
/************************************************************************************************/
int Search(Student stu[] , int count, char studentId[10], Student * student)
{
int i;
int state = 0;
for (i=0; i<STUDENT_COUNT; i++)
if (cmp(stu[i].id,studentId))
{
strcpy((*student).id,stu[i].id);
strcpy((*student).name,stu[i].name );
(*student).score[0] = stu[i].score [0];
(*student).score[1] = stu[i].score [1];
(*student).score[2] = stu[i].score [2];
(*student).sum = stu[i].sum;
return 1;
}
return 0;
}
/************************************************************************************************/
//功能:计算所有课程的总分
//参数:Student stu[]
// int count:学科数目
//返回值:总分
/************************************************************************************************/
int Sum(int score[] , int count)
{
int sum = 0;
while (count >0)
sum += score[--count];
return sum;
}