学生管理系统

本文介绍了一个使用双向链表实现的学生信息管理系统的详细设计与实现过程,包括添加、删除、查找、修改学生信息等功能,并提供了文件存储和加载的支持。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

linktable.h

class StudentInfo
{
public:
	char code[6];
	float chinesescore;
	float mathscore;
	StudentInfo* next;
	StudentInfo* prior;
};

//添加学生信息,如果prior为NULL,则newinfo为头结点
StudentInfo* AddInfo(StudentInfo* prior,StudentInfo* newinfo);
//根据编号删除学生信息
bool DelStudentInfo(char* code,StudentInfo*& pHead);
//根据编号查找学生信息
StudentInfo* FindStudentInfoByCode(char* code,StudentInfo* pHead);
//修改学生成绩信息
void ModifyStudentInfo(StudentInfo* p);
//从头结点开始打印学生信息
void PrintStudentInfo(StudentInfo* pHead);
//对指定的节点输入学生信息
void InputInfo(StudentInfo* pInfo);
//获取最后一个节点
StudentInfo* GetLastStudentInfo(StudentInfo* pHead);
//保存链表到文件,顺序保存
void SaveStudentInfoToFile(char* filename,StudentInfo* pHead);
//从文件中读取链表
void LoadStudentInfoFromFile(char* filename,StudentInfo*& pHead);
//操作提示
void ShowOperateInfo(void);


students.h

#include "linkTable.h"
#include <stdio.h>
#include <string.h>

//添加学生信息,如果prior为NULL,则newinfo为头结点
StudentInfo* AddInfo(StudentInfo* prior,StudentInfo* newinfo)
{
	if (!prior)
	{
		newinfo->next = NULL;
		newinfo->prior = NULL;
	}
	else
	{
		prior->next = newinfo;
		newinfo->prior = prior;
		newinfo->next = NULL;
	}
	memset(newinfo->code,0,sizeof(newinfo->code));
	InputInfo(newinfo);
	return newinfo;
}

//根据编号删除学生信息
bool DelStudentInfo(char* code,StudentInfo*& pHead)
{
	StudentInfo* pDel = FindStudentInfoByCode(code,pHead);
	if (pDel == NULL)
		return false;
	else
	{
		if (pDel == pHead)
			pHead = pDel->next;
		else
		{			
			pDel->prior->next = pDel->next;
			if (pDel->next != NULL)
				pDel->next->prior = pDel->prior;
		}
		delete pDel;
		pDel = NULL;
		return true;
	}
}

//根据编号查找学生信息
StudentInfo* FindStudentInfoByCode(char* code,StudentInfo* pHead)
{
	StudentInfo* p = pHead;
	while (p != NULL)
	{
		if (strcmp(p->code,code) == 0)
		{
			return p;
		}
		p = p->next;
	}
	return NULL;
}
//修改学生成绩信息
void ModifyStudentInfo(StudentInfo* p)
{
	printf("%s号学生当前信息:语文:%f,数学:%f\n",p->code,p->chinesescore,p->mathscore);
	printf("%s","请输入修改后的语文成绩:");
	scanf("%f",&(p->chinesescore));
	printf("%s","请输入修改后的数学成绩:");
	scanf("%f",&p->mathscore);
	printf("修改完毕,%s号学生最新信息:语文:%f,数学:%f\n",p->code,p->chinesescore,p->mathscore);
}

//从头结点开始打印学生信息
void PrintStudentInfo(StudentInfo* pHead)
{
	StudentInfo* p = pHead;
	while (p!=NULL)
	{
		printf("%s号学生:语文:%f,数学:%f\n",p->code,p->chinesescore,p->mathscore);
		p = p->next;
	}
}

//对指定的节点输入学生信息
void InputInfo(StudentInfo* pInfo)
{
	printf("%s","请输入学生编号:");
	scanf("%s",pInfo->code);
	printf("%s","请输入语文成绩:");
	scanf("%f",&pInfo->chinesescore);
	printf("%s","请输入数学成绩:");
	scanf("%f",&pInfo->mathscore);
}

//获取最后一个节点
StudentInfo* GetLastStudentInfo(StudentInfo* pHead)
{
	if (pHead == NULL)
		return NULL;
	StudentInfo* p = pHead;
	while (p)
	{
		if (p->next == NULL)
			return p;
		p = p->next;
	}
}

//保存链表到文件,顺序保存
void SaveStudentInfoToFile(char* filename,StudentInfo* pHead)
{
	FILE* f;
	f= fopen(filename,"w");
	StudentInfo* p = pHead;
	while (p)
	{
		fwrite(p->code,sizeof(char),sizeof(p->code),f);
		fwrite(&p->chinesescore,sizeof(float),1,f);
		fwrite(&p->mathscore,sizeof(float),1,f);
		//fwrite("\n",sizeof(char),1,f);
		p=p->next;
	}
	//fwrite("/0",sizeof(char),1,f);
	fclose(f);
}

//从文件中读取链表
void LoadStudentInfoFromFile(char* filename,StudentInfo*& pHead)
{
	FILE* f;
	f= fopen(filename,"r");
	if (f == NULL)
		return;
	fseek(f, 0, SEEK_SET); 
	StudentInfo* p = NULL;
	StudentInfo* newp = NULL;
	int c;
	//c = fgetc(f);
	while (!feof(f))
	{
		//因为不能完全依赖feof判断文件是否到结尾,所以用fgetc的返回值来判断,
		//当读取到结尾时,feof实际并没有获得到达结尾的信息,所以这里用fgetc继续读取一个字符,来判断是否结束
		//如果不是-1(结尾),那么回退一个文件位置,如果是-1,退出
		c = fgetc(f);
		if (c != -1)
			fseek(f, -1, SEEK_CUR);
		else
			return;
		if (pHead == NULL)
		{
			pHead = new StudentInfo;
			pHead->next = NULL;
			pHead->prior = NULL;
			memset(pHead->code,0,sizeof(pHead->code));
			fread(pHead->code,sizeof(char),sizeof(pHead->code),f);
			fread(&pHead->chinesescore,sizeof(float),1,f);
			fread(&pHead->mathscore,sizeof(float),1,f);
			p = pHead;
		}
		else
		{
			newp = new StudentInfo;
			newp->next = NULL;
			newp->prior = NULL;
			newp->prior = p;
			memset(newp->code,0,sizeof(newp->code));
			fread(newp->code,sizeof(char),sizeof(newp->code),f);
			fread(&newp->chinesescore,sizeof(float),1,f);
			fread(&newp->mathscore,sizeof(float),1,f);
			p->next = newp;
			p = newp;
		}	
		
	}
	fclose(f);
}

//操作提示
void ShowOperateInfo(void)
{
	printf("%s\n","1:增加信息");
	printf("%s\n","2:删除信息");
	printf("%s\n","3:查找信息");
	printf("%s\n","4:修改信息");
	printf("%s\n","5:保存信息");
	printf("%s\n","9:打印信息");
	printf("%s\n","0:结束");
}


main

#include "linkTable.h"
#include "stdio.h"
#include "stdlib.h"

int main()
{
	ShowOperateInfo();
	int i;
	//curInfo当前节点,用来添加节点用
	StudentInfo* curInfo = new StudentInfo;
	StudentInfo* pHead = NULL;	
	LoadStudentInfoFromFile("e://link.txt",pHead);
	//将curInfo指向最后一个节点
	if (pHead != NULL)
		curInfo = GetLastStudentInfo(pHead);
	char code[10]={0};
	while (true)
	{
		printf("%s","请输入操作号:");
		scanf("%d",&i);
		switch (i)
		{
			//退出
		case 0:
			exit(0);
			//添加
		case 1:
			if (pHead == NULL)
			{
				pHead = new StudentInfo;	
				curInfo = pHead = AddInfo(NULL,pHead);
			}
			else
			{
				StudentInfo* newInfo = new StudentInfo;
				curInfo = AddInfo(curInfo,newInfo);
			}
			break;
			//删除
		case 2:			
			printf("%s","请输入要删除的学生编号:");
			scanf("%s",code);
			DelStudentInfo(code,pHead);
			curInfo = GetLastStudentInfo(pHead);
			break;
			//查找
		case 3:
			printf("%s\n","请输入要查找的学生编号:");
			scanf("%s",code);
			{
				StudentInfo* p = FindStudentInfoByCode(code,pHead);
				if (p == NULL)
					printf("编号%s不存在",code);
				else
				{
					printf("%s号学生:语文:%f,数学:%f\n",p->code,p->chinesescore,p->mathscore);
				}
			}
			break;
			//修改
		case 4:
			printf("%s\n","请输入要修改的学生编号:");
			scanf("%s",code);
			{
				StudentInfo* p = FindStudentInfoByCode(code,pHead);
				if (p == NULL)
					printf("编号%s不存在",code);
				else
				{
					ModifyStudentInfo(p);
				}
			}
			break;
		case 5:
			SaveStudentInfoToFile("e://link.txt",pHead);
			break;
			//打印
		case 9:
			PrintStudentInfo(pHead);
			break;
		default:
			exit(0);
		}
	}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值