西电C语言程序设计实验之图书馆管理系统

Posted akyna-zh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了西电C语言程序设计实验之图书馆管理系统相关的知识,希望对你有一定的参考价值。

简单文件数据库-模拟图书馆管理系统

涉及知识点:文件读写、内存管理、结构体定义、基本数据结构、高级格式化输入输出

要求:编写一个程序模拟图书管理系统。

用户分为管理员和读者两类,分别显示不同文本格式菜单,通过菜单项对应数字进行选择。

读者菜单包括借书、还书、查询等功能。
管理员菜单包括图书和读者信息录入、修改和删除

图书信息至少应包括:编号、书名、数量
读者信息至少应包括:编号、姓名、所借图书

可根据图书名称或编号进行图书信息查询,可查询某本书现在被哪些读者借走。

命令行参数如下:
Libsim –a(-u) xxxx

第一个参数为可执行程序名称;
第二个参数为用户身份,-a表示管理员,-u表示读者;
第三个参数为用户名

几个函数说明:

展示读者和图书信息

void ShowBook(char* bname);
void ShowReader(char* rname);

图书借阅和归还

void BookLend(char *rname, char *bname);
void BookReturn(char *rname, char* bname);

图书和读者信息增删

void AddBook(char* bname);
void DelBook(char* bname);
void AddReader(char* rname, char* reroot);							
void DelReader(char* rname);

读取读者,管理员和图书信息

void ReadBooksInfo();
void ReadReadersInfo();
void ReadAdministratorsInfo();

更新读者,管理员和图书信息

void UpdateBorrowerList();
void UpdateBorrowedBookList();
void UpdateBookleft();
void UpdateBooks();
void UpdateReaders();
void UpdateAdministrators();
void UpdateReroots();
void UpdateAdroots();

图书馆信息:
在这里插入图片描述
信息编写规则:

每个文件以end作为结尾
而null表示该书或读者已经被删除

books.txt:
每行一本书,第一行的书编号为1
readers.txt:
每行一个读者姓名,第一行的读者编号为1
administrators.txt:
每行一个管理员姓名,第管理员一行的读者编号为1

books'borrowers.txt:
每行若干个人名,第一行对应编号为1的书的借阅人
borrowers'books.txt:
每行若干个书名,第一行对应编号为1的人的在借书

reroots.txt:
每行一个密码,第一行对应编号为1的读者的密码
adroots.txt
每行一个密码,第一行对应编号为1的管理员的密码




源码

#include<stdio.h>
#include<string.h>
typedef struct Book{
	char name[20];//名称 
	int num;      //编号 
	int left;     //库存  
}Book;
typedef struct Reader{
	char name[20];//姓名 
	int num;      //编号
	char root[20];//密码 
}Reader;
typedef struct Administrator{
	char name[20];//姓名 
	int num;   	  //编号
	char root[20];//密码 
}Administrator;  

Book books[110];                  //图书信息 
Reader readers[110];			  //读者信息 
Administrator administrators[10]; //管理员信息 
char borrowerList[110][100];      //图书在借人 
char borrowedBookList[110][100];  //读者在借书 
 
void ShowBook(char* bname);
void ShowReader(char* rname);
void BookLend(char *rname, char *bname);
void BookReturn(char *rname, char* bname);
void ReadBooksInfo();
void ReadReadersInfo();
void ReadAdministratorsInfo();
void UpdateBorrowerList();
void UpdateBorrowedBookList();
void UpdateBookleft();
void UpdateBooks();
void UpdateReaders();
void UpdateAdministrators();
void UpdateReroots();
void UpdateAdroots();
void AddBook(char* bname);
void DelBook(char* bname);
void AddReader(char* rname, char* reroot);							
void DelReader(char* rname);

//Libsim –a(-u) xxxx
//第一个参数为可执行程序名称;第二个参数为用户身份,-a表示管理员,-u表示读者;第三个参数为用户名

