【c语言】练手:一个简单的图书管理系统

【c语言】练手:一个简单的图书管理系统


最近在学习结构体和结构体指针等内容,为了巩固所学习的内容,于是写了一个简陋的图书管理系统,在敲代码过程中也遇到一些容易忽略的问题,于是记录下来,大家可以给给建议!


核心功能主要有3个:录入书籍信息,打印书籍信息,删除书籍信息。
思路很简单,定义一个结构体Book来实例化结构体变量从而用于存放每一本书的信息。书的信息包括 书名,作者, 价格, 出版时间和出版日期。
//存放书信息的结构体 
struct Book
{
	char title[128];
	char author[40];
	double price;
	struct Date date; //这里的Date也是一个结构体
	char publisher[40];
};

定义一个结构体用于存放日期的年,月,日

//存放日期的结构体 
struct Date
{
	int year;
	int month;
	int day;
};

//存放 书 的指针数组 
struct Book * library[1000] = {NULL};
int book_num = 0; 		//记录书籍总数 
int count = 0; 			//用来在每个模块中记录操作的书籍数量 

main函数里提示操作的部分:
int main(void)
{
	printf("==========欢迎使用图书馆1.0==========\n");
	while(1)  //输入的指令不是4时就一直循环输入
	{
	
		printf("请输入操作指令前对应的序号:\n\
		1.录入书籍信息\n\
		2.打印书籍信息\n\
		3.删除书籍信息\n\
		4.退出图书馆\n");
		
		int order = 0;   //存放输入的序号
		scanf("%d" , &order);
        ......(后面省略)

书籍信息录入部分:
//录入书籍模块 
		if (order == 1)
		{
			libraryInput(); 
		}

libraryInput 函数的内容

//录入图书馆模块
void libraryInput()
{
		int num = 0;
		printf("请输入要录入书籍的数量:");
		scanf("%d" , &num);
		
		
		while(num--)
		{
			count++;   //定义的一个全局变量,用在每一个模块里记录操作第几本书
			struct Book *b;
			
			b = (struct Book *)malloc(sizeof(struct Book)); // 动态申请内存空间,操作完记得释放free(b);
			if (b == NULL)
			{
				printf("内存分配失败!\n");
				exit(1); 
			}
			
			printf("请录入第%d本书籍的信息:\n" , count);
			
			b = getInput(b);
			
			book_num++; 		//全局变量 , 更新书籍总数 
			
			//把书放进图书馆前看看哪里有空位
			for(int i = 0 ; i < 1000 ; i++)
			{
				if (library[i] == NULL)
				{
					library[i] = b;
					break;
				}
			} 
			
		}
		free(b);
		printf("书籍信息录入完成!\n\n");
		count = 0;
} 
struct Book * getInput(struct Book *book) //返回结构体指针的函数,参数也是一个结构体指针
{
	printf("请输入书名:");
	scanf("%s" , book->title);
	
	printf("请输入作者:");
	scanf("%s" , book->author); 		//字符串数组指针不用解引用
	
	printf("请输入价格:");
	scanf("%lf" , &book->price); 		//&不能漏掉
	
	printf("请输入出版日期:");
	scanf("%d %d %d" , &book->date.year , &book->date.month , &book->date.day); 			//注意不要漏了 & 
	
	getchar();
	
	printf("请输入出版社:");
	scanf("%s" , book->publisher);
	
	putchar('\n');
	
	return book;
}

打印模块
//打印书籍信息 
		if (order == 2)
		{	
			libraryPrintf();
		}
//打印图书馆模块
void libraryPrintf()
{
	for (int i = 0 ; i < 1000 ; i++)
	{
			if (library[i] != NULL)
			{
				count++;
				printf("\n打印第%d本书籍的信息:\n" , count);
				printfBook(library[i]); //函数
				if (count == book_num)
				{
					break;
				}
			} 
	}
		
	printf("\n打印完成!总共打印了%d本书籍!\n\n" , count);
	count = 0;
} 
//打印书籍信息模块
void printfBook(struct Book *book)
{
	printf("书名:%s\n" , book->title);
	printf("作者:%s\n" , book->author);
	printf("价格:%.2lf\n" , book->price);
	printf("出版时间:%d-%d-%d\n" , book->date.year , book->date.month , book->date.day);
	printf("出版社:%s\n" , book->publisher);
} 

删除书籍信息模块
//书籍删除模块 
		if (order == 3)
		{
			libraryDel();			 
		}
 //图书馆删除模块
 void libraryDel()
 {
 		char book_title[128];
		printf("请输入要删除的书名:" );
		scanf("%s" , &book_title);
		getchar();
		
		for (int i = 0 ; i < 1000 ; i++)
		{
			if ( !strcmp( library[i]->title , book_title) )
			{
				
				printf("确定要删除该书籍吗?(Y/N) :\n");
				if (getchar() == 'Y')
				{
					library[i] = NULL;
					book_num--;
					printf("书籍%s删除成功!\n\n" , book_title);
					break;
				}
				else
				{
					break;
				}
				
			}
			if (i == 999)
			{
				printf("找不到书籍信息,请检查格式是否错误!\n");
				break;
			}
		}
} 

完整代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Book * getInput(struct Book *book);
void printfBook(struct Book *book);
void libraryInput();
void libraryPrintf();
void libraryDel();

//存放 书 的指针数组 
struct Book * library[1000] = {NULL};
int book_num = 0; 		//记录书籍总数 
int count = 0; 			//用来在每个模块中记录操作的书籍数量 


//存放日期的结构体 
struct Date
{
	int year;
	int month;
	int day;
};

//存放书信息的结构体 
struct Book
{
	char title[128];
	char author[40];
	double price;
	struct Date date;
	char publisher[40];
};

struct Book * getInput(struct Book *book)
{
	printf("请输入书名:");
	scanf("%s" , book->title);
	
	printf("请输入作者:");
	scanf("%s" , book->author);
	
	printf("请输入价格:");
	scanf("%lf" , &book->price); 		//&
	
	printf("请输入出版日期:");
	scanf("%d %d %d" , &book->date.year , &book->date.month , &book->date.day); 			//注意不要漏了 & 
	
	getchar();
	
	printf("请输入出版社:");
	scanf("%s" , book->publisher);
	
	putchar('\n');
	
	return book;
}

//打印书籍信息模块
void printfBook(struct Book *book)
{
	printf("书名:%s\n" , book->title);
	printf("作者:%s\n" , book->author);
	printf("价格:%.2lf\n" , book->price);
	printf("出版时间:%d-%d-%d\n" , book->date.year , book->date.month , book->date.day);
	printf("出版社:%s\n" , book->publisher);
} 

//录入图书馆模块
void libraryInput()
{
	int num = 0;
		printf("请输入要录入书籍的数量:");
		scanf("%d" , &num);
		
		
		while(num--)
		{
			count++;
			struct Book *b;
			
			b = (struct Book *)malloc(sizeof(struct Book));
			if (b == NULL)
			{
				printf("内存分配失败!\n");
				exit(1); 
			}
			
			printf("请录入第%d本书籍的信息:\n" , count);
			
			b = getInput(b);
			book_num++; 		//更新书籍总数 
			
			//把书放进图书馆前看看哪里有空位
			for(int i = 0 ; i < 1000 ; i++)
			{
				if (library[i] == NULL)
				{
					library[i] = b;
					break;
				}
			} 
			
		}
		
		printf("书籍信息录入完成!\n\n");
		count = 0;
} 

//打印图书馆模块
void libraryPrintf()
{
	for (int i = 0 ; i < 1000 ; i++)
	{
			if (library[i] != NULL)
			{
				count++;
				printf("\n打印第%d本书籍的信息:\n" , count);
				printfBook(library[i]);
				if (count == book_num)
				{
					break;
				}
			} 
	}
		
	printf("\n打印完成!总共打印了%d本书籍!\n\n" , count);
	count = 0;
} 
 
 //图书馆删除模块
 void libraryDel()
 {
 	char book_title[128];
		printf("请输入要删除的书名:" );
		scanf("%s" , &book_title);
		getchar();
		
		for (int i = 0 ; i < 1000 ; i++)
		{
			if ( !strcmp( library[i]->title , book_title) )
			{
				
				printf("确定要删除该书籍吗?(Y/N) :\n");
				if (getchar() == 'Y')
				{
					library[i] = NULL;
					book_num--;
					printf("书籍%s删除成功!\n\n" , book_title);
					break;
				}
				else
				{
					break;
				}
				
			}
			if (i == 999)
			{
				printf("找不到书籍信息,请检查格式是否错误!\n");
				break;
			}
		}
} 

int main(void)
{
	printf("==========欢迎使用图书馆1.0==========\n");
	while(1)
	{
	
		printf("请输入操作指令前对应的序号:\n\
		1.录入书籍信息\n\
		2.打印书籍信息\n\
		3.删除书籍信息\n\
		4.退出图书馆\n");
		
		int order = 0;
		scanf("%d" , &order);
		
		//录入书籍模块 
		if (order == 1)
		{
			libraryInput();
		}
		
		//打印书籍信息 
		if (order == 2)
		{
			
			libraryPrintf();
			
		}
		
		//书籍删除模块 
		if (order == 3)
		{
			libraryDel();			 
		}
		
		if (order == 4)
		{
			printf("\n========恭送皇上~=======\n");
			break;
		}
	
	}
	
	return 0;
}
  • 8
    点赞
  • 66
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端corner

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值