当前位置:网站首页>【升级版学生信息管理系统】+文件操作+更多细节

【升级版学生信息管理系统】+文件操作+更多细节

2022-06-21 12:28:00 每天都要坚持刷题

目录

0.题目要求:

预览效果图 

1.头文件和结构体类型定义

 2.功能设计(菜单设计)

3,。整体框架(主函数) 

4.分函数实现

4-0初始化并且将文件加载到程序

 4-1 退出程序并将程序数据保存到文件中

4-2录入学生信息 

4-3 删除学生信息

4-4 查询指定课程

4-5 查询指定学生

 4-6修改学生信息

 4-7按照学号排序

 4-8统计学生总数

 4-9打印学生信息

 4-10选择错误,重新选择

 5.源代码


0.题目要求:

预览效果图 

 

 

 

 

 

1.头文件和结构体类型定义

 

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>

typedef struct Student
{
	char name[20];//名字
	char id[20];//学号
	double ChiScore;//语文成绩
	double MatScore;//数学成绩
	double EngScore;//英语成绩
	double Score;//总成绩
}Student;

typedef struct SeqList
{
	Student* stu;
	int size;
	int capacity;
}SeqList;

 

 2.功能设计(菜单设计)

void Meau(void)
{
	printf("-------------------------------\n");
	printf("----     0.退出程序         ----\n");
	printf("----     1.录入学生信息     ----\n");
	printf("----     2.删除学生信息     ----\n");
	printf("----     3.查询指定课程     ----\n");
	printf("----     4.查询指定学生     ----\n");
	printf("----     5.修改学生成绩     ----\n");
	printf("----     6.按照学号排序     ----\n");
	printf("----     7.统计学生总数     ----\n");
	printf("----     8.打印学生信息     ----\n");
	printf("-------------------------------\n");
}

3,。整体框架(主函数) 

int main()
{
	SeqList ST;
	InitSeqList(&ST);

	int input = 0;
	do//先执行一次
	{
		Meau();
		printf("请输入您的选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 0:
			printf("感谢您的使用,即将退出程序.\n");
			SaveSeqList(&ST);
			Sleep(2000);
			system("cls");
			break;
		case 1:
			//printf("录入学生信息.\n");
			int num = 0;
			printf("请输入你要录入学生的个数:>");
			scanf("%d", &num);
			for (int i = 1; i <=num; i++)
			{
				Student newstu;
				//将学生信息打包好
				InitStudent(&newstu,i);
				//录入学生信息操作
				AddSeqList(&ST, newstu);
			}
			printf("录入成功.\n");
			//插入后自动按照学生成绩排序-希尔排序
			ShellSortByScore(&ST, ST.size);
			Sleep(2000);
			system("cls");
			break;
		case 2:
			//printf("删除学生信息.\n");
			DelSeqList(&ST);
			Sleep(500);
			system("cls");
			break;
		case 3:
			//printf("查询指定课程.\n");
			SearchByCourse(&ST);
			Sleep(2000);
			system("cls");
			break;
		case 4:
			//printf("查询指定学生.\n");
			SearchByStuName(&ST);
			Sleep(2000);
			system("cls");
			break;
		case 5:
			//printf("修改学生成绩.\n");
			ModifySeqList(&ST);
			ShellSortByScore(&ST, ST.size);
			Sleep(2000);
			system("cls");
			break;
		case 6:
			//printf("按照学号排序.\n");
			QsortById(&ST,0,ST.size-1);
			PrintSeqList(&ST);
			Sleep(2000);
			system("cls");
			break;
		case 7:
			//printf("统计学生总数.\n");
			printf("当前学生总数:%d\n", ST.size);
			Sleep(500);
			system("cls");
			break;
		case 8:
			//printf("打印学生信息.\n");
			PrintSeqList(&ST);
			Sleep(2000);
			system("cls");
	    	break;
		default:
			printf("输入错误,请重新输入.\n");
			Sleep(1000);
			system("cls");
			break;
		}
	} while (input);
}

