图书管理系统数组版

@[C语言](图书馆里系统 数组处理)

工程概论

主要针对学生书写的一个例子程序,内容比较完善,同时具备很好的扩展性,函数模块化

基本功能

主要实现了对图书的插入、查询、删除、管理等功能,同时增加了用户登录模块。

代码部分

void LoadBook()
{
	FILE *fp = fopen("book.txt", "r"); //第一次打开文件肯定不存在
	if (fp == NULL)
	{
		fp = fopen("book.txt", "w+");
		fclose(fp);
		return;
	}
	else
	{
		while (fscanf(fp, "%s\t%s\t%s\t%s\t%s\t%d\n", info[top].id, info[top].name, info[top].author, info[top].press, info[top].pubdate, &info[top].price) != EOF)
		{
			top++;
		}
		fclose(fp);
	}
}


void WriteBook()
{
	int i = 0;
	FILE *fp = fopen("book.txt", "w");
	if (fp == NULL)
	{
		return;
	}
	for (i = 0; i < top;i++)
	{
		fprintf(fp, "%s\t%s\t%s\t%s\t%s\t%d\n", info[i].id, info[i].name, info[i].author, info[i].press, info[i].pubdate, info[i].price);
	}
	fclose(fp);
}

int FindBookId(char *id)
{
	int i = 0;
	for (i = 0; i < top; i++)
	{
		if (strcmp(info[i].id, id) == 0)
		{
			return i;
		}
	}
	return -1;
}


void InsertBookView()
{
	b_info binfo;
	printf("请依次输入新增的图书编号、名称、作者、出版社、出版时间(yyyy/MM/dd)、价格");
	scanf("%s%s%s%s%s%d", binfo.id, binfo.name, binfo.author, binfo.press, binfo.pubdate, &binfo.price);
	if (strlen(binfo.id) == 0)
	{
		printf("图书编号不得为空!");
		return;
	}
	if (FindBookId(binfo.id) >= 0)
	{
		printf("图书编号已存在,添加失败\n");
		return;
	}
	info[top++] = binfo;
	WriteBook();
	printf("添加成功\n");
}


void DeleteBookView()
{
	char id[BUF_MAX] = { 0 };
	int ret = -1;
	printf("请输入删除的图书编号:");
	scanf("%s", id);
	if (ret = (FindBookId(id)) < 0)
	{
		printf("图书编号不存在,删除失败\n");
		return;
	}
	for (ret; ret < top; ret++)
	{
		info[ret] = info[ret + 1];
	}
	top--;
	WriteBook();
	printf("删除成功\n");
}

void UpdateBookView()
{
	b_info binfo;
	int ret = -1;
	printf("请输入修改的图书编号:");
	scanf("%s", binfo.id);
	if (ret = (FindBookId(binfo.id)) < 0)
	{
		printf("图书编号不存在,修改失败\n");
		return;
	}

	printf("请依次输入新的名称、作者、出版社、出版时间(yyyy/MM/dd)、价格");
	scanf("%s%s%s%s%d",binfo.name, binfo.author, binfo.press, binfo.pubdate, &binfo.price);
	info[ret] = binfo;
	WriteBook();
	printf("修改成功\n");
}

void PrintBookView()
{
	int i = 0;
	printf("编  号\t    书  名\t\t作 者\t出  版  社\t\t出版时间\t价格\n");
	for (i = 0; i < top; i++)
	{
		printf("%6s\t%10s\t\t%6s\t%10s\t\t%8s\t%d\n", info[i].id, info[i].name, info[i].author, info[i].press, info[i].pubdate, info[i].price);
	}
}

