C语言图书管理系统

我们在学习完C语言后都会自己设计一个系统,而博主在学习完c语言后写了个图书管理系统,接下来让我们一起来看看程序吧。

在设计一个系统之前我们应该思考系统的结构,系统的功能以及客户的需求等等。我设计的这个系统有以下功能:管理图书信息(增删改查),管理读者信息(增删改查),借书操作以及还书操作。

有任何问题请在下面评论,谢谢。

接下来直接上代码:

下面展示 全部代码

// 图书管理系统
// An highlighted block
#include <stdio.h>
#include <string.h> 
#include <stdlib.h>
typedef struct book{
	int bookNum;				//图书编号 
	char bookName[20];			//书名 
	char author[20];			//作者名 
	int flNum;					//分类编号 
	char publish[20];			//出版设 
	int publishTime[3];		//出版时间 
	int saveNum;				//库存数量 
	char value[20];				//价格 
	struct book *next;
}book; 
typedef struct reader{
	int num;					//读者编号 
	char name[20];				//读者姓名 
	int maxQuota;				//最大借阅额度 
	int borrow;					//已借阅数量 
	struct reader *next;
}reader; 
typedef struct brwTable{
	int bookNum;				//图书编号 
	char bookName[20];			//书名 
	int num;					//读者编号 
	char name[20];				//读者姓名 
	int saveNum;				//库存数量 
	struct brwTable *next;
}borrow; 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void bookMenu();				//图书菜单函数 
void createBook();				//图书信息录入函数 
void showBook();				//游览图书信息函数 
void findBook();				//查找图书信息函数 
void deleteBook();				//删除图书信息函数 
void updateBook();				//修改图书信息函数 
void book_menu(); 				//修改图书信息菜单 
void readerMenu();				//读者菜单函数 
void createReader();			//读者信息录入函数 
void showReader();				//游览读者信息函数 
void deleteReader();			//删除读者信息函数 
void findReader();				//查找读者信息函数 
void updateReader();			//修改读者信息函数 
void borrowBook();					//借书函数 
void repay();					//还书函数 
int main(int argc, char *argv[]) {
	int ch;
	int temp;
	while(1)
	{
		printf("\t\t\t1、图书信息管理\n");
		printf("\t\t\t2、读者信息管理\n");
		printf("\t\t\t3、借书操作\n");
		printf("\t\t\t4、还书操作\n");
		printf("\t\t\t0、退出系统\n");
		printf("请输入你要进行的操作:");
		temp=scanf("%d",&ch);
		fflush(stdin);
		while(temp==0||temp==-1)
		{
			printf("输入错误,请重新输入!:");
			temp=scanf("%d",&ch);
			fflush(stdin);
			if(temp==1)
			break;
			fflush(stdin);
		}
			
		switch(ch)
		{
			case 1:bookMenu();break;
			case 2:readerMenu();break;
			case 3:borrowBook();break;
			case 4:repay();break;
			case 0:exit(0);
			default:printf("输入错误!请重新输入:"); 
		}
	}
	return 0;
}
//图书信息菜单 
void bookMenu()
{		int n,temp;
		char ch;
		printf("\t\t\t1、图书信息录入\n");
		printf("\t\t\t2、图书信息游览\n");
		printf("\t\t\t3、图书信息查询\n");
		printf("\t\t\t4、图书信息的删除\n");
		printf("\t\t\t5、图书信息的修改\n");
		printf("\t\t\t0、退出\n");
		printf("请输入你要进行的操作:");
		temp=scanf("%d",&n);
		fflush(stdin);
		while(temp==0||temp==-1)
		{
			printf("输入错误,请重新输入!:");
			temp=scanf("%d",&n);
			fflush(stdin);
			if(temp==1)
			break;
		}
		
		switch(n)
		{
			case 1:createBook();break;
			case 2:showBook();break;
			case 3:findBook();break;
			case 4:deleteBook();break;
			case 5:updateBook();while(1){printf("是否继续修改 是(Y)/否(N):");ch=getchar();scanf("%c",&ch);if(ch=='y'||ch=='Y')updateBook();else break;}break;
			case 0:break;
			default:printf("输入错误!请重新输入:\n");
		}
}
//图书信息录入
void createBook()
{
	book *head,*p,*tail;
	int ch,j,i; 
	head=tail=NULL;
	printf("请输入要录入的图书数量:");
	scanf("%d",&ch);
	for(j=0;j<ch;j++)
	{
	if((p=(book *)malloc(sizeof(book)))==NULL)
	{
		printf("分配空间失败!");
		exit(0); 
	}
	printf("第%d个的图书信息录入!\n",j+1);
	printf("请输入图书编号:");
	scanf("%d",&p->bookNum);
	printf("请输入书名:");
	scanf("%s",p->bookName);	 
	printf("请输入作者姓名:");
	scanf("%s",p->author);
	printf("请输入分类号:");
	scanf("%d",&p->flNum);
	printf("请输入出版社:");
	scanf("%s",p->publish);
	printf("请输入出版时间(格式为年:月:日):");
	for(i=0;i<3;i++)
	{
		scanf("%d",&p->publishTime[i]);
	}
	printf("请输入库存数量:");
	scanf("%d",&p->saveNum);
	printf("请输入图书价格:");
	scanf("%s",p->value);		
	if(head==NULL)
		head=p;
	else
	tail->next=p;
	tail=p;
	tail->next=NULL;
	}

	FILE *file;

	if((file = fopen("book.txt","w"))==NULL)
	{	
		printf("打开文件失败!");
	} 
	while(head!=NULL)
	{
		fprintf(file,"%d %s %s %d %s %d %d %d %d %s\n",head->bookNum,head->bookName,head->author,head->flNum,head->publish,head->publishTime[0],head->publishTime[1],head->publishTime[2],head->saveNum,head->value);
		head=head->next;
	}
	fclose(file);		
 } 
 //游览图书信息
 void showBook()
 {
 	book *p;
 	FILE *file;
 	char temp;
	if((file = fopen("book.txt","r"))==NULL)
	{	
		printf("打开文件失败!");
	} 
	
	if((temp=fgetc(file))==EOF)
	{
		printf("未有图书录入!\n");
		return; 
	 } 
	 rewind(file);
	while(!feof(file))
	{
	if((p=(book *)malloc(sizeof(book)))==NULL)
		{
			printf("分配空间失败!");
			exit(0); 
		}
	fscanf(file,"%d %s %s %d %s %d %d %d %d %s\n",&p->bookNum,p->bookName,p->author,&p->flNum,p->publish,&p->publishTime[0],&p->publishTime[1],&p->publishTime[2],&p->saveNum,p->value);
	printf("图书编号:%d\t书名:%s\t作者姓名:%s\t分类号:%d\n出版社:%s\t出版时间:%d年%d月%d日\t库存数量:%d\t价格:%s\n",p->bookNum,p->bookName,p->author,p->flNum,p->publish,p->publishTime[0],p->publishTime[1],p->publishTime[2],p->saveNum,p->value);
	}
	fclose(file);	
  } 
  //查找图书信息
  void findBook()
  {
  	book *p;
  	int ch=0;
  	char name[20];
  	FILE *file;
  	if((file=fopen("book.txt","r"))==NULL)
  	{
  		printf("打开文件失败!");
		  exit(0); 
	  }
	printf("请输入要查找的图书的名字:");
	scanf("%s",name);
	while(!feof(file))
	{
		if((p=(book *)malloc(sizeof(book)))==NULL)
		{
			printf("分配空间失败!");
			exit(0); 	
		}
		fscanf(file,"%d %s %s %d %s %d %d %d %d %s\n",&p->bookNum,p->bookName,p->author,&p->flNum,p->publish,&p->publishTime[0],&p->publishTime[1],&p->publishTime[2],&p->saveNum,p->value);
		if(strcmp(p->bookName,name)==0)
		{
			printf("图书编号:%d\t书名:%s\t作者姓名:%s\t分类号:%d\n出版社:%s\t出版时间:%d年%d月%d日\t库存数量:%d\t价格:%s\n",p->bookNum,p->bookName,p->author,p->flNum,p->publish,p->publishTime[0],p->publishTime[1],p->publishTime[2],p->saveNum,p->value);
			ch=1;
			break;
		}
	}
	if(ch==0)
	printf("未找到该书籍!\n");
   } 
   //删除图书信息
   void deleteBook()
   {
   		char name[20];
   		char ch;
   		book *p,*head,*tail,*q;
   		
   		FILE *file;
   		if((head=(book *)malloc(sizeof(book)))==NULL)
   		{
   			printf("分配空间失败!");
				exit(0); 
		}
		tail=head;
		tail->next=NULL;
   		if((file=fopen("book.txt","r"))==NULL)
   		{
   		printf("打开文件失败!");
		  exit(0); 
		}
		printf("请输入要删除的图书的名字:");
		scanf("%s",name);
		ch=getchar(); 
	
		while(!feof(file))
		{
			if((p=(book *)malloc(sizeof(book)))==NULL)
			{
				printf("分配空间失败!");
				exit(0); 	
			}
			fscanf(file,"%d %s %s %d %s %d %d %d %d %s\n",&p->bookNum,p->bookName,p->author,&p->flNum,p->publish,&p->publishTime[0],&p->publishTime[1],&p->publishTime[2],&p->saveNum,p->value);
			p->next=NULL;
			tail->next=p;
			tail=p;	
		}
		fclose(file);
		
		if((file=fopen("book.txt","w"))==NULL)
   		{
   		printf("打开文件失败!");
		  exit(0); 
		}
		
		tail=head;
		head=head->next;
		q=tail;
		
		while(head!=NULL)
		{	
		ch='a'; 
		if(strcmp(head->bookName,name)==0)
		{
			printf("图书编号:%d\t书名:%s\t作者姓名:%s\t分类号:%d\n出版社:%s\t出版时间:%d年%d月%d日\t库存数量:%d\t价格:%s\n",head->bookNum,head->bookName,head->author,head->flNum,head->publish,head->publishTime[0],head->publishTime[1],head->publishTime[2],head->saveNum,head->value);
			printf("是否删除 是(Y)/否(N):");
			ch=getchar();
			p=head;
			printf("\n");
			if(ch=='Y'||ch=='y')
			{
				tail->next=head->next;
				printf("删除成功!\n");
			}
			else
			{
				printf("删除失败!\n");
				break;
			}
		}	
			head=head->next;
			tail=tail->next;
			if(ch=='Y'||ch=='y')
				free(p);
		}
		q=q->next;
		while(q!=NULL)
		{	
			fprintf(file,"%d %s %s %d %s %d %d %d %d %s\n",q->bookNum,q->bookName,q->author,q->flNum,q->publish,q->publishTime[0],q->publishTime[1],q->publishTime[2],q->saveNum,q->value);
			q=q->next;
		}
		
		fclose(file);
	}