4.分函数实现

4-0初始化并且将文件加载到程序

 


void CheckCapacity(SeqList* ps)
{
	if (ps->capacity == ps->size)
	{
		int newcapacity = 2 * (ps->capacity);
		Student* temp = (Student*)realloc(ps->stu, sizeof(Student)*newcapacity);
		if (temp == NULL)
		{
			perror("CheckCapacity::realloc");
			return;
		}
		ps->capacity = newcapacity;
		ps->stu = temp;
		printf("扩容成功.\n");
	}
}


//二进制的方式读
void LoadSeqList(SeqList* ps)
{
	FILE* pf = fopen("D:\\桌面\\test.txt", "rb");
	if (pf == NULL)
	{
		perror("LoadSeqList::fopen");
		return;
	}
	Student temp = { 0 };
	while (fread(&temp, sizeof(Student), 1, pf))
	{
		CheckCapacity(ps);
		ps->stu[ps->size] = temp;
		ps->size++;
	}
	fclose(pf);
	pf = NULL;
}


void InitSeqList(SeqList* ps)
{
	ps->stu = (Student*)malloc(sizeof(Student) * 4);
	if (ps->stu == NULL)
	{
		perror("InitSeqList::malloc");
		return;
	}
	ps->size = 0;
	ps->capacity = 4;

	LoadSeqList(ps);
}

 4-1 退出程序并将程序数据保存到文件中

//退出前保存学生管理系统中的信息
void SaveSeqList(const SeqList* ps)
{
	FILE* pf = fopen("D:\\桌面\\test.txt", "wb");
	if (pf == NULL)
	{
		perror("SaveSeqList::fopen");
		return;
	}

	//写文件
	for (int i = 0; i < ps->size; i++)
	{
		fwrite(ps->stu + i, sizeof(Student), 1, pf);
	}

	fclose(pf);
	pf = NULL;
}

4-2录入学生信息 


void CheckCapacity(SeqList* ps)
{
	if (ps->capacity == ps->size)
	{
		int newcapacity = 2 * (ps->capacity);
		Student* temp = (Student*)realloc(ps->stu, sizeof(Student)*newcapacity);
		if (temp == NULL)
		{
			perror("CheckCapacity::realloc");
			return;
		}
		ps->capacity = newcapacity;
		ps->stu = temp;
		printf("扩容成功.\n");
	}
}


//版本1:一个个录入
//void AddStudent(SeqList* ps)
//{
//	//检查顺序表是否需要扩容
//	CheckCapacity(ps);
//
//	printf("请输入学生名字:>");
//	scanf("%s", ps->stu[ps->size].name);
//	printf("请输入学生学号:>");
//	scanf("%s", ps->stu[ps->size].id);
//	printf("请输入学生的语文成绩:>");
//	scanf("%d", &(ps->stu[ps->size].ChiScore));
//	printf("请输入学生的数学成绩:>");
//	scanf("%d", &(ps->stu[ps->size].MatScore));
//	printf("请输入学生的英语成绩:>");
//	scanf("%d", &(ps->stu[ps->size].EngScore));
//
//	printf("录入学生成绩成功.\n");
//	ps->size++;
//
//}

//版本2:一次录入多个学生
void InitStudent(Student* ps,int i)
{
	printf("请输入第%d个待录入学生的信息.\n",i);

	printf("请输入学生名字:>");
	scanf("%s", ps->name);
	printf("请输入学生学号:>");
	scanf("%s", ps->id);
	printf("请输入%s的语文成绩:>",ps->name);
	scanf("%lf", &(ps->ChiScore));
	printf("请输入%s的数学成绩:>",ps->name);
	scanf("%lf", &(ps->MatScore));
	printf("请输入%s的英语成绩:>",ps->name);
	scanf("%lf", &(ps->EngScore));
    
	ps->Score = ps->ChiScore + ps->MatScore + ps->EngScore;
}

void AddSeqList(SeqList* ps, Student newstu)
{
	CheckCapacity(ps);
	ps->stu[ps->size] = newstu;
	ps->size++;
}

4-3 删除学生信息

//删除
void DelSeqList(SeqList* ps)
{
	if (ps->size == 0)
	{
		printf("当前学生总数为0,无法删除.\n");
		return;
	}
	char name[20]={0};
	printf("请输入您要删除学生的学生姓名:>");
	scanf("%s", name);
	int pos = FindByName(ps, name);
	if (pos != -1)
	{
		//找到了,则删除下标为i(pos)的元素
		int begin = pos+1;
		while (begin < ps->size)
		{
			ps->stu[begin - 1] = ps->stu[begin];
			begin++;
		}
		ps->size--;
		printf("删除成功.\n");
	}
	else
	{
		//找不到,做出反馈
		printf("当前学生信息管理系统中不存在名字为%s的学生.", name);
	}
	
}

4-4 查询指定课程

 


//查询指定课程

double cmp_struct_by_ChiScore(const void* e1, const void* e2)
{
	return  ((Student*)e2)->ChiScore- ((Student*)e1)->ChiScore;
}

double cmp_struct_by_MatScore(const void* e1,const void* e2)
{
	return   ((Student*)e2)->MatScore- ((Student*)e1)->MatScore;
}

double cmp_struct_by_EngScore(const void* e1, const void* e2)
{
	return   ((Student*)e2)->EngScore- ((Student*)e1)->EngScore;
}


void DisPlayByChiScore(SeqList* ps)
{
	printf("%-20s %-20s %-10s %-20s\n","姓名","学号","语文成绩","语文成绩排名");
	for (int i = 0; i < ps->size; i++)
	{
		printf("%-20s %-20s %-10.2lf %-20d\n", ps->stu[i].name, ps->stu[i].id, ps->stu[i].ChiScore,i+1);
	}
}

void DisPlayByMatScore(SeqList* ps)
{
	printf("%-20s %-20s %-10s %-20s\n", "姓名", "学号", "数学成绩", "数学成绩排名");
	for (int i = 0; i < ps->size; i++)
	{
		printf("%-20s %-20s %-10.2lf %-20d\n", ps->stu[i].name, ps->stu[i].id, ps->stu[i].MatScore,i+1);
	}
}

void DisPlayByEngScore(SeqList* ps)
{
	printf("%-20s %-20s %-10s %-20s\n", "姓名", "学号", "英语成绩", "英语成绩排名");
	for (int i = 0; i < ps->size; i++)
	{
		printf("%-20s %-20s %-10.2lf %-20d\n", ps->stu[i].name, ps->stu[i].id, ps->stu[i].EngScore,i+1);
	}
}

void SearchByCourse(SeqList* ps)
{
	//求要查询课程的平均分
	char course[5] = { 0 };
	printf("请输入要查询的课程名称:>");
	scanf("%s", course);
	double sum = 0;
	if (strcmp(course, "语文") == 0)
	{
		for (int i = 0; i < ps->size; i++)
		{
			sum += ps->stu[i].ChiScore;
		}
		//按语文成绩排序
	qsort(ps->stu, ps->size, sizeof(ps->stu[0]), cmp_struct_by_ChiScore);
	DisPlayByChiScore(ps);
	}
	else if (strcmp(course, "数学") == 0)
	{
		for (int i = 0; i < ps->size; i++)
		{
			sum += ps->stu[i].MatScore;
		}
		//按数学成绩排序
		qsort(ps->stu, ps->size, sizeof(ps->stu[0]), &cmp_struct_by_MatScore);
		DisPlayByMatScore(ps);
	}
	else if (strcmp(course, "英语") == 0)
	{
		for (int i = 0; i < ps->size; i++)
		{
			sum += ps->stu[i].EngScore;
		}
		//按英语成绩排序
		qsort(ps->stu, ps->size, sizeof(ps->stu[0]), cmp_struct_by_EngScore);
		DisPlayByEngScore(ps);
	}
	double averge = 1.0 * sum / (ps->size);
	printf("%s的平均分:>%.2lf\n", course, averge);

	//不同分数段的学生人数
	int size1 = 0, size2 = 0, size3 = 0;
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->stu[i].Score < 60)
		{
			size1++;
		}
		else if (ps->stu[i].Score < 80)
		{
			size2++;
		}
		else
		{
			size3++;
		}
	}
	printf("不及格人数:%d\t及格人数:%d\t优秀人数:%d\n", size1, size2, size3);

}

 

 

4-5 查询指定学生

//查询指定学生
void SearchByStuName(SeqList* ps)
{
	char name[20] = { 0 };
	printf("请输入你要查询的学生姓名:>");
	scanf("%s", name);
	int pos = FindByName(ps, name);
	printf("名字为%s的学生的相关信息:>\n", name);
	printf("学号:%s\t语文成绩:%.2lf\t数学成绩:%.2lf\t英语成绩:%.2lf\t总分:%.2lf\t班级排名:%d\n",
		ps->stu[pos].id, ps->stu[pos].ChiScore, ps->stu[pos].MatScore, ps->stu[pos].EngScore,
		ps->stu[pos].Score, pos + 1);
}

 4-6修改学生信息


//找到返回下标,找不到返回-1
int FindByName(const SeqList* ps, char* name)
{
	for (int i = 0; i < ps->size; i++)
	{
		if (strcmp(ps->stu[i].name, name) == 0)
		{
			return i;
		}
	}
	return -1;
}


//修改学生信息
void ModifySeqList(SeqList* ps)
{
	//找到要修改学生的下标
	char name[20] = { 0 };
	printf("请输入您要修改学生的学生姓名:>");
	scanf("%s", name);
	int pos = FindByName(ps, name);
	//修改科目名称
	char course[5] = { 0 };
	printf("请输入要修改的成绩科目名称:>");
	scanf("%s", course);
	//修改后的分数
	double newscore = 0.0;
	printf("请输入修改后%s同学%s的成绩:>", name, course);
	scanf("%lf", &newscore);
	if (strcmp(course, "语文") == 0)
	{
		ps->stu[pos].ChiScore = newscore;
	}
	else if (strcmp(course, "数学") == 0)
	{
		ps->stu[pos].MatScore = newscore;
	}
	else if (strcmp(course, "英语") == 0)
	{
		ps->stu[pos].EngScore = newscore;
	}

	printf("修改成功.\n");
}

 

 4-7按照学号排序

//按学号来排序

void Swap(Student* s1, Student* s2)
{
	Student temp = *s1;
	*s1 = *s2;
	*s2 = temp;
}

void QsortById(SeqList* ps, int begin, int end)
{
	if (begin >= end)
	{
		return;
	}
	int keyi = begin;
	int left = begin, right = end;
	while (left < right)
	{
		while (left < right && strcmp(ps->stu[right].id, ps->stu[keyi].id)>0)
		{
		right--;
        }
		while (left < right && strcmp(ps->stu[left].id, ps->stu[keyi].id) <= 0)
		{
			left++;
		}
		Swap(&(ps->stu[left]), &(ps->stu[right]));
	}
	int meeti = left;
	Swap(&(ps->stu[meeti]), &(ps->stu[keyi]));
	QsortById(ps, begin, meeti - 1);
	QsortById(ps, meeti + 1, end);
}

 

 

 4-8统计学生总数

 4-9打印学生信息


//打印
void PrintSeqList(SeqList* ps)
{
	printf("%-20s %-20s %-8s %-8s %-8s %-5s %-5s\n", "姓名", "学号", "语文成绩", "数学成绩", "英语成绩","总分","总分数班级排名");

	for (int i = 0; i < ps->size; i++)
	{
		printf("%-20s %-20s %-8.2lf %-8.2lf %-8.2lf %-5.2lf %-5d\n", ps->stu[i].name, ps->stu[i].id, ps->stu[i].ChiScore, 
		ps->stu[i].MatScore, ps->stu[i].EngScore, ps->stu[i].Score,i+1);
	}
}

 4-10选择错误,重新选择

 5.源代码

只用了一个源文件(test.c)写

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>

typedef struct Student
{
	char name[20];//名字
	char id[20];//学号
	double ChiScore;//语文成绩
	double MatScore;//数学成绩
	double EngScore;//英语成绩
	double Score;//总成绩
}Student;

typedef struct SeqList
{
	Student* stu;
	int size;
	int capacity;
}SeqList;

//菜单函数
void Meau(void)
{
	printf("-------------------------------\n");
	printf("----     0.退出程序         ----\n");
	printf("----     1.录入学生信息     ----\n");
	printf("----     2.删除学生信息     ----\n");
	printf("----     3.查询指定课程     ----\n");
	printf("----     4.查询指定学生     ----\n");
	printf("----     5.修改学生成绩     ----\n");
	printf("----     6.按照学号排序     ----\n");
	printf("----     7.统计学生总数     ----\n");
	printf("----     8.打印学生信息     ----\n");
	printf("-------------------------------\n");
}



void CheckCapacity(SeqList* ps)
{
	if (ps->capacity == ps->size)
	{
		int newcapacity = 2 * (ps->capacity);
		Student* temp = (Student*)realloc(ps->stu, sizeof(Student)*newcapacity);
		if (temp == NULL)
		{
			perror("CheckCapacity::realloc");
			return;
		}
		ps->capacity = newcapacity;
		ps->stu = temp;
		printf("扩容成功.\n");
	}
}


//二进制的方式读
void LoadSeqList(SeqList* ps)
{
	FILE* pf = fopen("D:\\桌面\\test.txt", "rb");
	if (pf == NULL)
	{
		perror("LoadSeqList::fopen");
		return;
	}
	Student temp = { 0 };
	while (fread(&temp, sizeof(Student), 1, pf))
	{
		CheckCapacity(ps);
		ps->stu[ps->size] = temp;
		ps->size++;
	}
	fclose(pf);
	pf = NULL;
}


void InitSeqList(SeqList* ps)
{
	ps->stu = (Student*)malloc(sizeof(Student) * 4);
	if (ps->stu == NULL)
	{
		perror("InitSeqList::malloc");
		return;
	}
	ps->size = 0;
	ps->capacity = 4;

	LoadSeqList(ps);
}



//版本1:一个个录入
//void AddStudent(SeqList* ps)
//{
//	//检查顺序表是否需要扩容
//	CheckCapacity(ps);
//
//	printf("请输入学生名字:>");
//	scanf("%s", ps->stu[ps->size].name);
//	printf("请输入学生学号:>");
//	scanf("%s", ps->stu[ps->size].id);
//	printf("请输入学生的语文成绩:>");
//	scanf("%d", &(ps->stu[ps->size].ChiScore));
//	printf("请输入学生的数学成绩:>");
//	scanf("%d", &(ps->stu[ps->size].MatScore));
//	printf("请输入学生的英语成绩:>");
//	scanf("%d", &(ps->stu[ps->size].EngScore));
//
//	printf("录入学生成绩成功.\n");
//	ps->size++;
//
//}