void SelectBookView()
{
	int type = 0;
	char temp[BUF_MAX] = { 0 };
	int i = 0;
	printf("请选择查找条件:1.编号、2.书名、3.作者、4.出版社");
	scanf("%d", &type);
	switch (type)
	{
	case 1:
	{
			  printf("请输入查找编号:");
			  scanf("%s", temp);
			  printf("编  号\t    书  名\t\t作 者\t出  版  社\t\t出版时间\t价格\n");
			  for (i = 0; i < top; i++)
			  {
				  if (strcmp(info[i].id, temp) == 0)
				  {
					  printf("%6s\t%10s\t\t%6s\t%10s\t\t%8s\t%d\n", info[i].id, info[i].name, info[i].author, info[i].press, info[i].pubdate, info[i].price);
					  break;
				  }
			  }
	}break;
	case 2:
	{
			  printf("请输入查找书名:");
			  scanf("%s", temp);
			  printf("编  号\t    书  名\t\t作 者\t出  版  社\t\t出版时间\t价格\n");
			  for (i = 0; i < top; i++)
			  {
				  if (strcmp(info[i].name, temp) == 0)
				  {
					  printf("%6s\t%10s\t\t%6s\t%10s\t\t%8s\t%d\n", info[i].id, info[i].name, info[i].author, info[i].press, info[i].pubdate, info[i].price);
				  }
			  }
	}break;
	case 3:
	{
			  printf("请输入查找作者:");
			  scanf("%s", temp);
			  printf("编  号\t    书  名\t\t作 者\t出  版  社\t\t出版时间\t价格\n");
			  for (i = 0; i < top; i++)
			  {
				  if (strcmp(info[i].author, temp) == 0)
				  {
					  printf("%6s\t%10s\t\t%6s\t%10s\t\t%8s\t%d\n", info[i].id, info[i].name, info[i].author, info[i].press, info[i].pubdate, info[i].price);
				  }
			  }
	}break;
	case 4:
	{
			  printf("请输入查找出版社:");
			  scanf("%s", temp);
			  printf("编  号\t    书  名\t\t作 者\t出  版  社\t\t出版时间\t价格\n");
			  for (i = 0; i < top; i++)
			  {
				  if (strcmp(info[i].author, temp) == 0)
				  {
					  printf("%6s\t%10s\t\t%6s\t%10s\t\t%8s\t%d\n", info[i].id, info[i].name, info[i].author, info[i].press, info[i].pubdate, info[i].price);
				  }
			  }
	}break;
	default:
		break;
	}
}

void UserView()//用户界面
{
	char ch;
	system("cls");
	fflush(stdin);
	printf("*********************************\n");
	printf("*           1:插入图书          *\n");
	printf("*           2:删除图书          *\n");
	printf("*           3:查找图书          *\n");
	printf("*           4:修改图书          *\n");
	printf("*           5:浏览图书          *\n");
	printf("*           O:退出登录          *\n");
	printf("*           Q:退出系统          *\n");
	printf("*********************************\n");
	printf("请选择需要进行的操作:");
	while ((ch = getchar()) != 'Q')
	{
		if (ch == '1')
		{
			InsertBookView();
		}
		else if (ch == '2')
		{
			DeleteBookView();
		}
		else if (ch == '3')
		{
			SelectBookView();
		}
		else if (ch == '4')
		{
			UpdateBookView();
		}
		else if (ch == '5')
		{
			PrintBookView();
		}
		else if (ch == 'O')
		{
			printf("成功退出\n");
			return;
		}
		system("pause");
		system("cls");
		fflush(stdin);
		printf("*********************************\n");
		printf("*           1:插入图书          *\n");
		printf("*           2:删除图书          *\n");
		printf("*           3:查找图书          *\n");
		printf("*           4:修改图书          *\n");
		printf("*           5:浏览图书          *\n");
		printf("*           O:退出登录          *\n");
		printf("*           Q:退出系统          *\n");
		printf("*********************************\n");
		printf("请选择需要进行的操作:");
	}
	if (ch == 'Q')exit(0);
}

int main()
{
	char ch;
	char temp[BUF_MAX] = { 0 };
	LoadBook();
	system("cls");
	fflush(stdin);
	printf("*********************************\n");
	printf("*           L:登录系统          *\n");
	printf("*           Q:退出系统          *\n");
	printf("*********************************\n");
	printf("请选择需要进行的操作:");
	while ((ch = getchar()) != 'Q')
	{
		if (ch == 'L')
		{
			strcpy(temp, "");
			printf("请输入登录账号");
			scanf("%s",temp);
			if (strcmp(temp, admin) == 0)
			{
				UserView();
			}
			else
			{
				printf("账号有误\n");
			}
		}
		else
		{
			printf("选择有误\n");
		}
		system("pause");
		system("cls");
		fflush(stdin);
		printf("*********************************\n");
		printf("*           L:登录系统          *\n");
		printf("*           Q:退出系统          *\n");
		printf("*********************************\n");
		printf("请选择需要进行的操作:");
	}
	exit(0);
	return 1;
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
工程地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值