#include<stdio.h>
#include<string.h>
#include<math.h>
#include "malloc.h"
#include <stdlib.h>
#define MAX_LEN 10 /* 字符串最大长度 */
#define STU_NUM 30 /* 最多的学生人数 */
#define COURSE_NUM 6 /* 最多的考试科目数 */
#define LEN sizeof(struct Student)
typedef struct Student
{
long num; /* 每个学生的学号 */
char name[MAX_LEN]; /* 每个学生的姓名 */
float score[COURSE_NUM]; /* 每个学生COURSE_NUM门功课的成绩 */
float sum; /* 每个学生的总成绩 */
float aver; /* 每个学生的平均成绩 */
struct Student *next;
}STU;
int Menu(void); //创建菜单
void Print(STU *head, int n, int m); //打印函数
void AverSumofEveryStudent(STU *head, int n, int m); //计算每门课程的总分和平均分
void AverSumofEveryCourse(STU *head, int n, int m); //计算每个学生的总分和平均分
STU *SortbyScore(STU *head, int n); //按每个学生的总分由高到低排出名次表
STU *Creat(int n, int m); //创建链表并录入信息
STU *Creat1(int n, int m);
STU *SortbyScore1(STU *head, int n); //按每个学生的总分由低到高排出名次表
STU *SortbyNum(STU *head); //按学号由小到大排出成绩表
STU *SortbyName(STU *head, int n); //按姓名的字典顺序排出成绩表
void SearchbyNum(STU *head, int n, int m); //按学号查询学生排名及其考试成绩
void SearchbyName(STU *head, int n, int m); //按姓名查询学生排名及其考试成绩
void StatisticAnalysis(STU *head, int n, int m); //按类别及比例输出
void WritetoFile(STU *head, int n, int m); //将每个学生的纪录信息写入文件
STU *ReadfromFile(STU *head, int *n, int *m); //从文件中读出每个学生的纪录信息并显示
int main()
{
int n, m;
int i;
STU *head; //定义头节点
head = (STU *)malloc(LEN);
while (1)
{
i = Menu();
if (i == 1)
{
system("cls"); //清屏
printf("\t\t\t******************************************************************************\n");
printf("\t\t\tInput student number(n<30):\n"); //输入学生数
printf("\t\t\t");
scanf("%d", &n);
printf("\t\t\tInput course number(m<=6):\n");
printf("\t\t\t");
scanf("%d", &m);
}
switch (i)
{
case 1:
printf("\t\t\tInput student's ID, name and score:\n");
head = Creat(n, m);
system("cls"); //清屏
break;
case 2:
system("cls"); //清屏
AverSumofEveryStudent(head, n, m);
break;
case 3:
system("cls"); //清屏
AverSumofEveryCourse(head, n, m);
break;
case 4:
system("cls"); //清屏
printf("\n\n\n");
printf("\t\t\tSort in ascending order by score:\n");
head = SortbyScore(head, n);
Print(head, n, m);
break;
case 5:
system("cls"); //清屏
printf("\n\n\n");
printf("\t\t\tSort in ascending order by score:\n");
head = SortbyScore1(head, n);
Print(head, n, m);
break;
case 6:
system("cls"); //清屏
printf("\n\n\n");
printf("\t\t\tSort in ascending order by number:\n");
head = SortbyNum(head);
Print(head, n, m);
break;
case 7:
system("cls"); //清屏
printf("\n\n\n");
printf("\t\t\tSort in dictionary order by name:\n");
head = SortbyName(head, n);
Print(head, n, m);
break;
case 8:
system("cls"); //清屏
printf("\n\n\n");
printf("\t\t\t******************************************************************************\n");
printf("\t\t\tInput the number you want to search:\n");
SearchbyNum(head, n, m);
break;
case 9:
system("cls"); //清屏
printf("\n\n\n");
printf("\t\t\t******************************************************************************\n");
printf("\t\t\tInput the name you want to search:\n");
SearchbyName(head, n, m);
break;
case 10:
system("cls"); //清屏
StatisticAnalysis(head, n, m);
break;
case 11:
system("cls"); //清屏
printf("\n\n\n");
Print(head, n, m);
break;
case 12:
system("cls"); //清屏
WritetoFile(head, n, m);
break;
case 13:
system("cls"); //清屏
head = ReadfromFile(head, &n, &m);
break;
case 0:
system("cls"); //清屏
printf("\n\n\n");
printf("\t\t\t******************************************************************************\n");
printf("\t\t\tEnd of program!\n");
printf("\t\t\t******************************************************************************\n");
return 0;
default:
system("cls"); //清屏
printf("\n\n\n");
printf("\t\t\t******************************************************************************\n");
printf("\t\t\tInput error!\n");
printf("\t\t\t******************************************************************************\n");
}
}
return 0;
}
// 创建菜单
int Menu(void)
{
int i;
system("title 学生成绩管理系统V7.0 By wyxiang");
printf("\n\n");
printf("\t\t\t 学生成绩管理系统V7.0\n");
printf("\t\t\t******************************************************************************\n");
printf("\t\t\t* Management for Students' scores *\n");
printf("\t\t\t* 1. Input record *\n");
printf("\t\t\t* 2. Caculate total and average score of every course *\n");
printf("\t\t\t* 3. Caculate total and average score of every student *\n");
printf("\t\t\t* 4. Sort in descending order by score *\n");
printf("\t\t\t* 5. Sort in ascending order by score *\n");
printf("\t\t\t* 6. Sort in ascending order by number *\n");
printf("\t\t\t* 7. Sort in dictionary order by name *\n");
printf("\t\t\t* 8. Search by number *\n");
printf("\t\t\t* 9. Search by name *\n");
printf("\t\t\t* 10. Statistic analysis *\n");
printf("\t\t\t* 11. List record *\n");
printf("\t\t\t* 12. Write to a file *\n");
printf("\t\t\t* 13. Read from a file *\n");
printf("\t\t\t* 0. Exit *\n");
printf("\t\t\t******************************************************************************\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\t\t\t*****************Please Input your choice:");
scanf("%d", &i);
return i;
}
// 创建链表
STU *Creat(int n, int m) {
STU *head;
STU *p1, *p2;
int i, j;
// system("cls");
for (i = 1; i<n + 1; i++)
{
p1 = (STU *)malloc(LEN);
printf("\t\t\t");
scanf("%ld", &p1->num);
printf("\t\t\t");
scanf("%s", p1->name);
for (j = 0; j<m; j++)
{
printf("\t\t\t");
scanf("%f", &p1->score[j]);
}
p1->next = NULL;
if (i == 1)
{
head = p2 = p1;
}
else
{
p2->next = p1;
p2 = p1;
}
}
return(head);
}
STU *Creat1(int n, int m)
{
STU *head;
STU *p1, *p2;
int i, j;
// system("cls");
for (i = 1; i<n + 1; i++)
{
p1 = (STU *)malloc(LEN);
p1->next = NULL;
if (i == 1)
{
head = p2 = p1;
}
else
{
p2->next = p1;
p2 = p1;
}
}
return(head);
}
void AverSumofEveryStudent(STU *head, int n, int m)
{
STU *p;
int i, j = 1;
char ch;
float sum;
p = head;
if (head != NULL)
{
printf("\t\t\t******************************************************************************\n");
for (i = 0; i<m; i++)
{
p = head;
sum = 0.0;
do {
sum += p->score[i];
p = p->next;
} while (p != NULL);
printf("\t\t\tcourse %d: sum=%.0f , aver=%.0f\n", j, sum, sum / n);
j++;
}
printf("\t\t\t***************************************************************