⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 例2--图书管理--代码.cpp

📁 图书馆管理系统的实现
💻 CPP
📖 第 1 页 / 共 3 页
字号:
				}
				else if(bnCurr==NULL)
				{
					break;
				}
				else
				{
					PrintBook(bnCurr);
					bnCurr=bnCurr->bnNext;
					++iTemp;
				}
			}
			printf("   本次查找已完成!\n");
		}
		else if(iChoose==6)
		{
			printf("   ****** 图书查找已完成 ******\n");
			return bnCurr;
		}
		else
		{
			printf("   请输入正确的选择!\n");
		}
	}
}

/* 用户查找 */
void ReaderFindMenu(void)
{
	printf("  ╭───────────╮\n");
	printf("  ∣       查找用户       ∣\n");
	printf("  ├───────────┤\n");
	printf("  ∣1.用户ID              ∣\n");
	printf("  ∣2.姓名                ∣\n");
	printf("  ∣3.完成                ∣\n");
	printf(" ╰───────────╯\n");
}

ReaderNode *ReaderFind(ReaderNode * rnFirst,BookNode * bnFirst)
{
	int iChoose;
	int iTemp=0;
	int iReaderID;
	char csReaderName[50];
	ReaderNode *rnCurr=NULL;
	for(;;iTemp=0)
	{
		ReaderFindMenu();
		iChoose=ChooseFunction();
		if(iChoose==1)
		{
			printf("   请输入用户ID:");
			iReaderID=InputNumber();
			for(rnCurr=rnFirst->rnNext;rnCurr!=NULL&&rnCurr->rReader.iID!=iReaderID;rnCurr=rnCurr->rnNext){}
			if(rnCurr==NULL)
			{
				printf("   没有此用户!\n");
			}
			else
				PrintReader(rnCurr,bnFirst);
			printf("   本次查找已完成!\n");
		}
		else if(iChoose==2)
		{
			rnCurr=rnFirst->rnNext;
			printf("   请输入用户姓名:");
			InputString(csReaderName);
			for(;;)
			{
				for(;rnCurr!=NULL&&strcmp(rnCurr->rReader.csName,csReaderName)!=0;rnCurr=rnCurr->rnNext){}
				if(rnCurr==NULL&&iTemp==0)
				{
					printf("   没有此用户!\n");
					break;
				}
				else if(rnCurr==NULL)
				{
					break;
				}
				else
				{
					PrintReader(rnCurr,bnFirst);
					rnCurr=rnCurr->rnNext;
					++iTemp;
				}
			}
			printf("   本次查找已完成!\n");
		}
		else if(iChoose==3)
		{
			printf("   用户查找已完成!\n");
			return rnCurr;
		}
		else
		{
			printf("   请输入正确的选择!\n");
		}
	}
}

/* 查询功能选择 */
void FindMenu()
{
	printf("  ╭───────────╮\n");
	printf("  ∣        查 询         ∣\n");
	printf("  ├───────────┤\n");
	printf("  ∣ 1.查找图书           ∣\n");
	printf("  ∣ 2.查找用户           ∣\n");
	printf("  ∣ 3.完成               ∣\n");
	printf(" ╰───────────╯\n");
}

void Find(BookNode * bnFirst,ReaderNode * rnFirst)
{
	int iChoose;
	for(;;)
	{
		FindMenu();
		iChoose=ChooseFunction();
		if(iChoose==1)
		{
			BookFind(bnFirst);
		}
		else if(iChoose==2)
		{
			ReaderFind(rnFirst,bnFirst);
		}
		else if(iChoose==3)
		{
			break;
		}
		else
			printf("   请输入正确的选择!\n");
	}
}

//////////////////////////////////////////
//读者管理
//////////////////////////////////////////

/* 打印用户清单 */
void PrintReaderList(ReaderNode * rnFirst,BookNode * bnFirst)
{
	ReaderNode* rnCurr;
	printf("   ****** 用户清单列表 ******\n");
	if(rnFirst->rnNext==NULL)
	{
		printf("   无任何相关信息!\n");
		return;
	}
	for(rnCurr=rnFirst->rnNext;rnCurr!=NULL;rnCurr=rnCurr->rnNext)
	{
		PrintReader(rnCurr,bnFirst);
		printf("   ****** 用户清单打印完成 ******\n");
	}
}