//修改图书信息
void updateBook()
{
	FILE *file;
	if((file=fopen("book.txt","r"))==NULL)
	{
		printf("打开文件失败!");
		  exit(0); 
	}
	
	book *head,*tail,*p,*q;
	char name[20],ch='a';
	int n,i,temp; 
	head=tail=NULL;
	printf("请输入要修改的图书姓名:");
	scanf("%s",name);
	while(!feof(file))
	{
		if((p=(book *)malloc(sizeof(book)))==NULL)
		{
			printf("分配空间失败!");
			exit(0); 	
		}
		fscanf(file,"%d %s %s %d %s %d %d %d %d %s\n",&p->bookNum,p->bookName,p->author,&p->flNum,p->publish,&p->publishTime[0],&p->publishTime[1],&p->publishTime[2],&p->saveNum,p->value);
		if(head==NULL)
		head=p;
		else
		tail->next=p;
		tail=p;
		tail->next=NULL;
	}
	fclose(file);
	
	q=head;
	
	while(head!=NULL)
	{
		if(strcmp(name,head->bookName)==0)
		{
			printf("图书编号:%d\t书名:%s\t作者姓名:%s\t分类号:%d\n出版社:%s\t出版时间:%d年%d月%d日\t库存数量:%d\t价格:%s\n",head->bookNum,head->bookName,head->author,head->flNum,head->publish,head->publishTime[0],head->publishTime[1],head->publishTime[2],head->saveNum,head->value);
			printf("是否修改 是(Y)/否(N):");
			ch=getchar();
			ch=getchar();
		
		if(ch=='Y'||ch=='y')
			{
				book_menu(); 
				temp=scanf("%d",&n);
				fflush(stdin);
				while(temp==0||temp==-1) 
				{
					printf("输入有误,请重新输入:");
					temp=scanf("%d",&n);
					fflush(stdin);
					if(temp==1)
					break;
				}
				switch(n)
				{
					case 1: printf("请输入修改后的图书编号:");scanf("%d",&head->bookNum);break;	
					case 2: printf("请输入修改后的图书姓名:");scanf("%s",head->bookName);break;
					case 3: printf("请输入修改后的图书作者:");scanf("%s",head->author);break;
					case 4: printf("请输入修改后的图书分类号:");scanf("%d",&head->flNum);break;
					case 5: printf("请输入修改后的图书出版社:");scanf("%s",head->publish);break;
					case 6: printf("请输入修改后的图书出版时间(格式为年:月:日:)");for(i=0;i<3;i++)scanf("%d",&head->publishTime[i]);break;
					case 7: printf("请输入修改后的图书库存数量:");scanf("%d",&head->saveNum);break;
					case 8: printf("请输入修改后的图书价格:");scanf("%s",head->value);break;
					case 0: break;
					default: printf("输入错误,请重新输入!\n"); 
				} 
				printf("修改成功!\n");		
				break; 
			}
			else
			{
				printf("修改失败!\n");
				break;;
			}
		}
		head=head->next;
	}
		if((file=fopen("book.txt","w"))==NULL)
	   		{
	   			printf("打开文件失败!");
			  	exit(0); 
			}
			while(q!=NULL)
			{
			//	printf("图书编号:%d\t书名:%s\t作者姓名:%s\t分类号:%d\n出版社:%s\t出版时间:%d年%d月%d日\t库存数量:%d\t价格:%s\n",q->bookNum,q->bookName,q->author,q->flNum,q->publish,q->publishTime[0],q->publishTime[1],q->publishTime[2],q->saveNum,q->value);
				fprintf(file,"%d %s %s %d %s %d %d %d %d %s\n",q->bookNum,q->bookName,q->author,q->flNum,q->publish,q->publishTime[0],q->publishTime[1],q->publishTime[2],q->saveNum,q->value);
				q=q->next;
			}
			fclose(file);
			if(ch=='a')
			printf("未找到该图书!请重新输入!\n");
 } 
 //修改图书信息菜单
 void book_menu()
 {
	printf("\t\t\t1、修改图书编号\n");
	printf("\t\t\t2、修改图书姓名\n");
	printf("\t\t\t3、修改图书作者\n");
	printf("\t\t\t4、修改图书分类号\n");
	printf("\t\t\t5、修改图书出版社\n");
	printf("\t\t\t6、修改图书出版时间\n");
	printf("\t\t\t7、修改图书库存数量\n");
	printf("\t\t\t8、修改图书价格\n");
	printf("\t\t\t0、退出\n");
	printf("请输入你要进行的操作:");
  } 
  //图书信息菜单 
