简单的学生信息管理系统

做这东西纯粹是无聊(花了一下午帮同学写的),没啥营养。各个函数都注释好了,很容易看懂。

有些地方写的不太合适也懒得改了,凑合看吧

好了少废话直接贴代码。。。

PS:vs2013和codeblock都编译通过,vc6编译出一大堆错误(无视吧o(╯□╰)o)

主函数:

#define _CRT_SECURE_NO_WARNINGS
#include
#include"函数接口.h"
int main()
{
	Show_Menu();
	system("pause");
	return 0;
}
接口函数:

#include
#include
#include
#define M 3
typedef struct student
{
	char number[20];
	char name[20];
	int score[M];
	int sum;
	struct student *pNext;
}Stu, *pStu;
pStu Head = NULL; // 头结点
pStu *Addr = NULL;	// 指针数组保存每一个节点的地址
// 函数位置不能放错了,否则编译要出问题!!!
void Show_Menu();
void Return_Home_Page();
// 添加结点
pStu Add_New_Node(char *name,char *number,int *score)
{
	int i,sum=0;
	pStu NewNode = (pStu)malloc(sizeof(Stu));
	strcpy(NewNode->name, name);
	strcpy(NewNode->number, number);
	for (i = 0; i < M; i++)
	{
		NewNode->score[i] = score[i];
		sum += score[i];
	}
	NewNode->sum = sum;
	NewNode->pNext = NULL;
	return NewNode;
}
// 创建数据表
void Create_Link_List(pStu *pHead, char *name, char *number, int *score)
{
	if (*pHead == NULL)
	{
		*pHead = Add_New_Node(name, number, score);
	}
	else
	{
		pStu p = *pHead;
		while (p->pNext != NULL)
		{
			p = p->pNext;
		}
		p->pNext = Add_New_Node(name, number, score);
	}
}
// 登记函数
void Input_Stu_Data(void)
{
	int rs,i,j;
	char tmp_name[20] = { 0 };
	char tmp_number[20] = { 0 };
	int tmp_score[M] = { 0 };
	system("cls");
	printf("请输入需要输入几个学生信息: ");
	scanf("%d", &rs);
	for (i = 0; i < rs; i++)
	{
		printf("请输入第%d个学生的学号: ", i + 1);
		scanf("%s", tmp_number);
		printf("请输入学生姓名: ");
		scanf("%s", tmp_name);
		for (j = 0; j < M; j++)
		{
			printf("请输入第%d门课成绩: ",j + 1);
			scanf("%d", &tmp_score[j]);
		}
		Create_Link_List(&Head, tmp_name, tmp_number, tmp_score);
		printf("\n");
	}
	Return_Home_Page();
}
// 浏览函数
void Look_Through_Stu_Data(pStu pHead)
{
	system("cls");
	if (pHead == NULL)
	{
		printf("系统里没有任何学生的信息!\n");
		Return_Home_Page();
	}
	int i, k = 1;
	pStu p = pHead;
	while (p != NULL)
	{
		printf("第%d个学生学号为:%s\n", k, p->number);
		printf("第%d个学生姓名为:%s\n", k, p->name);
		for (i = 0; i < M; i++)
		{
			printf("第%d个学生的第%d门课的成绩:%d\n", k, i + 1, p->score[i]);
		}
		printf("第%d个学生的总成绩为:%d\n\n", k, p->sum);
		k++;
		p = p->pNext;
	}
	Return_Home_Page();
}
// 打印出找到的学生的数据
void Print_One_Stu_Data(pStu p)
{
	printf("您所查找学生的信息为:\n");
	printf("----学号 -----姓名----英语成绩----高数成绩----计算机成绩----\n");
	printf("-----%s-------%s--------%d---------%d----------%d---------\n", p->number, p->name, 
		p->score[0],p->score[1], p->score[2]);
}
// 获取表中节点的个数
int Get_Count_Of_List(pStu pHead)
{
	if (pHead == NULL)
	{
		printf("系统里没有任何学生的信息!\n");
		return 0;
	}
	int n = 0;
	pStu p = pHead;
	while (p != NULL)
	{
		n++;
		p = p->pNext;
	}
	return n;
}
// 排序
void Sort_List(pStu pHead)
{
	// 用指针数组的方式保存每个节点的地址,直接对地址所指向的
	// 值进行排序,避免直接对结构体排序,(数据反复拷贝太浪费时间了)
	system("cls");
	if (pHead == NULL)
	{
		printf("表空无法排序\n");
		return;
	}
	int i, j = 0, n = Get_Count_Of_List(pHead);
	Addr = (pStu*)malloc(sizeof(pStu *)* n); // 动态分配数组(二级指针)
	pStu p = pHead;
	while (p != NULL)
	{
		Addr[j++] = p;
		p = p->pNext;
	}
	for (i = 0; i < n - 1; i++)
	{
		for (j = i + 1; j < n; j++)
		{
			if (Addr[i]->sum < Addr[j]->sum)
			{
				pStu temp = Addr[i];
				Addr[i] = Addr[j];
				Addr[j] = temp;
			}
		}
	}
	printf("按照总成绩排序后信息为:\n");
	printf("----学号----姓名----总成绩----\n");
	for (i = 0; i < n; i++)
	{
		printf("-----%s-------%s-------%d-----\n", Addr[i]->number, Addr[i]->name, Addr[i]->sum);
	}
	// 用完了释放内存避免内存泄露
	free(Addr);
	Return_Home_Page();
}
// 通过学号查找
pStu Search_Data_By_Number(pStu pHead,char *number,int *flag)
{
	if (pHead == NULL)
	{
		return NULL;
	}
	pStu p = pHead;
	while (p != NULL)
	{
		if (0 == strcmp(p->number, number))
		{
			*flag = 1;
			return p;
		}
		p = p->pNext;
	}
	return NULL;
}
// 通过姓名查找
pStu Search_Data_By_Name(pStu pHead, char *name,int *flag)
{
	if (pHead == NULL)
	{
		return NULL;
	}
	pStu p = pHead;
	while (p != NULL)
	{
		if (0 == strcmp(p->name, name))
		{
			*flag = 1;
			return p;
		}
		p = p->pNext;
	}
	return NULL;
}
// 通过分数查找
void Search_Data_By_Score(pStu pHead,int sum)
{
	int flag = 0;
	if (pHead == NULL)
	{
		printf("学生数据为空无法查找\n");
		return ;
	}
	pStu p = pHead;
	while (p != NULL)
	{
		if (p->sum == sum)
		{
			flag = 1;
			Print_One_Stu_Data(p);
		}
		p = p->pNext;
	}
	if (!flag)
		printf("不好意思找遍了,没找着\n");
}
// 删除一个数据
int Delete_One_Data(pStu *pHead, char *number)
{
	int flag = 0;
	if (*pHead == NULL)
	{
		return 0;
	}
	pStu p = *pHead;
	if (0 == strcmp(p->number, number))
	{
		*pHead = p->pNext;
		free(p);
		return 1;
	}
	else
	{
		while (p->pNext!=NULL)
		{
			if (0 == strcmp(p->pNext->number, number))
			{
				flag = 1;
				break;
			}
			p = p->pNext;
		}
		if (flag)
		{
			pStu p1 = p->pNext->pNext;
			pStu temp = p->pNext;
			p->pNext = p1;
			free(temp);
			return 1;
		}
	}
	return 0;
}
// 删除所有数据
int Free_All_List(pStu *pHead)
{
	if (*pHead == NULL)
	{
		return 0;
	}
	pStu p = *pHead,p1=NULL;
	p1 = p->pNext;
	while (p1!=NULL)
	{
		p = p1;
		p1 = p1->pNext;
		free(p);
	}
	free(*pHead);
	*pHead = NULL;
	return 1;
}
// 删除函数
void Delete_Stu_Data()
{
	char Delete_Info[20] = { 0 };
	system("cls");
	printf("删除全部学生信息请输入\"all\",删除指定学号的学生信息请输入\"one\"\n");
	scanf("%s", Delete_Info);
	if (0 == strcmp(Delete_Info, "all"))
	{
		if (Free_All_List(&Head))
			printf("删除成功!\n");
		else
			printf("删除失败\n");
	}
	else if (0 == strcmp(Delete_Info, "one"))
	{
		char buffer[20] = { 0 };
		printf("请输入你要删除的人的学号:");
		scanf("%s", buffer);
		if (Delete_One_Data(&Head, buffer))
			printf("删除成功!\n");
		else
			printf("输入数据有误删除失败!\n");
	}
	else
	{
		printf("输入的数据有误!\n");
		Delete_Stu_Data();
	}
	Return_Home_Page();
}
// 保存文件
void Save_Data(pStu pHead)
{
	if (pHead == NULL)
	{
		printf("空表,无法写入到文件!\n");
		Return_Home_Page();
	}
	FILE *pfr = fopen("card.dat", "wb");
	if (pfr == NULL)
	{
		printf("not open!\n");
		exit(0);
	}
	// 排序存盘
	int i,j=0,n = Get_Count_Of_List(pHead);
	Addr = (pStu*)malloc(sizeof(pStu *)* n); // 动态分配数组(二级指针)
	pStu p = pHead;
	while (p != NULL)
	{
		Addr[j++] = p;
		p = p->pNext;
	}
	for (i = 0; i < n - 1; i++)
	{
		for (j = i + 1; j < n; j++)
		{
			if (Addr[i]->sum < Addr[j]->sum)
			{
				pStu temp = Addr[i];
				Addr[i] = Addr[j];
				Addr[j] = temp;
			}
		}
	}
	for (i = 0; i < n; i++)
	{
		fprintf(pfr, "%s %s", Addr[i]->number, Addr[i]->name);
		for (j = 0; j < M; j++)
			fprintf(pfr, " %d", Addr[i]->score[j]);
		fprintf(pfr, "\r\n");
	}
	if (ferror(pfr))
	{
		fclose(pfr);
		printf("写文件失败!\n");
		return;
	}
	printf("写文件成功!\n");
	fclose(pfr);
	free(Addr);
	Return_Home_Page();
}
// 从文件中读取(导出文件)
void Export_Data(pStu *pHead)
{
	int i;
	FILE *pfr = fopen("card.dat", "rb");
	if (pfr == NULL)
	{
		printf("not open!\n");
		exit(0);
	}
	while (!feof(pfr))
	{
		pStu temp = (pStu)malloc(sizeof(Stu));
		memset(temp, 0, sizeof(temp));
		fscanf(pfr, "%s%s%d%d%d", temp->number, temp->name, &(temp->score[0]),
			&(temp->score[1]), &(temp->score[2]));
		int buffer[M] = { 0 };
		for (i = 0; i < M; i++)
			buffer[i] = temp->score[i];	
		if (feof(pfr)) // 读取结束跳出循环
			break;
		Create_Link_List(&(*pHead),temp->name ,temp->number , buffer);
	}
	printf("文件打开成功!\n");
	fclose(pfr);
	Return_Home_Page();
}
// 显示菜单
void Show_Menu()
{
	int i,n,flag,n1=0,sum=0;
	pStu temp = NULL;
	char buffer[20], tp[20];
	const char *table[] = { "学号", "姓名", "成绩" };
	system("cls");
	printf("			学生信息管理系统\n");
	printf("				作者:(GadyPu)\n");
	printf("--------------------Menu-------------------------\n");
	printf("		1.登记学生信息\n");
	printf("		2.删除学生信息\n");
	printf("		3.浏览所有已登记的学生\n");
	printf("		4.查找\n");
	printf("			4.1按学号查找\n");
	printf("			4.2按姓名查找\n");
	printf("			4.3按成绩查找\n");
	printf("		5.根据总成绩排序\n");
	printf("		6.存储到文件\n");
	printf("		7.从文件导出\n");
	printf("		8.退出系统\n");
Sign:printf(" 请选择 ");
	scanf("%d", &n);
	switch (n)
	{
	case 1:
		Input_Stu_Data();
		break;
	case 2:
		Delete_Stu_Data();
		break;
	case 3:
		Look_Through_Stu_Data(Head);
		break;
	case 4:
		printf("三种查找方式:学号,姓名,成绩,请输入查找方式:");
		scanf("%s", tp);
		for (i = 0; i < sizeof(table) / sizeof(table[0]); i++)
		{
			if (0 == strcmp(tp, table[i]))
			{
				n1 = i + 1;
				break;
			}
		}
		switch (n1)
		{
		case 1:
			flag = 0;
			memset(buffer, 0, sizeof(buffer));
			printf("请输入需要查找学生的学号:");
			scanf("%s", buffer);
			temp = Search_Data_By_Number(Head, buffer, &flag);
			if (0 == flag )
				printf("查无此数据!\n");
			else
				Print_One_Stu_Data(temp);
			Return_Home_Page();
			break;
		case 2:
			flag = 0;
			memset(buffer, 0, sizeof(buffer));
			printf("请输入需要查找学生的姓名:");
			scanf("%s", buffer);
			temp=Search_Data_By_Name(Head,buffer,&flag);
			if (0 == flag )
				printf("查无此数据!\n");
			else
				Print_One_Stu_Data(temp);
			Return_Home_Page();
			break;
		case 3:
			printf("按成绩查找,请输入成绩:");
			scanf("%d", &sum);
			Search_Data_By_Score(Head,sum);
			Return_Home_Page();
			break;
		default:
		{
				   printf("你输入的有误,请重新输入\n");
				   Return_Home_Page();
		}
		}
		break;
	case 5:
		Sort_List(Head);
		break;
	case 6:
		Save_Data(Head);
		break;
	case 7:
		Export_Data(&Head);
		break;
	case 8:
		exit(0);
		break;
	default:
	{
			   printf("请输入1-8直接的数字,谢谢!\n");
			   goto Sign;
	}
	}
}
void Return_Home_Page()
{
	char sign[20] = { 0 };
	printf("\n");
	printf("还需要操作么?如果需要请输入:yes,否则输入:no\n");
	scanf("%s", sign);
	if (0 == strcmp(sign, "yes"))
	{
		Show_Menu();
	}
	else if (0 == strcmp(sign, "no"))
	{
		exit(0);
	}
	else
	{
		printf("请输入正确的字符,谢谢\n");
		// 递归调用
		Return_Home_Page();
	}
}
简单的学生信息管理系统_第1张图片

你可能感兴趣的:(C语言学习笔记)