/* 插入新用户 */
void InsertNewReader(ReaderNode * rnFirst)
{
	ReaderNode *rnNewReader;
	rnNewReader=(ReaderNode *)malloc(sizeof(ReaderNode));
	if(!rnNewReader)
	{
		printf("   内存资源枯竭!\n");
		getchar();
		exit(0);
	}
	printf("   填写新用户属性\n");
	printf("   新用户姓名:");
	InputString(rnNewReader->rReader.csName);
	printf("   新用户班级:");
	InputString(rnNewReader->rReader.csClass);
	rnNewReader->rnNext=NULL;
	rnNewReader->rReader.iBookCount=0;

	ReaderNode *rnCurr,*rnPrev;
	if(rnFirst->rnNext==NULL)
	{
		rnFirst->rnNext=rnNewReader;
		rnNewReader->rReader.iID=1;
	}
	else
	{
		for(rnCurr=rnFirst->rnNext;rnCurr!=NULL;rnCurr=rnCurr->rnNext)
		{
			rnPrev=rnCurr;
		}
		rnPrev->rnNext=rnNewReader;
		rnNewReader->rReader.iID=rnPrev->rReader.iID+1;
	}
	printf("   加入用户成功!\n");
}

/* 修改用户属性 */
void ReaderChargeMenu(void)
{
	printf("  ╭───────────╮\n");
	printf("  ∣  可以修改的用户属性  ∣\n");
	printf("  ├───────────┤\n");
	printf("  ∣ 1.用户姓名           ∣\n");
	printf("  ∣ 2.用户所在班级       ∣\n");
	printf("  ∣ 3.完成               ∣\n");
	printf(" ╰───────────╯\n");
}

void ReaderCharge(ReaderNode * rnFirst,BookNode * bnFirst)
{
	int iChoose;
	ReaderNode *rnCurr;
	printf("   ****** 选择读者 ******\n");
	rnCurr=ReaderFind(rnFirst,bnFirst);
	if(rnCurr==NULL)
	{
		printf("   错误操作!\n");
		return;
	}
	ReaderChargeMenu();
	for(;;)
	{
		iChoose=ChooseFunction();
		if(iChoose==1)
		{
			printf("   输入新的用户姓名;");
			InputString(rnCurr->rReader.csName);
		}
		else if(iChoose==2)
		{
			printf("   输入新的班级:");
			InputString(rnCurr->rReader.csClass);
		}
		else if(iChoose==3)
		{
			printf("   修改完成!\n");
			printf("   ***** 当前用户属性 *****\n");
			PrintReader(rnCurr,bnFirst);
			break;
		}
		else
		{
			printf("   请输入正确的选择!\n");
		}
	}
}

/* 删除用户 */
void DeleteReader(ReaderNode * rnFirst,BookNode * bnFirst)
{
	char cChoose;
	ReaderNode *rnCurr,*rnPrev,*rnTemp;
	printf("   ****** 选择读者 ******\n");
	rnTemp=ReaderFind(rnFirst,bnFirst);
	if(rnTemp==NULL)
	{
		printf("   错误操作!\n");
		return;
	}
	rnPrev=rnFirst,rnCurr=rnFirst->rnNext;
	for(;rnCurr->rReader.iID!=rnTemp->rReader.iID;rnCurr=rnCurr->rnNext)
	{
		rnPrev=rnCurr;
	}
	printf("   是否要删除此用户?\n");
	printf("   是(y)\\否(n):");
	scanf("%c",&cChoose);

	if(cChoose=='y')
	{
		getchar();
		if(rnCurr->rReader.iBookCount!=0)
		{
			printf("   无法删除!(可能是因为图书无全部归还)\n");
		}
		else
		{
			rnPrev->rnNext=rnCurr->rnNext;
			free(rnCurr);
			printf("   成功删除!\n");
		}
	}
	else
	{
		if(cChoose=='\n')
		{
			return;
		}
		while(getchar()!='\n'){}
		printf("   你取消了删除操作!\n");
	}
}