void readerMenu()
{		int n;
		char ch;
		printf("\t\t\t1、读者信息录入\n");
		printf("\t\t\t2、读者信息游览\n");
		printf("\t\t\t3、读者信息查询\n");
		printf("\t\t\t4、读者信息的删除\n");
		printf("\t\t\t5、读者信息的修改\n");
		printf("\t\t\t0、退出\n");
		printf("请输入你要进行的操作:");
		scanf("%d",&n);
		switch(n)
		{
			case 1:createReader();break;
			case 2:showReader();break;
			case 3:findReader();break;
			case 4:deleteReader();break;
			case 5:updateReader();while(1){printf("是否继续修改 是(Y)/否(N):");ch=getchar();scanf("%c",&ch);if(ch=='y'||ch=='Y')updateReader();else break;}break;
			case 0:break;
			default:printf("输入错误!请重新输入:\n"); 
		}
}
//读者信息录入
void createReader()
{
	FILE *file;
	reader *p;
	int n,i;
	if((file=fopen("reader.txt","w"))==NULL)
	{
		printf("打开文件失败!\n");
		exit(0);
	}
	if((p=(reader *)malloc(sizeof(reader)))==NULL)
	{
		printf("分配内存失败!");
		exit(0);
	}
	printf("请输入要添加读者的数量:");
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		printf("第%d个读者的录入\n",i+1);
		printf("请输入读者编号:");
		scanf("%d",&p->num);
		printf("请输入读者姓名:");
		scanf("%s",p->name);
		printf("请输入读者最大借阅数:");
		scanf("%d",&p->maxQuota);
		printf("请输入读者已借阅数量:");
		scanf("%d",&p->borrow);
		fprintf(file,"%d %s %d %d\n",p->num,p->name,p->maxQuota,p->borrow);
	}
	printf("录入成功!\n"); 
	fclose(file);
 }
 //展示读者信息 
 void showReader()
 {
 	FILE *file;
 	reader *p;
 	char temp;
 	if((file=fopen("reader.txt","r"))==NULL)
	{
		printf("打开文件失败!\n");
		exit(0);
	}
	if((p=(reader *)malloc(sizeof(reader)))==NULL)
		{
			printf("分配内存失败!");
			exit(0);
		}
	if((temp=fgetc(file))==EOF)
	{
		printf("未有读者录入!\n");
		return; 
	 } 
	 rewind(file);
	while(!feof(file))
	{
		fscanf(file,"%d %s %d %d\n",&p->num,p->name,&p->maxQuota,&p->borrow);
		printf("读者编号:%d\t读者姓名:%s\t读者最大借阅数: %d\t已借阅数量: %d\n",p->num,p->name,p->maxQuota,p->borrow);
	}
	fclose(file); 
 }
 //查询读者信息
 void findReader()
 {
 	FILE *file;
 	reader *p;
 	int ch=0;
 	char name[20];
 	if((file=fopen("reader.txt","r"))==NULL)
	{
		printf("打开文件失败!\n");
		exit(0);
	}
	if((p=(reader *)malloc(sizeof(reader)))==NULL)
	{
		printf("分配空间失败!\n");
		exit(0);
	}
	printf("请输入要查询读者的名字:");
	scanf("%s",name);
	while(!feof(file))
	{
		fscanf(file,"%d %s %d %d\n",&p->num,p->name,&p->maxQuota,&p->borrow);
		if(strcmp(name,p->name)==0)
		{
			ch=1;
			printf("读者编号:%d\t读者姓名:%s\t读者最大借阅数: %d\t已借阅数量: %d\n",p->num,p->name,p->maxQuota,p->borrow);
		}
	}
	if(ch==0)
	 printf("未找到该读者,请重新输入!\n");
	fclose(file);
  } 
 //删除读者信息
 void deleteReader()
 {
 	FILE *file;
 	char name[20],ch='a';
 	reader *p,*head,*tail;
 	head=tail=NULL;
 	if((file=fopen("reader.txt","r"))==NULL)
	{
		printf("打开文件失败!\n");
		exit(0);
	}

	while(!feof(file))
	{
		if((p=(reader *)malloc(sizeof(reader)))==NULL)
		{
			printf("分配内存失败!");
			exit(0);
		}
		fscanf(file,"%d %s %d %d\n",&p->num,p->name,&p->maxQuota,&p->borrow);
		p->next=NULL;
		if(head==NULL)
			head=p;
		else
			tail->next=p;
			tail=p;
	}
	printf("请输入你要删除的读者姓名:");
	scanf("%s",name);
	fclose(file);
	if((file=fopen("reader.txt","w"))==NULL)
	{
		printf("打开文件失败!\n");
		exit(0);
	}
	while(head!=NULL)
	{
		
			if(strcmp(name,head->name)==0)
			{
				printf("读者编号:%d\t读者姓名:%s\t读者最大借阅数: %d\t已借阅数量: %d\n",head->num,head->name,head->maxQuota,head->borrow);
				printf("是否删除,是(Y)/否(N):");
				ch=getchar();
				ch=getchar();
				if(ch=='Y'||ch=='y')
				{
					head=head->next;
					continue;
				}
			}
			fprintf(file,"%d %s %d %d\n",head->num,head->name,head->maxQuota,head->borrow);
			head=head->next; 
	}
	if(ch=='Y'||ch=='y')
	{
		printf("删除成功!\n");
	}
	else
		printf("删除失败!\n"); 
	if(ch=='a')
	 printf("未找到该读者,请重新输入!\n");
	fclose(file); 
  } 
  //修改读者信息函数
   void updateReader()
   {
   	FILE *file;
	if((file=fopen("reader.txt","r"))==NULL)
	{
		printf("打开文件失败!");
		  exit(0); 
	}
	
	reader *head,*tail,*p,*q;
	char name[20],ch='a';
	int n,temp; 
	head=tail=NULL;
	printf("请输入要修改的读者姓名:");
	scanf("%s",name);
	while(!feof(file))
	{
		if((p=(reader *)malloc(sizeof(reader)))==NULL)
		{
			printf("分配空间失败!");
			exit(0); 	
		}
		fscanf(file,"%d %s %d %d\n",&p->num,p->name,&p->maxQuota,&p->borrow);	
		if(head==NULL)
		head=p;
		else
		tail->next=p;
		tail=p;
		tail->next=NULL;
	}
	fclose(file);
	
	q=head;
	
	while(head!=NULL)
	{ 
		if(strcmp(name,head->name)==0)
		{
			printf("读者编号:%d\t读者姓名:%s\t读者最大借阅数: %d\t已借阅数量: %d\n",head->num,head->name,head->maxQuota,head->borrow);		
			printf("是否修改 是(Y)/否(N):");
			ch=getchar();
			ch=getchar();
		
		if(ch=='Y'||ch=='y')
			{
				printf("\t\t\t1、修改读者编号\n");
				printf("\t\t\t2、修改读者姓名\n");
				printf("\t\t\t3、修改读者最大借阅额度\n");
				printf("\t\t\t4、修改读者已借阅数量\n");
				printf("\t\t\t0、退出\n");
				printf("请输入你要进行的操作:");
				temp=scanf("%d",&n);
				fflush(stdin);
				while(temp==0||temp==-1) 
				{
					printf("输入有误,请重新输入:");
					temp=scanf("%d",&n);
					fflush(stdin);
					if(temp==1)
					break;
				}
				
				
				switch(n)
				{
					case 1: printf("请输入修改后的读者编号:");scanf("%d",&head->num);break;	
					case 2: printf("请输入修改后的读者姓名:");scanf("%s",head->name);break;
					case 3: printf("请输入修改后的读者最大借阅额度:");scanf("%d",&head->maxQuota);break;
					case 4: printf("请输入修改后的读者已借阅数量:");scanf("%d",&head->borrow);break; 
					case 0: break;
					default: printf("输入错误,请重新输入!\n"); 
				} 
				printf("修改成功!\n");		
				break; 
			}
			else
			{
				printf("修改失败!\n");
				break;;
			}
		}
		head=head->next;
	}
		if((file=fopen("reader.txt","w"))==NULL)
	   		{
	   			printf("打开文件失败!");
			  	exit(0); 
			}
			while(q!=NULL)
			{
				fprintf(file,"%d %s %d %d\n",q->num,q->name,q->maxQuota,q->borrow);
				q=q->next;
			}
			fclose(file);
			if(ch=='a')
			printf("未找到该图书!请重新输入!\n");
 } 
 //借书操作 
void borrowBook()
{
	FILE *file;
	FILE *file1;
	FILE *file2;
	reader *head,*tail;
	head=tail=NULL;
	char name[20],bookName[20],num;
	int ch=0,empty=0,maxQuota,borrow=0;
	reader *p;
	book *q,*first,*last;
 	first=last=NULL;
 	
	if((file=fopen("reader.txt","r"))==NULL)
	{
		printf("文件打开失败!\n");
		exit(0);
	}

	if(fgetc(file)==-1)
		{
			printf("未有读者录入!\n");
			return;
		}
	printf("请输入你的名字:"); 
	scanf("%s",name);
	getchar();
	rewind(file);
	while(!feof(file))
	{
		if((p=(reader *)malloc(sizeof(reader)))==NULL)
		{
			printf("分配内存失败!\n");
			exit(0);
		 }
		fscanf(file,"%d %s %d %d\n",&p->num,p->name,&p->maxQuota,&p->borrow);	
		if(strcmp(p->name,name)==0)
		{
			printf("查到该读者!\n");
			maxQuota=p->maxQuota;
			num=p->num;
			borrow=p->borrow;
			ch=1;
		}
		if(head==NULL)
			head=p;
		else 
			tail->next=p;
		tail=p;
		tail->next=NULL; 
	}

	if(ch!=1)
		{
			printf("未查到该读者,请先注册!\n");
			return;
		}
	if(maxQuota==0||(borrow==maxQuota))
		{
			printf("您最大借阅额度不足,不能借书!\n");
			return;
		} 
	fclose(file);
	printf("请输入要借阅的图书:");
	scanf("%s",bookName);
	getchar();
	if((file=fopen("book.txt","r+"))==NULL)
	{
		printf("文件打开失败!\n");
		exit(0);
	}
	if((file1=fopen("borrow.txt","w"))==NULL)
	{
		printf("文件打开失败!\n");
		exit(0);
	}
	if(fgetc(file)==-1)
		{
			printf("未有图书录入!\n");
			return;
		}
	rewind(file);
	while(!feof(file))
	{
		if((q=(book *)malloc(sizeof(book)))==NULL)
		{
			printf("分配内存失败!\n");
			exit(0);
		 } 
		fscanf(file,"%d %s %s %d %s %d %d %d %d %s\n",&q->bookNum,q->bookName,q->author,&q->flNum,q->publish,&q->publishTime[0],&q->publishTime[1],&q->publishTime[2],&q->saveNum,q->value);
		if(strcmp(q->bookName,bookName)==0)
		{
			//saveNum=q->saveNum;
			empty=1;
			
			if(q->saveNum==0)
				{
					printf("该图书未有库存\n");
				}
				else
				{
					q->saveNum--;
					borrow++;
					fprintf(file1,"%d %s %d %s %d\n",q->bookNum,q->bookName,num,name,q->saveNum);
				}
		}
			if(empty==1)
			{
				printf("查到该图书!\n");
				printf("借书成功!\n"); 
			}
			else
			{
				printf("未查到该图书!请重新输入!\n");
				return;
			}
		if(first==NULL)
			first=q;
		else 
			last->next=q;
		last=q;
		last->next=NULL; 
	}
	rewind(file);
	while(first!=NULL)
	{
		fprintf(file,"%d %s %s %d %s %d %d %d %d %s\n",first->bookNum,first->bookName,first->author,first->flNum,first->publish,first->publishTime[0],first->publishTime[1],first->publishTime[2],first->saveNum,first->value);
		first=first->next;
	}
	if((file2=fopen("reader.txt","w"))==NULL)
	{
		printf("文件打开失败!\n");
		exit(0);
	}
	while(head!=NULL)
	{
		if(strcmp(name,head->name)==0)
		{
			head->borrow=borrow; 
		}
		fprintf(file2,"%d %s %d %d\n",head->num,head->name,head->maxQuota,head->borrow);
		head=head->next; 
	}
	free(q);
	free(p);
	fclose(file1);
	fclose(file);
	fclose(file2);
}
//还书操作 
void repay()
{
	
	FILE *file;
	FILE *file1;
	FILE *file2;
	char name[20],bookName[20],ch;
	int n,saveNum,temp;
	borrow *head,*tail,*p;
	book *first,*last,*q;
	reader *start,*end,*k;
	head=tail=NULL;
	first=last=NULL;
	start=end=NULL;
	if((file=fopen("borrow.txt","r"))==NULL)
	{
		printf("文件打开失败!\n");
		exit(0);
	}
	if((file1=fopen("book.txt","r+"))==NULL)
	{
		printf("文件打开失败!\n");
		exit(0);
	}
	if((file2=fopen("reader.txt","r+"))==NULL)
	{
		printf("文件打开失败!\n");
		exit(0);
	}
	if(fgetc(file)==-1)
		{
			printf("未有读者借阅图书!\n");
			return;
		}
	printf("请输入您的名字:");
	scanf("%s",name); 
	getchar();
	rewind(file);
	while(!feof(file))
	{
		if((p=(borrow *)malloc(sizeof(borrow)))==NULL)
		{
			printf("分配内存失败!\n");
			exit(0);
		 } 
		fscanf(file,"%d %s %d %s %d\n",&p->bookNum,p->bookName,&p->num,p->name,&p->saveNum);
		if(strcmp(name,p->name)==0)
		{
			printf("图书编号:%d\t 图书名称:%s\t 读者编号:%d\t 读者姓名:%s\t 图书库存数量:%d\n",p->bookNum,p->bookName,p->num,p->name,p->saveNum);
			n=1;
		}
		if(head==NULL)
			head=p;
		else 
			tail->next=p;
		tail=p;
		tail->next=NULL; 
	}

	if(n==1)
	printf("查找完毕!\n");
	else
	{
	printf("未查到该读者借书信息!\n");
	return;
	}
	
	fclose(file);
	if((file=fopen("borrow.txt","w"))==NULL)
	{
		printf("文件打开失败!\n");
		exit(0);
	}
	printf("请输入您要还的图书名称:");
	gets(bookName); 
	rewind(file);
	while(head!=NULL)
	{
		if((strcmp(bookName,head->bookName)==0)&&(strcmp(name,head->name)==0))
			{
				printf("是否确认还书 是(Y)/否(N):");
				ch=getchar(); 
				if(ch=='y'||ch=='Y')
					{
						saveNum=head->saveNum+1;
						head=head->next;
						printf("还书成功\n");
						temp=1;
						
						continue;
					}
				else
					printf("还书失败!\n");	
			}
			fprintf(file,"%d %s %d %s %d\n",head->bookNum,head->bookName,head->num,head->name,head->saveNum);
			head=head->next;
	}
	if(temp!=1)
	{
		printf("未查到该图书!请重新输入!\n");
		return; 
	 } 
	
	while(!feof(file1))
	{
		if((q=(book *)malloc(sizeof(book)))==NULL)
		{
			printf("分配内存失败!\n");
			exit(0);
		 } 
		fscanf(file1,"%d %s %s %d %s %d %d %d %d %s\n",&q->bookNum,q->bookName,q->author,&q->flNum,q->publish,&q->publishTime[0],&q->publishTime[1],&q->publishTime[2],&q->saveNum,q->value);
		if(strcmp(q->bookName,bookName)==0)
		{
			q->saveNum=saveNum;
		}
		if(first==NULL)
			first=q;
		else 
			last->next=q;
		last=q;
		last->next=NULL; 
	}
	while(!feof(file2))
	{
		if((k=(reader *)malloc(sizeof(reader)))==NULL)
		{
			printf("分配内存失败!\n");
			exit(0);
		 } 
		fscanf(file2,"%d %s %d %d\n",&k->num,k->name,&k->maxQuota,&k->borrow);
		if(strcmp(k->name,name)==0)
		{
			if(temp==1&&k->borrow!=0)
				k->borrow--; 
			if(k->borrow==0)
				k->borrow=0; 
		}
		if(start==NULL)
			start=k;
		else 
			end->next=k;
		end=k;
		end->next=NULL; 
	}

	rewind(file1);
	rewind(file2);
	while(first!=NULL)
	{
		fprintf(file1,"%d %s %s %d %s %d %d %d %d %s\n",first->bookNum,first->bookName,first->author,first->flNum,first->publish,first->publishTime[0],first->publishTime[1],first->publishTime[2],first->saveNum,first->value);
		first=first->next; 
	}
	while(start!=NULL)
	{
		fprintf(file2,"%d %s %d %d\n",start->num,start->name,start->maxQuota,start->borrow);
		start=start->next; 
	}

	fclose(file);
	fclose(file1);
	fclose(file2);
 } 
  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值