int main(int argc, char *argv[]) {
	ReadBooksInfo();
	ReadReadersInfo();
	ReadAdministratorsInfo();
	char *command = argv[1], *name = argv[2];
	printf("Hello, %s! Welcome to XDULibrary!\\n", name);
	if(!strcmp(command, "-a")){
		char root[10];
		printf("Please enter your root:\\n");
		scanf("%s", root);
		int i, flag = 0, choice;
		for(i = 0; i < 10; i++)
			if(!strcmp(administrators[i].root, root))	flag = 1;
		if(!flag){
			printf("wrong root\\n");
			return -1;
		}
		printf("0.exit\\n1.show books\\n2.show readers\\n3.delete readers\\n4.delete books\\n5.add readers\\n6.add books\\n");
		while (1) {
			printf("please choose what to do:\\n");
			scanf("%d", &choice);
			if (choice == 0){
				printf("exit successfully");
				return 0;
			}
			switch(choice){
				case 1:{
					char bname[20];
					printf("please enter book's name:\\n");
					scanf("%s", bname);
					ShowBook(bname);
					break;
				}
				case 2:{
					char rname[20];
					printf("please enter reader's name:\\n");
					scanf("%s", rname);
					ShowReader(rname);
					break;
				}
				case 3:{
					char rname[20];
					printf("please enter reader's name:\\n");
					scanf("%s", rname);																	
					DelReader(rname);
					break;
				}
				case 4:{
					char bname[20];
					printf("please enter book's name:\\n");
					scanf("%s", bname);																	
					DelBook(bname);			
					break;
				}
				case 5:{
					char rname[20], reroot[20];
					printf("please enter reader's name:\\n");
					scanf("%s", rname);
					printf("please enter reader's root:\\n");
					scanf("%s", reroot);
					AddReader(rname, reroot);			
					break;
				}
				case 6:{
					char bname[20];
					printf("please enter book's name:\\n");
					scanf("%s", bname);
					AddBook(bname);		
					break;
				}
				default:{
					printf("illegal input\\n");
					break;
				}
			}
		}
	}
	else if (!strcmp(command, "-u")){
		char root[10];
		printf("Please enter your root:\\n");
		scanf("%s", root);
		int i, flag = 0, choice;
		for(i = 0; i < 10; i++){
			if(!strcmp(readers[i].root, root))	flag = 1;
		}
		if(!flag){
			printf("wrong root\\n");
			return -1;
		}
		printf("0.exit\\n1.show books\\n2.show readers\\n3.lend books\\n4.return books\\n");
		while (1) {

			printf("please choose what to do:\\n");
			scanf("%d", &choice);
			if (choice == 0){
				printf("exit successfully");
				return 0;
			}
			switch(choice){
				case 1:{
					char bname[20];
					printf("please enter book's name:\\n");
					scanf("%s", bname);
					ShowBook(bname);
					break;
				}
				case 2:{
					char rname[20];
					printf("please enter reader's name:\\n");
					scanf("%s", rname);
					ShowReader(rname);
					break;
				}
				case 3:{
					char bname[20];
					printf("please enter book's name:\\n");
					scanf("%s", bname);
					BookLend(name, bname);
					break;
				}
				case 4:{
					char bname[20];
					printf("please enter book's name:\\n");
					scanf("%s", bname);
					BookReturn(name, bname);
					break;
				}
				default:{
					printf("illegal input\\n");
					break;
				}
			}
		}
	}
	else{
		printf("illegal input\\n");
		return -1; 
	}
	return 0;
}
void ReadBooksInfo(){
	FILE *fp1 = fopen("books.txt", "r");
	FILE *fp2 = fopen("booksleft.txt", "r");
	FILE *fp3 = fopen("borrowerList.txt", "r");
	if(fp1 == NULL || fp2 == NULL || fp3 == NULL){
		printf("system error\\n");
		return;
	}
	char t[100];
	int i = 0;
	while(fgets(t, 100, fp1) != NULL){
		if(t[strlen(t)-1] == '\\n')	t[strlen(t)-1] = '\\0';
		strcpy(books[i].name, t);
		books[i].num = i;
		i++;
		if(!strcmp(t, "end"))	break;
	}
	i = 0;
	while(fgets(t, 100, fp2) != NULL){
		if(t[strlen(t)-1] == '\\n')	t[strlen(t)-1] = '\\0';
		if(!strcmp(t, "end")){
			books[i].left = -1;
			break;
		}
		int n = atoi(t);
		books[i].left = n; 
		i++;
	}
	i = 0;		
	while(fgets(t, 100, fp3) != NULL){
		if(t[strlen(t)-1] == '\\n')	t[strlen(t)-1] = '\\0';
		strcpy(borrowerList[i], t);
		i++;
		if(!strcmp(t, "end"))	break;
	}
	fclose(fp1);
	fclose(fp2);
	fclose(fp3);
}
void ReadReadersInfo(){
	FILE *fp1 = fopen("readers.txt", "r");
	FILE *fp2 = fopen("borrowedBookList.txt", "r");
	FILE *fp3 = fopen("reroots.txt", "r");
	if(fp1 == NULL || fp2 == NULL || fp3 == NULL){
		printf("system error\\n");
		return;
	}
	char t[100];
	int i = 0;
	while(fgets(t, 100, fp1) != NULL){
		if(t[strlen(t)-1] == '\\n')	t[strlen(t)-1] = '\\0';
		strcpy(readers[i].name, t);
		readers[i].num = i;
		i++;
		if(!strcmp(t, "end"))	break;
	}
	i = 0;
	while(fgets(t, 100, fp2) != NULL){
		if(t[strlen(t)-1] == '\\n')	t[strlen(t)-1] = '\\0';
		strcpy(borrowedBookList[i], t);
		i++;
		if(!strcmp(t, "end"))	break;
	}
	i = 0;
	while(fgets(t, 100, fp3) != NULL){
		if(t[strlen(t)-1] == '\\n')	t[strlen(t)-1] = '\\0';
		strcpy(readers[i].root, t);
		i++;
		if(!strcmp(t, "end"))	break;
	}
	fclose(fp1);
	fclose(fp2);
	fclose(fp3);
}
void ReadAdministratorsInfo(){
	FILE *fp1 = fopen("administrators.txt", "r");
	FILE *fp2 = fopen("adroots.txt", "r");
	if(fp1 == NULL || fp2 == NULL){
		printf("system error\\n");
		return;
	}
	char t[100];
	int i = 0;
	while(fgets(t, 100, fp1) != NULL){
		if(t[strlen(t)-1] == '\\n')	t[strlen(t)-1] = '\\0';
		strcpy(administrators[i].name, t);
		administrators[i].num = i;
		i++;
		if(!strcmp(t, "end"))	break;
	}
	i = 0;
	while(fgets(t, 100, fp2) != NULL){
		if(t[strlen(t)-1] == '\\n')	t[strlen(t)-1] = '\\0';
		strcpy(administrators[i].root, t);
		i++;
		if(!strcmp(t, "end"))	break;
	}
	fclose(fp1);
	fclose(fp2);
}
void UpdateBorrowerList(){
	FILE *fw = fopen("borrowerList.txt", "w");
	if(fw == NULL){
		printf("system error\\n");
		return;
	}
	int k;
	for(k = 0; k < 100; k++){
		fputs(borrowerList[k], fw);
		fputc('\\n', fw);
		if(!strcmp(borrowerList[k], "end"))	break;
	}
	fclose(fw);
}
void UpdateBorrowedBookList(){
	FILE *fw = fopen("borrowedBookList.txt", "w");
	if(fw == NULL){
		printf("system error\\n");
		return;
	}
	int k;
	for(k = 0; k < 100; k++){
		fputs(borrowedBookList[k], fw);
		fputc('\\n', fw);
		if(!strcmp(borrowedBookList[k], "end"))	break;
	}
	fclose(fw);
}
void UpdateBookleft(){
	FILE *fw = fopen("booksleft.txt", "w");
	if(fw == NULL){
		printf("system error\\n");
		return;
	}
	int k;
	for(k = 0; k < 100; k++){
		char x[10];
		if(books[k].left == -1){
			fputs("end", fw);
			break;
		}
		itoa(books[k].left, x, 10);
		fputs(x, fw);
		fputc('\\n', fw);
		
	}
	fclose(fw);
}
void UpdateBooks(){
	FILE *fw = fopen("books.txt", "w");
	if(fw == NULL){
		printf("system error\\n");
		return;
	}
	int i;
	for(i = 0; i < 100; i++){
		if(!strcmp(books[i].name, "end")){
			fputs("end", fw);
			fclose(fw);
			return;
		}
		fputs(books[i].name, fw);
		fputc('\\n', fw);
	}
}
void UpdateReaders(){
	FILE *fw = fopen("readers.txt", "w");
	if(fw == NULL){
		printf("system error\\n");
		return;
	}
	int i;
	for(i = 0; i < 100; i++){
		if(!strcmp(readers[i].name, "end")){
			fputs("end", fw);
			fclose(fw);
			return;
		}
		fputs(readers[i].name, fw);
		fputc('\\n', fw);
	}	
}
void UpdateAdministrators(){
	FILE *fw = fopen("administrators.txt", "w");
	if(fw == NULL){
		printf("system error\\n");
		return;
	}
	int i;
	for(i = 0; i < 100; i++){
		if(!strcmp(administrators[i].name, "end")){
			fputs("end", fw);
			fclose(fw);
			return;
		}
		fputs(administrators[i].name, fw);
		fputc('\\n', fw);
	}
}
void UpdateReroots(){
	FILE *fw = fopen("reroots.txt", "w");
	if(fw == NULL){
		printf("system error\\n");
		return;
	}
	int i;
	for(i = 0; i < 100; i++){
		if(!strcmp(readers[i].root, "end")){ 
			fputs("end", fw);
			fclose(fw);
			return;
		}
		fputs(readers[i].root, fw);
		fputc('\\n', fw);
	} 
}
void UpdateAdroots(){
	FILE *fw = fopen("adroots.txt", "w");
	if(fw == NULL){
		printf("system error\\n");
		return;
	}
	int i;
	for(i = 0; i < 100; i++){
		if(!strcmp(administrators[i].root, "end")){
			fputs("end", fw);
			fclose(fw);
			return;
		}
		fputs(administrators[i].root, fw);
		fputc('\\n', fw);
	}
}
void ShowBook(char* bname){
	int i;
	for(i = 0; i < 100; i++){
		if(!strcmp(bname, books[i].name)){
			printf("name: %s\\n", books[i].name);
			printf("num: %d\\n", books[i].num);
			printf("left: %d\\n", books[i].left);
			printf("the borrowers: %s\\n", borrowerList[i]);
		}
		if(!strcmp(books[i].name, "end"))	break;
	}
}
void ShowReader(char* rname){
	int i;
	for(i = 0; i < 100; i++){		
		if(!strcmp(rname, readers[i].name)){
			printf("name: %s\\n", readers[i].name);
			printf("num: %d\\n", readers[i].num);
			printf("borrowed books: %s\\n", borrowedBookList[i]);
		}
		if(!strcmp(books[i].name, "end"))	break;
	}
}
void BookLend(char *rname, char *bname){
	int i, flag = 0;
	for(i = 0; i < 100; i++){
		if(!strcmp(books[i].name, bname)){
			if(books[i].left == 0){
				printf("sorry, the book is borrowed out\\n");
				return;
			}else{
				books[i].left--;
				if(!strcmp(borrowerList[i], "end")){
					strcpy(borrowerList[i], rname);
					strcpy(borrowerList[i+1], "end");
				}else{
					strcat(borrowerList[i], " ");
					strcat(borrowerList[i], rname);
				}
				int j;
				for(j = 0; j < 100; j++){
					if(!strcmp(readers[j].name, rname)){
						if(!strcmp(borrowedBookList[j], "end")){
							strcpy(borrowedBookList[j], bname);
							strcpy(borrowedBookList[j+1], "end");
						}else{
							strcat(borrowedBookList[j], " ");
							strcat(borrowedBookList[j], bname);
						}
					}
				}
				flag = 1;
				break;
			}
		}
	}
	UpdateBorrowerList();
	UpdateBorrowedBookList();
	UpdateBookleft();
	if(flag)	printf("success\\n");
	else	printf("fail\\n");
	ReadBooksInfo();
	ReadReadersInfo();
}
void BookReturn(char *rname, char* bname){
	int i, flag = 0;
	for(i = 0; i < 100; i++){
		if(!strcmp(books[i].name, bname)){
			books[i].left++;
			char *des1 = strstr(borrowerList[i], rname), *des2 = strstr(borrowerList[i], rname);
			char p[100];
			des1 += strlen(rname);
			if(des1 != NULL){
				strcpy(p, des1);
				strcpy(des2, p);
			}
			int j;
			for(j = 0; j < 100; j++){
				if(!strcmp(readers[j].name, rname)){
					char *des3 = strstr(borrowedBookList[j], bname), *des4 = strstr(borrowedBookList[j], bname);
					char q[100];
					des3 += strlen(bname);
					if(des3 != NULL){
						strcpy(q, des3);
						strcpy(des4, q);
					}
				}
			}
			flag = 1;
			break;
		}
	}
	UpdateBorrowerList();
	UpdateBorrowedBookList();
	UpdateBookleft();
	if(flag)	printf("success\\n");
	else	printf("fail\\n");
	ReadBooksInfo();
	ReadReadersInfo();
}
void AddBook(char* bname){
	int i, flag = 0;
	for(i = 0; i < 100; i++){
		if(!strcmp(books[i].name, "end")){
			strcpy(books[i].name, bname);
			books[i].num = i;
			books[i].left = 3;
			strcpy(books[i+1].name, "end");
			books[i+1].left = -1;
			flag = 1;
			break;
		}
	}
	UpdateBookleft();
	UpdateBooks();
	if(flag)	printf("success\\n");
	else	printf("fail\\n");
}
void DelBook(char* bname){
	int i, flag = 0;
	for(i = 0; i < 100; i++){
		if(!strcmp(books[i].name, bname)){
			if(books[i].left != 3){
				printf("fail\\n");
				return;
			}
			strcpy(books[i].name, "null");
			books[i].left = 0;
			flag = 1;
			break;
		}
	}
	UpdateBookleft();
	UpdateBooks();
	if(flag)	printf("success\\n");
	else	printf("fail\\n");
}
void AddReader(char* rname, char* reroot){
	int i, flag = 0;
	for(i = 0; i < 100; i++){
		if(!strcmp(readers[i].name, "end")){
			strcpy(readers[i].name, rname);
			strcpy(readers[i].root, reroot);
			readers[i].num = i;
			strcpy(readers[i+1].name, "end");
			strcpy(readers[i+1].root, "end");
			flag = 1;
			break;
		}
	}
	UpdateReaders();
	UpdateReroots();
	if(flag)	printf("success\\n");
	else	printf("fail\\n");
}													
void DelReader(char* rname){
	int i, flag = 0;
	for(i = 0; i < 100; i++){
		if(!strcmp(readers[i].name, rname)){
			if(!strcmp(borrowedBookList[i], "null")){
				printf("fail\\n");
				return;
			}
			strcpy(readers[i].name, "null");
			flag = 1;
			break;
		}
	}
	UpdateReaders();
	if(flag)	printf("success\\n");
	else	printf("fail\\n");
}

由于测试点和文本文件较多,这里就不进行测试了,朋友们可以自己试试,基本上是ok的。

需要完整图书馆管理系统(即还包含了文本文件的)的可以在评论区留言哦。也欢迎去github或gitee进行clone,后续若发现程序有问题,会进行版本更新。

https://gitee.com/jiang-zhihang/library-system.git
https://github.com/akynazh/LibrarySystem.git

一键三连是对创作者最大的鼓励~

end

以上是关于西电C语言程序设计实验之图书馆管理系统的主要内容,如果未能解决你的问题,请参考以下文章

西电C语言程序设计实验之图书馆管理系统

西电C语言程序设计实验之位图图像文件缩放

西电C语言程序设计实验之位图图像文件缩放

西电C语言程序设计实验之位图图像文件缩放

西电C语言程序设计实验之RLE解压缩算法

西电C语言程序设计实验之RLE解压缩算法