//版本2:一次录入多个学生
void InitStudent(Student* ps,int i)
{
	printf("请输入第%d个待录入学生的信息.\n",i);

	printf("请输入学生名字:>");
	scanf("%s", ps->name);
	printf("请输入学生学号:>");
	scanf("%s", ps->id);
	printf("请输入%s的语文成绩:>",ps->name);
	scanf("%lf", &(ps->ChiScore));
	printf("请输入%s的数学成绩:>",ps->name);
	scanf("%lf", &(ps->MatScore));
	printf("请输入%s的英语成绩:>",ps->name);
	scanf("%lf", &(ps->EngScore));
    
	ps->Score = ps->ChiScore + ps->MatScore + ps->EngScore;
}

void AddSeqList(SeqList* ps, Student newstu)
{
	CheckCapacity(ps);
	ps->stu[ps->size] = newstu;
	ps->size++;
}



//找到返回下标,找不到返回-1
int FindByName(const SeqList* ps, char* name)
{
	for (int i = 0; i < ps->size; i++)
	{
		if (strcmp(ps->stu[i].name, name) == 0)
		{
			return i;
		}
	}
	return -1;
}


//删除
void DelSeqList(SeqList* ps)
{
	if (ps->size == 0)
	{
		printf("当前学生总数为0,无法删除.\n");
		return;
	}
	char name[20]={0};
	printf("请输入您要删除学生的学生姓名:>");
	scanf("%s", name);
	int pos = FindByName(ps, name);
	if (pos != -1)
	{
		//找到了,则删除下标为i(pos)的元素
		int begin = pos+1;
		while (begin < ps->size)
		{
			ps->stu[begin - 1] = ps->stu[begin];
			begin++;
		}
		ps->size--;
		printf("删除成功.\n");
	}
	else
	{
		//找不到,做出反馈
		printf("当前学生信息管理系统中不存在名字为%s的学生.", name);
	}
	
}







//打印
void PrintSeqList(SeqList* ps)
{
	printf("%-20s %-20s %-8s %-8s %-8s %-5s %-5s\n", "姓名", "学号", "语文成绩", "数学成绩", "英语成绩","总分","总分数班级排名");

	for (int i = 0; i < ps->size; i++)
	{
		printf("%-20s %-20s %-8.2lf %-8.2lf %-8.2lf %-5.2lf %-5d\n", ps->stu[i].name, ps->stu[i].id, ps->stu[i].ChiScore, 
		ps->stu[i].MatScore, ps->stu[i].EngScore, ps->stu[i].Score,i+1);
	}
}






//修改学生信息
void ModifySeqList(SeqList* ps)
{
	//找到要修改学生的下标
	char name[20] = { 0 };
	printf("请输入您要修改学生的学生姓名:>");
	scanf("%s", name);
	int pos = FindByName(ps, name);
	//修改科目名称
	char course[5] = { 0 };
	printf("请输入要修改的成绩科目名称:>");
	scanf("%s", course);
	//修改后的分数
	double newscore = 0.0;
	printf("请输入修改后%s同学%s的成绩:>", name, course);
	scanf("%lf", &newscore);
	if (strcmp(course, "语文") == 0)
	{
		ps->stu[pos].ChiScore = newscore;
	}
	else if (strcmp(course, "数学") == 0)
	{
		ps->stu[pos].MatScore = newscore;
	}
	else if (strcmp(course, "英语") == 0)
	{
		ps->stu[pos].EngScore = newscore;
	}

	printf("修改成功.\n");
}

//希尔排序-按总成绩排序
void ShellSortByScore(SeqList* ps, int sz)
{
	int gap = sz;
	{
		while (gap > 1)
		{
			gap = (gap / 3 + 1);
			for (int i = 0; i < sz - gap; i++)
			{
				int end = i;
				Student temp = ps->stu[end+gap];
				while (end>=0)
				{
					if (ps->stu[end].Score < temp.Score)
					{
						ps->stu[end + gap] = ps->stu[end];
						end-=gap;
					}
					else
					{
						break;
					}
					ps->stu[end + gap] = temp;
				}
			}
		}
	}
}


//查询指定课程