/* 读者管理功能选择 */
void ReaderManageMenu(void)
{	
	printf("  ╭───────────╮\n");
	printf("  │       读者管理       ∣\n");
	printf("  ├───────────┤\n");
	printf("  ∣ 1.用户清单           ∣\n"); 
	printf("  ∣ 2.加入新读者         ∣\n");
	printf("  ∣ 3.修改读者属性       ∣\n");
	printf("  ∣ 4.删除读者           ∣\n");
	printf("  ∣ 5.完成               ∣\n");
	printf(" ╰───────────╯\n");
}

void ReaderManage(ReaderNode * rnFirst,BookNode * bnFirst)
{
	int iChoose;
	for(;;)
	{
		ReaderManageMenu();
		iChoose=ChooseFunction();
		if(iChoose==1)
		{
			PrintReaderList(rnFirst,bnFirst);
		}
		else if(iChoose==2)
		{
			InsertNewReader(rnFirst);
		}
		else if(iChoose==3)
		{
			ReaderCharge(rnFirst,bnFirst);
		}
		else if(iChoose==4)
		{
			DeleteReader(rnFirst,bnFirst);
		}
		else if(iChoose==5)
		{
			break;
		}
		else
			printf("   请输入正确的选择!\n");
	}
}

//////////////////////////////////////////
//关于
//////////////////////////////////////////
void AboutProgram(void)
{
	printf("  ╭────────────╮\n");
	printf("  │      关于本软件        ∣\n");
	printf("  ├────────────┤\n");
	printf("  ∣ 软件名称: 图书管理系统 ∣\n"); 
	printf("  ∣ 版本号: V1.03          ∣\n");
	printf("  ∣ 程序设计: 王 正        ∣\n");
	printf(" ╰────────────╯\n");
}

//////////////////////////////////////////
//退出
//////////////////////////////////////////
void Quit(BookNode * bnFirst,ReaderNode * rnFirst)
{
	char cChoose;
	printf("   是否退出图书管理系统?\n");
	printf("   是(y)\\否(n):");
	scanf("%c",&cChoose);
	if(cChoose=='y')
	{
		BookUnLoad(bnFirst);
		ReaderUnLoad(rnFirst);
		exit(0);
	}
	else
	{
		if(cChoose=='\n')
		{
			return;
		}

		while(getchar()!='\n'){}
	}
}

//////////////////////////////////////////
//主菜单
//////////////////////////////////////////
void MainMenu(void)
{
	printf("  ╭─────────────────────────────────╮\n");
	printf("  ∣ 借书(b) 还书(r) 查询(f) 图书管理(m) 读者管理(s) 关于(a) 退出(q) ∣\n");
	printf("  ╰─────────────────────────────────╯\n");
}

void main()
{
	char cFunction;
	BookNode *bnFirst;
	bnFirst=BookLoad();
	ReaderNode *rnFirst;
	rnFirst=ReaderLoad();
	for(;;)
	{
		printf("                         图书管理系统 V1.03\n");
		MainMenu();
		printf("   请选择:");
		cFunction=getchar();
		if(cFunction=='\n')
		{
			printf("   请输入正确的选择!\n");
		}
		else if(getchar()!='\n')
		{
			printf("   请输入正确的选择!\n");
			while(getchar()!='\n'){}
		}
		else
		{

			if(cFunction=='b')
			{
				BookBorrow(bnFirst,rnFirst);
				ReaderSave(rnFirst);
				BookSave(bnFirst);
			}
			else if(cFunction=='r')
			{
				BookReturn(bnFirst,rnFirst);
				ReaderSave(rnFirst);
				BookSave(bnFirst);
			}
			else if(cFunction=='m')
			{
				BookManage(bnFirst);
				BookSave(bnFirst);
			}
			else if(cFunction=='f')
			{
				Find(bnFirst,rnFirst);
			}
			else if(cFunction=='s')
			{
				ReaderManage(rnFirst,bnFirst);
				ReaderSave(rnFirst);
			}
			else if(cFunction=='a')
			{
				AboutProgram();
			}
			else if(cFunction=='q')
			{
				Quit(bnFirst,rnFirst);
			}
			else
			{
				printf("   请输入正确的选择!\n");
			}
		}
		printf("   ");
		system("PAUSE");
		system("CLS");
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -