C语言——学生成绩管理系统

最近正在学习C语言,搞了一个学生成绩管理系统的小程序,代码粗陋,大佬勿喷。求各位看官随手甩个赞~谢谢 _
先上图:
主菜单
整个程序采用链表来管理学生成绩,保存学生成绩用的是 文件处理函数,使用 fopen(FilePath, “wb”) 将学生信息在内存中的数据直接写入文件,相应的打开程序后读取时使用 fopen(FilePath, “rb”) 将数据直接再次还原到内存中去。

选项6 是手动保存链表数据及配置数据。
选项7 配置每次修改链表中的数据是否需要自动保存。
在这里插入图片描述
选项1 进入后,直接按照提示输入 学生的各个信息,一行一个

在这里插入图片描述
选项3 修改学生信息,进入后选择以什么方式查找要修改的学生,然后输入学生的学号或姓名,找到之后(因为可能存在重名的同学,所以这里做了序号索引),输入结果索引,然后根据提示将该学生的信息重新输入一遍,达到修改的效果。

在这里插入图片描述
选项4 查询,功能如上图所示
在这里插入图片描述

选项5 排序,根据提示输入条件,排序会导致链表数据改变,所以回到主菜单后会自动保存数据到文件

在这里插入图片描述
选项8 投票系统,输入链表中存在的人名,对应下方投票结果会实时变动,并排序,最高票数的人颜色高亮。
在这里插入图片描述
在这里插入图片描述

选项9 输入后右侧出现提示“导出成功”,则在相应目录下会出现student_export.txt文件,里面包含有链表中所有学生信息,这里采用的是 fprintf 函数输出到文件。

链表排序时,数据交换使用了kernel32.dll 中的内存数据拷贝 MoveMemory 函数,只需要将除链表中的pNext 以外的数据进行拷贝交换即可。

下面上代码:

// 学生管理系统.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "string.h"
#include "conio.h"
#include "windows.h"
#include "stdlib.h"

#define LIST_TITLE "学号 姓名 性别 语文 数学 英语\n"
#define LIST_TITLE_1 "学号 姓名 性别 语文 数学 英语 均分\n"
#define FILE_DATABASE "C:\\student_database.dat"
#define FILE_EXPORT "C:\\student_export.txt"
//颜色
enum
{
	BLACK,
	BLUE,
	GREEN,
	CYAN,
	RED,
	MAGENTA,
	BROWN,
	LIGHTGRAY,
	DARKGRAY,
	LIGHTBLUE,
	LIGHTGREEN,
	LIGHTCYAN,
	LIGHTRED,
	LIGHTMAGENTA,
	YELLOW,
	WHITE
};
//功能索引
enum
{
	Func_Add = 1,//添加学生信息
	Func_Delete,//删除
	Func_Modify,//修改
	Func_Search,//搜索
	Func_Sort,//排序
	Func_Save,//保存
	Func_AutoSave,//自动保存
	Func_Vote,//投票系统
	Func_Export,//导出学生信息
	Func_ExitSystem//退出系统
};
struct Student
{
	int num;//学号
	char name[20];//姓名
	char sex[8];//性别
	float score[3];//三门课程成绩
	float averge;//平均分
	int count;//投票计数
	Student* pNext;

};
Student* G_pStuHead;//链表头
bool G_autoStore = false;//自动保存

/************************************************************************/
/*设置字体颜色*/
/************************************************************************/
void setFontColor(int ForgC)
{
	WORD wColor;
	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	
	if(GetConsoleScreenBufferInfo(hOutput, &csbi))
	{
		//设置字体颜色
		wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
		SetConsoleTextAttribute(hOutput, wColor);
	}
}
/************************************************************************/
/*光标跳转到指定位置*/
/************************************************************************/
void gotoxy(int x, int y)
{
    // 更新光标位置 
    COORD pos;
	
    HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(hOutput, pos);
	// 隐藏光标 
//     CONSOLE_CURSOR_INFO cursor;
//     cursor.bVisible = FALSE;
//     cursor.dwSize = 1;	//值介于1 ~ 100 之间 单元格底部为下划线 ~ 完全填充单元格
//     SetConsoleCursorInfo(hOutput, &cursor);
	
}
/************************************************************************/
/*主菜单中指定位置打印是否自动保存*/
/************************************************************************/
void printAutoStoreInfo()
{
	setFontColor(LIGHTGREEN);
	gotoxy(13, 10);
	printf("%s\n", G_autoStore ? "是" : "否");
	setFontColor(WHITE);
}
/************************************************************************/
/*显示最上面的系统标题*/
/************************************************************************/
void showSystemTitle()
{
	setFontColor(LIGHTGREEN);
	printf("--------------------------------------------------------\n");
	printf("\t\t欢迎进入学生管理系统\n");
	printf("--------------------------------------------------------\n");
	setFontColor(WHITE);
}
/************************************************************************/
/*初始化屏幕*/
/************************************************************************/
void initScreen()
{
	system("CLS");
	showSystemTitle();
	printf("请输入数字序列号,选择您要执行的操作:\n");
	printf("1、添加学生信息\n");
	printf("2、删除学生信息\n");
	printf("3、修改学生信息\n");
	printf("4、查询学生信息\n");
	printf("5、排序\n");
	printf("6、保存(如打开自动保存,则无需手动执行)\n");
	printf("7、自动保存:");
	printAutoStoreInfo();
	printf("8、投票系统\n");
	printf("9、导出学生信息\n");
	setFontColor(LIGHTRED);
	printf("10、退出学生管理系统\n");
	setFontColor(WHITE);

}
/************************************************************************/
/*从指定位置开始清除指定长度元素*/
/************************************************************************/
void gotodelete(int x, int y, int length)
{
	int i;
    
	for (i = 0; i < length; i++)
	{
		gotoxy(x + i, y);
		printf(" ");
	}
}
/************************************************************************/
/*清除指定位置元素*/
/************************************************************************/
void gotodelete(int x, int y)
{
	gotodelete(x, y, 1);
}
/************************************************************************/
/*投票系统 */
/************************************************************************/
void voteSystem()
{
	bool hasFound;
	char name[20];
	int count, i, j;
	Student* pStu, *pTmp;
	Student** pStuArr;

	//初始化票数清零
	pStu = G_pStuHead->pNext;
	while(pStu != NULL)
	{
		pStu->count = 0;
		pStu = pStu->pNext;
	}

	count = 0;
	pStuArr = (Student**)malloc(4 * 100);//用于存放已经获得票数的同学指针
	gotoxy(0, 6);
	printf("投票结果如下:\n");
	gotoxy(0, 3);
	printf("请在下方输入你想投给的人的姓名(输入-1返回主菜单):\n");
	
	while (1)
	{
		gotodelete(0, 4, 20);//清空输入行
		gotoxy(0, 4);
		scanf("%s", name);
		
		if(strcmp(name, "-1") == 0)
		{
			break;
		}
		hasFound = false;
		pStu = G_pStuHead->pNext;
		//在系统中查找对应的人名
		while(pStu != NULL)
		{
			if(strcmp(pStu->name, name) == 0)
			{
				hasFound = true;
				break;
			}
			pStu = pStu->pNext;
		}
		if(! hasFound)
		{
			printf("查无此人!!!");
			Sleep(1000);
			gotodelete(0, 5, 20);
			continue;
		}
		//找到之后,这个人所对应的票数+1
		pStu->count++;
		for (i = 0; i < count; i++)
		{
			if(pStuArr[i] == pStu)
			{
				break;
			}
		}
		if(i == count)//说明未找到,则添加进候选人数组
		{
			pStuArr[count++] = pStu;
			if(count % 100 == 0)
			{
				pStuArr = (Student**)realloc(pStuArr, count + 100);
			}
		}
		//冒泡排序,票数
		for (i = 0; i < count - 1; i++)
		{
			for (j = i + 1; j < count; j++)
			{
				if(pStuArr[i]->count < pStuArr[j]->count)
				{
					pTmp = pStuArr[i];
					pStuArr[i] = pStuArr[j];
					pStuArr[j] = pTmp;
				}
			}
		}
		gotoxy(0, 7);//跳转到打印票数的那行
		//打印票数
		for (i = 0; i < count; i++)
		{
			if(i == 0)
			{
				setFontColor(LIGHTGREEN);
			}
			else
			{
				setFontColor(WHITE);
			}
			printf("%d %s\t%d\n", pStuArr[i]->num, pStuArr[i]->name, pStuArr[i]->count);
		}

	}
	free(pStuArr);
	
}
/************************************************************************/
/*导出学生信息(明文) */
/************************************************************************/
bool exportStudentInfo()
{
	Student* pStu;
	FILE* fp;

	pStu = G_pStuHead->pNext;

	if((fp = fopen(FILE_EXPORT, "w")) == NULL)
	{
		return false;
	}
	while (pStu != NULL)
	{
		fprintf(fp, "%d %s  %s %.2f %.2f %.2f %.2f\n", pStu->num, pStu->name, pStu->sex, 
							pStu->score[0], pStu->score[1], pStu->score[2], pStu->averge);
		pStu = pStu->pNext;
	}
	fclose(fp);
	return true;
}
/************************************************************************/
/*保存学生信息 (以数据库形式保存)*/
/************************************************************************/
bool saveStudentInfo()
{
	FILE *fp;
	Student *pStu;

	pStu = G_pStuHead;

	if((fp = fopen(FILE_DATABASE, "wb")) == NULL)
	{
		return false;
	}
	fwrite(&G_autoStore, sizeof(G_autoStore), 1, fp);
	while (pStu != NULL)
	{
		fwrite(pStu, sizeof(Student), 1, fp);
		pStu = pStu->pNext;
	}
	fclose(fp);
	return true;
}
/************************************************************************/
/*读取学生信息(读取数据库形式的文档) */
/************************************************************************/
bool loadStudentInfo()
{
	FILE *fp;
	int count;
	Student stu, *pStu, *pStuNew;

	count = 0;
	pStu = G_pStuHead;

	if((fp = fopen(FILE_DATABASE, "rb")) == NULL)
	{
		return false;
	}
	fread(&G_autoStore, sizeof(G_autoStore), 1, fp);//读取是否自动保存
	while(1)
	{
		fread(&stu, sizeof(Student), 1, fp);//读取文档中每个同学的数据
		if(feof(fp))//到文档尾则跳出
		{
			break;
		}
		if(count++ > 0)//这里 > 0 是因为保存的时候会把链表头保存进去,而链表头是没有有效数据的,所以要排除掉
		{
			pStuNew = (Student*)malloc(sizeof(Student));
			MoveMemory(pStuNew, &stu, sizeof(Student) - 4);//将结构体除指针外的所有数据拷贝进内存
			pStuNew->pNext = NULL;
			pStu->pNext = pStuNew;
			pStu = pStuNew;
		}
		if(stu.pNext == NULL)
		{
			break;
		}
	}
	fclose(fp);
	return true;
}
/************************************************************************/
/*学生信息排序 */
/************************************************************************/
bool sortStudentInfo()
{
	int order1, order2;
	bool swapData;
	char yesOrNo;
	Student* pStu1, *pStu2, tmpStu;

	pStu1 = G_pStuHead->pNext;
	if(pStu1 == NULL)
	{
		printf("系统中无学生信息!\n");
		system("pause");
		return false;
	}
	printf("输入以下序号执行相应功能(输入其他序号返回主菜单):\n");
	printf("1、根据学号进行排序\n");
	printf("2、根据姓名排序\n");
	printf("3、根据语文成绩排序\n");
	printf("4、根据数学成绩排序\n");
	printf("5、根据英语成绩排序\n");
	printf("6、根据平均分成绩排序\n");

	scanf("%d", &order1);
	if(order1 >= 1 && order1 <= 6)
	{
		printf("请选择正序OR倒序排列?(输入其他序号返回主菜单)\n");
		printf("1、正序排列\n");
		printf("2、倒序排列\n");
		scanf("%d", &order2);
		if(order2 >= 1 && order2 <= 2)
		{
			//冒泡排序
			for ( ; pStu1->pNext != NULL; pStu1 = pStu1->pNext)
			{
				for (pStu2 = pStu1->pNext; pStu2 != NULL; pStu2 = pStu2->pNext)
				{
					swapData = false;//是否交换数据
					switch(order1)
					{
					case 1://根据学号排序
						{
							if(order2 == 1 ? (pStu1->num > pStu2->num) : (pStu1->num < pStu2->num))//三目运算符, 判断正序还是倒序
							{
								swapData = true;
							}
							break;
						}
					case 2://根据姓名排序
						{
							if(order2 == 1 ? (strcmp(pStu1->name, pStu2->name) > 0) : (strcmp(pStu1->name, pStu2->name) < 0))
							{
								swapData = true;
							}
							break;
						}
					case 3://根据语文排序
					case 4://根据数学排序
					case 5://根据英语排序
						{
							if(order2 == 1 ? (pStu1->score[order1 - 3] > pStu2->score[order1 - 3]) : (pStu1->score[order1 - 3] < pStu2->score[order1 - 3]))
							{
								swapData = true;
							}
							break;
						}
					case 6://根据均分排序
						{
							if(order2 == 1 ? (pStu1->averge > pStu2->averge) : (pStu1->averge < pStu2->averge))
							{
								swapData = true;
							}
							break;
						}
					}

					if(swapData)
					{
						//交换内存数据,只需要将除pNext指针外的结构体数据拷贝交换即可
						MoveMemory(&tmpStu, pStu1, sizeof(Student) - 4);
						MoveMemory(pStu1, pStu2, sizeof(Student) - 4);
						MoveMemory(pStu2, &tmpStu, sizeof(Student) - 4);
					}
				}
			}
			printf("排序完成,是否显示?Y/N\n");
			getchar();//过滤掉输入时的换行符
			scanf("%c", &yesOrNo);
			if(yesOrNo == 'Y' || yesOrNo == 'y')
			{
				pStu1 = G_pStuHead->pNext;
				setFontColor(LIGHTGREEN);
				printf(LIST_TITLE_1);//显示列表标题头
				setFontColor(WHITE);
				//打印排序后的各个学生信息
				while(pStu1 != NULL)
				{
					printf("%d %s  %s %.2f %.2f %.2f %.2f\n", pStu1->num, pStu1->name, pStu1->sex, 
							pStu1->score[0], pStu1->score[1], pStu1->score[2], pStu1->averge);
					pStu1 = pStu1->pNext;
				}
				system("pause");
			}
			return true;

		}
	}
	return false;
}
/************************************************************************/
/*查询学生信息 */
/************************************************************************/
void searchStudentInfo()
{
	bool hasFound;
	int order, stuID, count, i, min, max;
	float score;
	char name[20];
	Student* pStu;
	Student** pStuArr;

	pStuArr = NULL;
	
	while (1)
	{
		system("CLS");
		showSystemTitle();

		if(pStuArr != NULL)//如果再次查询,这里需要判断,将上一轮查询的学生信息指针数组清空
		{
			free(pStuArr);
		}
		count = 0;
		stuID = 0;
		hasFound = false;
		pStu = G_pStuHead->pNext;
		pStuArr = (Student**)malloc(4 * 100);//初始化查询到后存放的学生信息指针数组

		printf("输入以下序号执行相应功能(输入其他序号返回主菜单):\n");
		printf("1、输入学号查询信息\n");
		printf("2、输入姓名查询信息\n");
		printf("3、输入语文成绩范围查询信息\n");
		printf("4、输入数学成绩范围查询信息\n");
		printf("5、输入英语成绩范围查询信息\n");
		printf("6、输入平均分范围查询信息\n");
		printf("7、列出所有学生信息\n");

		scanf("%d", &order);
		switch(order)
		{
		case 1://根据学号查询
			{
				printf("请输入要查询的学生学号:");
				scanf("%d", &stuID);
				while (pStu != NULL)
				{
					if(pStu->num == stuID)
					{
						hasFound = true;
						break;
					}
					pStu = pStu->pNext;
				}
				if(hasFound)//
				{
					setFontColor(LIGHTGREEN);
					printf(LIST_TITLE_1);
					setFontColor(WHITE);
					printf("%d %s  %s %.2f %.2f %.2f %.2f\n", pStu->num, pStu->name, pStu->sex, 
							pStu->score[0], pStu->score[1], pStu->score[2], pStu->averge);
				}
				
				break;
			}
		case 2://根据姓名查询
			{
				printf("请输入要查询的学生姓名:");
				scanf("%s", name);
				while (pStu != NULL)
				{
					if(strcmp(pStu->name, name) == 0)
					{
						hasFound = true;
						pStuArr[count++] = pStu;
						if(count % 100 == 0)
						{
							pStuArr = (Student**)realloc(pStuArr, count + 100);
						}
					}
					pStu = pStu->pNext;
				}
				if(hasFound)
				{
					setFontColor(LIGHTGREEN);
					printf(LIST_TITLE_1);
					setFontColor(WHITE);
					for (i = 0; i < count; i++)
					{
						printf("%d %s  %s %.2f %.2f %.2f %.2f\n", pStuArr[i]->num, pStuArr[i]->name, pStuArr[i]->sex, 
								pStuArr[i]->score[0], pStuArr[i]->score[1], pStuArr[i]->score[2], pStuArr[i]->averge);
					}

				}
				break;
			}
		case 3://根据语文成绩范围查询
		case 4://根据数学成绩范围查询
		case 5://根据英语成绩范围查询
		case 6://根据平均分范围查询
			{
				char *subjectStrArr[4] = {"语文", "数学", "英语", "平均"};
				printf("请输入要查询的%s成绩范围:", subjectStrArr[order - 3]);
				scanf("%d %d", &min, &max);
				while (pStu != NULL)
				{
					if(order < 6)// 3 ~ 5
					{
						score = pStu->score[order - 3];
					}
					else  //order = 6
					{
						score = pStu->averge;
					}
					if(score >= min && score <= max)
					{
						//找到符合条件的学生信息,则加入到指针数组中去
						hasFound = true;
						pStuArr[count++] = pStu;
						if(count % 100 == 0)
						{
							pStuArr = (Student**)realloc(pStuArr, count + 100);
						}
					}
					pStu = pStu->pNext;
				}
				if(hasFound)
				{
					setFontColor(LIGHTGREEN);
					printf(LIST_TITLE_1);
					setFontColor(WHITE);
					//打印指针数组中的学生信息
					for (i = 0; i < count; i++)
					{
						printf("%d %s  %s %.2f %.2f %.2f %.2f\n", pStuArr[i]->num, pStuArr[i]->name, pStuArr[i]->sex, 
							pStuArr[i]->score[0], pStuArr[i]->score[1], pStuArr[i]->score[2], pStuArr[i]->averge);
					}
					
				}
				break;
			}
		case 7://列出所有学生信息
			{
				hasFound = true;
				setFontColor(LIGHTGREEN);
				printf(LIST_TITLE_1);
				setFontColor(WHITE);
				while(pStu != NULL)
				{
					printf("%d %s  %s %.2f %.2f %.2f %.2f\n", pStu->num, pStu->name, pStu->sex, 
							pStu->score[0], pStu->score[1], pStu->score[2], pStu->averge);
					pStu = pStu->pNext;
				}
				break;
			}
		default:
			{
				goto lab_search;
			}
		}
		if(! hasFound)
		{
			printf("未能找到相应的学生信息!\n");
		}
		system("pause");
	}
lab_search:
	free(pStuArr);
}
/************************************************************************/
/*删除学生信息 */
/************************************************************************/
bool deleteStudentInfo()
{
	char yesOrNo;
	int stuID;
	bool hasFound;
	Student* pStu, *pStu1;

	hasFound = false;
	pStu = G_pStuHead->pNext;
	pStu1 = G_pStuHead;

	printf("请输入欲删除的学生学号:");
	scanf("%d", &stuID);
	while (pStu != NULL)
	{
		if(pStu->num == stuID)
		{
			hasFound = true;
			break;
		}
		pStu1 = pStu;
		pStu = pStu->pNext;
	}
	if(hasFound)
	{
		printf("找到此学生的信息如下:\n");
		setFontColor(LIGHTGREEN);
		printf(LIST_TITLE_1);
		setFontColor(WHITE);
		printf("%d %s  %s %.2f %.2f %.2f %.2f\n", pStu->num, pStu->name, pStu->sex, 
				pStu->score[0], pStu->score[1], pStu->score[2], pStu->averge);
		printf("是否删除?Y/N");
		getchar();//过滤掉输入时的换行符
		scanf("%c", &yesOrNo);
		if(yesOrNo == 'y' || yesOrNo == 'Y')
		{
			pStu1->pNext = pStu->pNext;
			free(pStu);
			printf("已删除\n");
		}
		else
		{
			hasFound = false;
		}
	}
	else
	{
		printf("未找到对应学生的信息\n");
	}
	system("pause");
	return hasFound;
}
/************************************************************************/
/*修改学生信息 */
/************************************************************************/
bool modifyStudentInfo()
{
	int order, count, i;
	int stuID;
	char name[20];
	char yesOrNo;
	bool hasModify;
	Student* pStu;
	Student** pStuArr;

	hasModify = false;
	count = 0;
	pStu = G_pStuHead->pNext;
	pStuArr = (Student**)malloc(4 * 100);//用于存放查找到的学生信息指针,这里定义指针数组是防止查询姓名出现重名

	printf("请输入以下序号,选择对应功能(1或2,否则返回上级菜单)\n");
	printf("1、输入学号查找学生\n");
	printf("2、输入姓名查找学生\n");
	scanf("%d", &order);

	if(order == 1)
	{
		printf("请输入要修改的学生学号:\n");
		scanf("%d", &stuID);
		while(pStu != NULL)
		{
			if(pStu->num == stuID)
			{
				pStuArr[count++] = pStu;
				break;
			}
			pStu = pStu->pNext;
		}
	}
	else if(order == 2)
	{
		printf("请输入要修改的学生姓名:\n");
		scanf("%s", name);
		while(pStu != NULL)
		{
			if(strcmp(pStu->name, name) == 0)
			{
				pStuArr[count++] = pStu;
				if(count % 100 == 0)//如果数组存放满了,则再次申请内存
				{
					pStuArr = (Student**)realloc(pStuArr, count + 100);
				}
			}
			pStu = pStu->pNext;
		}
	}
	else
	{
		return false;
	}
	if(count == 0)
	{
		printf("未能找到任何信息,是否继续修改?Y/N");
		getchar();//过滤掉输入时的换行符
		scanf("%c", &yesOrNo);
		if(yesOrNo == 'y' || yesOrNo == 'Y')
		{
			system("CLS");
			showSystemTitle();
			return modifyStudentInfo();
		}
	}
	else
	{
		printf("为您查找到%d个学生信息:\n   ", count);
		setFontColor(LIGHTGREEN);
		printf(LIST_TITLE);
		setFontColor(WHITE);
		for (i = 0; i < count; i++)
		{
			printf("%d、%d %s  %s %.2f %.2f %.2f\n", i + 1, pStuArr[i]->num, pStuArr[i]->name, pStuArr[i]->sex, 
				pStuArr[i]->score[0], pStuArr[i]->score[1], pStuArr[i]->score[2]);
		}

		printf("请输入您要修改的信息序号(1~%d),其他数字返回主菜单\n", count);
		scanf("%d", &order);
		if(order >= 1 && order <= count)
		{
			printf("请依次输入\n");
			setFontColor(LIGHTGREEN);
			printf(LIST_TITLE);
			setFontColor(WHITE);
			pStu = pStuArr[order - 1];
			scanf("%d %s %s %f %f %f", &pStu->num, pStu->name, pStu->sex, &pStu->score[0], &pStu->score[1], &pStu->score[2]);
			pStu->averge = (pStu->score[0] + pStu->score[1] + pStu->score[2]) / 3;
			hasModify = true;
		}
	}
	free(pStuArr);
	return hasModify;
}
/************************************************************************/
/*检测学号是否存在*/
/************************************************************************/
bool checkStuIDExist(int stuID)
{
	Student* pStu;

	pStu = G_pStuHead->pNext;

	while(pStu != NULL)
	{
		if(pStu->num == stuID)
		{
			return true;
		}
		pStu = pStu->pNext;
	}
	return false;
}
/************************************************************************/
/*添加学生信息 */
/************************************************************************/
bool addStudentInfo()
{
	printf("输入-1回车,返回上级菜单\n");
	setFontColor(LIGHTGREEN);
	printf(LIST_TITLE);
	setFontColor(WHITE);

	char c;
	bool hasAdd = false;
	Student* pStu = G_pStuHead;
	Student* pStuNew;

	while (pStu->pNext != NULL)
	{
		pStu = pStu->pNext;
	}

	while(1)
	{
		pStuNew = (Student*)malloc(sizeof(Student));
		scanf("%d", &pStuNew->num);
		if(pStuNew->num == -1)//输入-1返回主菜单
		{
			while ((c = getchar()) != EOF && c != '\n');//不停地使用getchar()获取缓冲中字符,直到获取的c是“\n”或文件结尾符EOF为止
			free(pStuNew);
			return hasAdd;
		}
		else if(checkStuIDExist(pStuNew->num))
		{
			while ((c = getchar()) != EOF && c != '\n');//不停地使用getchar()获取缓冲中字符,直到获取的c是“\n”或文件结尾符EOF为止
			printf("该学号已存在,请重新输入!\n");
			free(pStuNew);
			continue;
		}
		hasAdd = true;
		scanf("%s %s %f %f %f", pStuNew->name, pStuNew->sex, &pStuNew->score[0], 
			&pStuNew->score[1], &pStuNew->score[2]);
		pStuNew->averge = (pStuNew->score[0] + pStuNew->score[1] + pStuNew->score[2]) / 3;
		pStuNew->pNext = NULL;
		pStu->pNext = pStuNew;
		pStu = pStuNew;
	}

	return hasAdd;
}
/************************************************************************/
/*根据指令序号执行对应功能  */
/************************************************************************/
bool orderToExecute(int order)
{
	bool succ;

	succ = false;
	if(order != Func_Save && order != Func_AutoSave && order!= Func_Export)
	{
		system("CLS");
		showSystemTitle();
	}

	switch (order)
	{
	case Func_Add://添加
		{
			succ = addStudentInfo();
			break;
		}
	case Func_Delete://删除
		{
			succ = deleteStudentInfo();
			break;
		}
	case Func_Modify://修改
		{
			succ = modifyStudentInfo();
			break;
		}
	case Func_Search://搜索
		{
			searchStudentInfo();
			break;
		}
	case Func_Sort://排序
		{
			succ = sortStudentInfo();
			break;
		}
	case Func_Save://保存
		{
			succ = saveStudentInfo();
			if(succ)
			{
				gotoxy(42, Func_Save + 3);
				setFontColor(LIGHTGREEN);
				printf("保存成功!");
				setFontColor(WHITE);
				gotodelete(0, Func_ExitSystem + 4, 2);
				gotoxy(0, Func_ExitSystem + 4);
			}
			return false;
		}
	case Func_AutoSave://设置自动保存
		{
			G_autoStore = ! G_autoStore;
			printAutoStoreInfo();
			orderToExecute(Func_Save);//保存配置
			break;
		}
	case Func_Vote://投票系统
		{
			voteSystem();
			break;
		}
	case Func_Export://导出所有学生信息(明文)
		{
			succ = exportStudentInfo();
			gotoxy(17, Func_Export + 3);
			setFontColor(LIGHTGREEN);
			if(succ)
			{
				printf("导出成功!");
			}
			else
			{
				printf("导出失败!");
			}
			setFontColor(WHITE);
			gotodelete(0, Func_ExitSystem + 4, 2);
			gotoxy(0, Func_ExitSystem + 4);
			return false;
		}
	default:
		{
			
			break;
		}
	}
	return succ;
}