double cmp_struct_by_ChiScore(const void* e1, const void* e2)
{
	return  ((Student*)e2)->ChiScore- ((Student*)e1)->ChiScore;
}

double cmp_struct_by_MatScore(const void* e1,const void* e2)
{
	return   ((Student*)e2)->MatScore- ((Student*)e1)->MatScore;
}

double cmp_struct_by_EngScore(const void* e1, const void* e2)
{
	return   ((Student*)e2)->EngScore- ((Student*)e1)->EngScore;
}


void DisPlayByChiScore(SeqList* ps)
{
	printf("%-20s %-20s %-10s %-20s\n","姓名","学号","语文成绩","语文成绩排名");
	for (int i = 0; i < ps->size; i++)
	{
		printf("%-20s %-20s %-10.2lf %-20d\n", ps->stu[i].name, ps->stu[i].id, ps->stu[i].ChiScore,i+1);
	}
}

void DisPlayByMatScore(SeqList* ps)
{
	printf("%-20s %-20s %-10s %-20s\n", "姓名", "学号", "数学成绩", "数学成绩排名");
	for (int i = 0; i < ps->size; i++)
	{
		printf("%-20s %-20s %-10.2lf %-20d\n", ps->stu[i].name, ps->stu[i].id, ps->stu[i].MatScore,i+1);
	}
}

void DisPlayByEngScore(SeqList* ps)
{
	printf("%-20s %-20s %-10s %-20s\n", "姓名", "学号", "英语成绩", "英语成绩排名");
	for (int i = 0; i < ps->size; i++)
	{
		printf("%-20s %-20s %-10.2lf %-20d\n", ps->stu[i].name, ps->stu[i].id, ps->stu[i].EngScore,i+1);
	}
}

void SearchByCourse(SeqList* ps)
{
	//求要查询课程的平均分
	char course[5] = { 0 };
	printf("请输入要查询的课程名称:>");
	scanf("%s", course);
	double sum = 0;
	if (strcmp(course, "语文") == 0)
	{
		for (int i = 0; i < ps->size; i++)
		{
			sum += ps->stu[i].ChiScore;
		}
		//按语文成绩排序
	qsort(ps->stu, ps->size, sizeof(ps->stu[0]), cmp_struct_by_ChiScore);
	DisPlayByChiScore(ps);
	}
	else if (strcmp(course, "数学") == 0)
	{
		for (int i = 0; i < ps->size; i++)
		{
			sum += ps->stu[i].MatScore;
		}
		//按数学成绩排序
		qsort(ps->stu, ps->size, sizeof(ps->stu[0]), &cmp_struct_by_MatScore);
		DisPlayByMatScore(ps);
	}
	else if (strcmp(course, "英语") == 0)
	{
		for (int i = 0; i < ps->size; i++)
		{
			sum += ps->stu[i].EngScore;
		}
		//按英语成绩排序
		qsort(ps->stu, ps->size, sizeof(ps->stu[0]), cmp_struct_by_EngScore);
		DisPlayByEngScore(ps);
	}
	double averge = 1.0 * sum / (ps->size);
	printf("%s的平均分:>%.2lf\n", course, averge);

	//不同分数段的学生人数
	int size1 = 0, size2 = 0, size3 = 0;
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->stu[i].Score < 60)
		{
			size1++;
		}
		else if (ps->stu[i].Score < 80)
		{
			size2++;
		}
		else
		{
			size3++;
		}
	}
	printf("不及格人数:%d\t及格人数:%d\t优秀人数:%d\n", size1, size2, size3);

}

//查询指定学生
void SearchByStuName(SeqList* ps)
{
	char name[20] = { 0 };
	printf("请输入你要查询的学生姓名:>");
	scanf("%s", name);
	int pos = FindByName(ps, name);
	printf("名字为%s的学生的相关信息:>\n", name);
	printf("学号:%s\t语文成绩:%.2lf\t数学成绩:%.2lf\t英语成绩:%.2lf\t总分:%.2lf\t班级排名:%d\n",
		ps->stu[pos].id, ps->stu[pos].ChiScore, ps->stu[pos].MatScore, ps->stu[pos].EngScore,
		ps->stu[pos].Score, pos + 1);
}

