#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Stu {
int id;
char name[20];
int math;
int english;
int c;
double average;
struct Stu *next;
}Stu;
Stu *head;
int size;
void menu()
{
printf("\n");
printf(" 欢迎进入学生成绩管理系统 \n");
printf("\n");
printf("* 1、输入学生的信息 \t 6、显示学生的成绩 \n");
printf(" 2、从文件读取信息 \t 7、统计学生的成绩 \n");
printf(" 3、查询学生的成绩 \t 8、保存输入的信息 \n");
printf(" 4、删除学生的信息 \t 9、对成绩进行排序 \n");
printf(" 5、插入学生的信息 \t 0、退出系统 \n");
printf("**************************************\n");
printf("请从0~9中选择功能操作:\n");
}
void show() {
Stu *stu = head->next;
if (size == 0) {
printf("目前没有学生信息\n");
return;
}
printf("学生编号 学生姓名 高数成绩 英语成绩 C语言成绩\n");
while (stu != NULL) {
printf("%d\t %s\t %d\t %d\t %d\n", stu->id, stu->name, stu->math, stu->english, stu->c);
stu = stu->next;
}
}
void read() {
FILE *fp;
Stu stu = { 0 };
Stu *pre = head;
fp = fopen("D:\data.txt", "r");
size = 0;
if (fp != NULL) {
while (fread(&stu, sizeof(stu), 1, fp)) {
Stu *new = (Stu *) malloc(sizeof(Stu));
new->id = stu.id;
strcpy(new->name, stu.name);
new->math = stu.math;
new->english = stu.english;
new->c = stu.c;
new->average = stu.average;
new->next = NULL;
pre->next = new;
pre = new;
size++;
}
}
fclose(fp);
printf("读取文件成功\n");
show(); // 读取完文件的内容显示出来}
void write() {
Stu *stu = head->next;
FILE *fp;
fp = fopen("D:\data.txt", "w");
while (stu != NULL) {
fwrite(stu, sizeof(Stu), 1, fp);
stu = stu->next;
}
fclose(fp);
printf("保存文件成功\n");
}
void add() {
int n, i;
Stu *cur = head;
Stu *temp;
while (cur->next != NULL) {
cur = cur->next;
}
printf("要输入学生的数量:");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("请输入第 %d 名学生信息\n", i+1);
temp = (Stu *) malloc(sizeof(Stu));
printf("请输入学生的学号:");
scanf("%d", &temp->id);
printf("请输入学生的姓名:");
scanf("%s", temp->name);
printf("请输入学生的高数成绩:");
scanf("%d", &temp->math);
printf("请输入学生的英语成绩:");
scanf("%d", &temp->english);
printf("请输入学生的C语言成绩:");
scanf("%d", &temp->c);
temp->average = (temp->english + temp->math + temp->c) / 3.0;
temp->next = NULL;
cur->next = temp;
cur = temp;
size++;
}
}
void queryById() {
int id;
Stu *stu = head->next;
printf("请输入要查询学生的学号:");
scanf("%d", &id);
while (stu != NULL) {
if (stu->id == id) {
printf("查询成功,学生学生信息如下:\n");
printf("学生编号:%d 学生姓名:%s 学生高数成绩: %d 英语成绩: %d C语言成绩: %d\n", stu->id, stu->name, stu->math, stu->english, stu->c);
return;
}
stu = stu->next;
}
printf("查询失败\n");
}
void queryByName() {
char name[20];
Stu *stu = head->next;
printf("请输入要查询学生的姓名:");
scanf("%s", name);
while (stu != NULL) {
if (strcmp(stu->name, name) == 0) {
printf("查询成功,学生学生信息如下:\n");
printf("学生编号:%d 学生姓名:%s 学生高数成绩: %d 英语成绩: %d C语言成绩: %d\n", stu->id, stu->name, stu->math, stu->english, stu->c);
return;
}
stu = stu->next;
}
printf("查询失败\n");
}
void del() {
int id;
Stu *pre = head;
Stu *temp;
printf("请输入要删除的学生的编号:");
scanf("%d", &id);
while (pre->next != NULL) {
if (pre->next->id == id) {
temp = pre->next;
pre->next = temp->next;
free(temp);
printf("删除成功!\n");
return;
}
pre = pre->next;
}
printf("删除失败!没有该编号学生\n");
}
void query() {
int type;
Stu *stu = head->next;
printf("1、按学号查询\n");
printf("2、按姓名查询\n");
printf("请选择查询的方式:");
scanf("%d", &type);
switch (type) {
case 1:
queryById();
break;
case 2:
queryByName();
break;
}
}
void insert() {
int index;
Stu *pre = head;
printf("请输入要插入的位置:\n");
scanf("%d", &index);
if (index > size + 1) {
printf("插入失败,输入位置不存在\n");
return;
}
index--;
while (index > 0) {
pre = pre->next;
index--;
}
Stu *temp = (Stu *) malloc(sizeof(Stu));
printf("请输入学生的学号:");
scanf("%d", &temp->id);
printf("请输入学生的姓名:");
scanf("%s", temp->name);
printf("请输入学生的高数成绩:");
scanf("%d", &temp->math);
printf("请输入学生的英语成绩:");
scanf("%d", &temp->english);
printf("请输入学生的C语言成绩:");
scanf("%d", &temp->c);
temp->average = (temp->english + temp->math + temp->c) / 3.0;
temp->next = pre->next;
pre->next = temp;
size++;
printf("插入成功!\n");
}
void swap(Stu *first, Stu *second) {
int temp;
char name[20];
double ave;
temp = first->id;
first->id = second->id;
second->id = temp;
strcpy(name, first->name);
strcpy(first->name, second->name);
strcpy(second->name, name);
temp = first->math;
first->math = second->math;
second->math = temp;
temp = first->english;
first->english = second->english;
second->english = temp;
temp = first->c;
first->c = second->c;
second->c = temp;
ave = first->average;
first->average = second->average;
second->average = ave;}
void sortByType(int type) {
Stu *first = head;
Stu *second;
while (first != NULL) {
second = first->next;
while (second != NULL) {
if ((type == 1) && (first->average < second->average)) {
swap(first, second);
} else if ((type == 2) && (first->math < second->math)) {
swap(first, second);
} else if ((type == 3) && (first->english < second->english)) {
swap(first, second);
} else if ((type == 4) && (first->c < second->c)) {
swap(first, second);
}
second = second->next;
}
first = first->next;
}
}
void sort() {
int type;
printf("1、按学生平均成绩排序\n");
printf("2、按高数成绩排序\n");
printf("3、按英语成绩排序\n");
printf("4、按C语言成绩排序\n");
printf("请输入排序类型:\n");
scanf("%d", &type);
sortByType(type);
show();
}
void statistic() {
int mathSum = 0, mathA = 0, mathB = 0, mathC = 0, mathD = 0;
int engSum = 0, engA = 0, engB = 0, engC = 0, engD = 0;
int cSum = 0, cA = 0, cB = 0, cC = 0, cD = 0;
int math, eng, c;
Stu *stu = head->next;
while (stu != NULL) {
math = stu->math;
mathSum += math;
if (math >= 85) {
mathA++;
} else if (math >= 75) {
mathB++;
} else if (math >= 60) {
mathC++;
} else {
mathD++;
}
eng = stu->english;
engSum += eng;
if (eng >= 85) {
engA++;
} else if (eng >= 75) {
engB++;
} else if (eng >= 60) {
engC++;
} else {
engD++;
}
c = stu->c;
cSum += c;
if (c >= 85) {
cA++;
} else if (c >= 75) {
cB++;
} else if (c >= 60) {
cC++;
} else {
cD++;
}
stu = stu->next;
}
printf("统计结果如下:\n");
printf("学生人数:%d \n", size);
printf("高数平均分:%.2f 优秀人数:%d, 良好人数:%d, 合格人数:%d, 不及格人数:%d\n", 1.0 * mathSum/size, mathA, mathB, mathC, mathD);
printf("英语平均分:%.2f 优秀人数:%d, 良好人数:%d, 合格人数:%d, 不及格人数:%d\n", 1.0 * engSum/size, engA, engB, engC, engD);
printf("C语言平均分:%.2f 优秀人数:%d, 良好人数:%d, 合格人数:%d, 不及格人数:%d\n", 1.0 * cSum/size, cA, cB, cC, cD);
}
int main(){
int select;
head = (Stu *) malloc(sizeof(Stu));
head->next = NULL;
size = 0;
while(1){
menu();//菜单页面显示
scanf("%d",&select);
switch (select)
{
case 1:
add();
break;
case 2:
read();
break;
case 3:
query();
break;
case 4:
del();
break;
case 5:
insert();
break;
case 6:
show();
break;
case 7:
statistic();
break;
case 8:
write();
break;
case 9:
sort();
break;
case 0:
return 0;
default:
printf("错误指令,请重新输入!\n");
break;
}
}
}