int main(int argc, char* argv[])
{
	int order;
	bool succ;

	system("title 学生管理系统 by 机智蛋");
	order = 0;
	succ = false;
	G_pStuHead = (Student*)malloc(sizeof(Student));
	G_pStuHead->pNext = NULL;

	loadStudentInfo();

	while(1)
	{
		if(order != Func_Save && order != Func_AutoSave && order != Func_Export)//当输入这几个指令时不需要初始化屏幕
		{
			initScreen();
		}
		if(succ && order != Func_Save && G_autoStore)//执行自动保存
		{
			orderToExecute(Func_Save);
		}
		succ = false;

		do 
		{
			scanf("%d", &order);
			if(order >= Func_Add && order <= Func_ExitSystem)
			{
				break;
			}
			else
			{
				printf("指令错误,请重新输入\n");
			}
		} while (1);
		
		if(order == Func_ExitSystem)//退出
		{
			printf("欢迎下次继续使用,拜拜~~\n");
			return 0;
		}

		succ = orderToExecute(order);//执行功能

		
	}
	
	return 0;
}


全篇完~ 码字不易,兄弟们给个赞再走吧~ 在这里插入图片描述

  • 14
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include <stdio.h> #include <stdlib.h> #include <string.h> #include<ctype.h> #define M 3 struct student {long num; char name[20]; char sex; int score[5]; int ave; }stu[M]; void init() { printf("\t\t********************************\n\n"); printf("\t\tStudent score management systerm \n"); printf("\n"); printf("\t\t Make by Maggiehe \n"); printf("\n"); printf("\t\t Student number is 3206***** \n\n"); printf("\t\t********************************\n\n"); } void input() { int i,j; printf("\nInput the student number name sex math English PE maolun majing:\n"); for(i=0;i<M;i++) {scanf("%ld,%s,%c",&stu[i].num,stu[i].name,&stu[i].sex); for(j=0;j<5;j++) scanf("%d",&stu[i].score[j]); scanf("%d",&stu[i].ave); save(); } } void list() { int i,j; clrscr(); printf("\t**********************student****************************\n"); printf("num name sex math English PE maolun majing:\n"); for(i=0;i<M;i++) { printf("%ld %s %c",stu[i].num,stu[i].name,stu[i].sex); for(j=0;j<5;j++) printf("%d",stu[i].score[j]); printf("%d",stu[i].ave); } if((i+1)%10==0) { printf("\npress any key continue.......\n"); getch(); } printf("\t*************************end*********************************\n"); } void average() { int i,j; float sum[M]; for(i=0;i<M;i++) for(j=0;j<5;j++) sum[i]+=stu[i].score[j]; stu[i].ave=sum[i]/5.0; } void search_num() { long num; int i,j; printf("\nplease enter number which you want to search:"); scanf("%ld",&num); printf("\n"); for(i=0;i<M;i++) if(stu[i].num!=num) printf("The number is not exist\n"); else { printf("number name sex math English PE maolun majing average\n"); printf("%ld %s %c",stu[i].num,stu[i].name,stu[i].sex); for(j=0;j<5;j++) printf("%d",stu[i].score[j]); printf("%d",stu[i].ave); } } void search_nam() { int i,j; char name[20]; printf("\nplease enter name which you want to search:"); scanf("%s",&name); printf("\n"); for(i=0;i<M;i++) if(strcmp(stu[i].name,name)==0) { printf("number name sex math English PE maolun majing average:\n"); printf("%ld%s%c",stu[i].num,stu[i].name,stu[i].sex); for(j=0;j<5;j++) printf("%d",stu[i].score[j]); printf("%d",stu[i].ave); } else printf("\nThe name is not exist!!!\n"); } void search_ave() { int i,j,a=0; float k; printf("please enter average score which you want to search:"); scanf("%f",&k); printf("\n"); for(i=0;i<M;i++) if(stu[i].ave!=k) printf("The average score is not exist!!!\n"); else for(i=0;i<M;i++) { if(stu[i].ave<=k) printf("number name sex math English PE maolun majing average\n"); printf("%ld %s %c",stu[i].num,stu[i].name,stu[i].sex); for(j=0;j<5;j++) printf("%d",stu[i].score[j]); printf("%d",stu[i].ave); a++; } } void sort() { int i,j; struct student temp; for(i=0;i<M;i++) for(j=i+i;j<M;j++) if(stu[i].ave<stu[j].ave) {temp=stu[i]; stu[i]=stu[j]; stu[j]=temp; } } void save() { int i; FILE *fp; if((fp=fopen("student.dat","wb"))==NULL) {printf("cannot open file\n"); return; } for(i=0;i<M;i++) if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1) printf("file write error\n"); fclose(fp); } void delect() { int i,t; long num; printf("please enter number which you want to delect:"); scanf("%ld",&num); printf("\n"); for(i=0;i<M;i++) { if(stu[i].num!=num) printf("The number is not exist!!!\n"); if(stu[i].num==num) t=i; } for(i=t;i<M;i++) stu[i]=stu[i+1]; save(); } void insert() {FILE *fp; int i,n,t; struct student s; printf("please input record\n"); printf("number name sex math English PE maolun majing average\n"); scanf("%ld,%s,%c,%d%d%d%d%d%d",&s.num,s.name,&s.sex,&s.score[0],&s.score[1],&s.score[2],&s.score[3],&s.score[4],&s.ave); s.ave=(s.score[0]+s.score[1]+s.score[2]+s.score[3]+s.score[4])/3.0; if((fp=fopen("student.dat","wb"))==NULL) {printf("cannot open file\n"); return; } i=M; for(t=0;stu[i].ave>s.ave&&t<M;t++) if(fwrite(&s,sizeof(struct student),1,fp)!=1) printf("file write error\n"); fclose(fp); } void MainMenu() {clrscr(); printf("\t********************************************************************\n \n); Printf(“\n”); printf("\t choose one of following:\n"); printf("\t 1. search record\n"); printf("\t 2. sort record\n"); printf("\t 3. insert record\n"); printf("\t 4. delect record\n"); printf("\t please enter your choice:"); printf(“\n”); printf("\t********************************************************************\n"); } void menu_select() {clrscr(); printf("\t******************************************************************\n\n"); printf(“\n”); printf("\t choose one of following:\n"); printf("\t 1.1 search by number\n"); printf("\t 1.2 search by name\n"); printf("\t 1.3 search by average score\n"); printf("\t please enter your choice:"); printf(“\n”); printf("\t******************************************************************\n"); } void search() { char ch; clrscr(); menu_select(); ch=getch(); switch(ch) { case '1':search_num();break; case '2':search_nam();break; case '3':search_ave();break; } } main() { char ch; clrscr(); init(); input(); list(); average(); save(); MainMenu(); ch=getch(); switch(ch) { case '1':search();break; case '2':sort();break; case '3':insert();break; case '4':delect();break; } search_num(); search_nam(); search_ave(); sort(); delect(); insert; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值