//按学号来排序

void Swap(Student* s1, Student* s2)
{
	Student temp = *s1;
	*s1 = *s2;
	*s2 = temp;
}

void QsortById(SeqList* ps, int begin, int end)
{
	if (begin >= end)
	{
		return;
	}
	int keyi = begin;
	int left = begin, right = end;
	while (left < right)
	{
		while (left < right && strcmp(ps->stu[right].id, ps->stu[keyi].id)>0)
		{
		right--;
        }
		while (left < right && strcmp(ps->stu[left].id, ps->stu[keyi].id) <= 0)
		{
			left++;
		}
		Swap(&(ps->stu[left]), &(ps->stu[right]));
	}
	int meeti = left;
	Swap(&(ps->stu[meeti]), &(ps->stu[keyi]));
	QsortById(ps, begin, meeti - 1);
	QsortById(ps, meeti + 1, end);
}

//退出前保存学生管理系统中的信息
void SaveSeqList(const SeqList* ps)
{
	FILE* pf = fopen("D:\\桌面\\test.txt", "wb");
	if (pf == NULL)
	{
		perror("SaveSeqList::fopen");
		return;
	}

	//写文件
	for (int i = 0; i < ps->size; i++)
	{
		fwrite(ps->stu + i, sizeof(Student), 1, pf);
	}

	fclose(pf);
	pf = NULL;
}

int main()
{
	SeqList ST;
	InitSeqList(&ST);

	int input = 0;
	do//先执行一次
	{
		Meau();
		printf("请输入您的选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 0:
			printf("感谢您的使用,即将退出程序.\n");
			//文件操作:保存信息
			SaveSeqList(&ST);
			Sleep(2000);
			system("cls");
			break;
		case 1:
			//printf("录入学生信息.\n");
			int num = 0;
			printf("请输入你要录入学生的个数:>");
			scanf("%d", &num);
			for (int i = 1; i <=num; i++)
			{
				Student newstu;
				//将学生信息打包好
				InitStudent(&newstu,i);
				//录入学生信息操作
				AddSeqList(&ST, newstu);
			}
			printf("录入成功.\n");
			//插入后自动按照学生成绩排序-希尔排序
			ShellSortByScore(&ST, ST.size);
			Sleep(2000);
			system("cls");
			break;
		case 2:
			//printf("删除学生信息.\n");
			DelSeqList(&ST);
			Sleep(500);
			system("cls");
			break;
		case 3:
			//printf("查询指定课程.\n");
			SearchByCourse(&ST);
			Sleep(2000);
			system("cls");
			break;
		case 4:
			//printf("查询指定学生.\n");
			SearchByStuName(&ST);
			Sleep(2000);
			system("cls");
			break;
		case 5:
			//printf("修改学生成绩.\n");
			ModifySeqList(&ST);
			ShellSortByScore(&ST, ST.size);
			Sleep(2000);
			system("cls");
			break;
		case 6:
			//printf("按照学号排序.\n");
			QsortById(&ST,0,ST.size-1);
			PrintSeqList(&ST);
			Sleep(2000);
			system("cls");
			break;
		case 7:
			//printf("统计学生总数.\n");
			printf("当前学生总数:%d\n", ST.size);
			Sleep(500);
			system("cls");
			break;
		case 8:
			//printf("打印学生信息.\n");
			PrintSeqList(&ST);
			Sleep(2000);
			system("cls");
	    	break;
		default:
			printf("请在0-8中作选择.\n");
			Sleep(1000);
			system("cls");
			break;
		}
	} while (input);
}
原网站

版权声明
本文为[每天都要坚持刷题]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_64428099/article/details/125381809