High-level language (C language) programming-library management system-(structure + file)

1 Introduction
Task background: The book management system is an indispensable information system for everyone in the current society. The use of C language and Dve c++ to make a book management system fundamentally changes the large, cumbersome, and easy-to-lost information of books, thereby improving information management and storage efficiency . Based on the analysis and design of the system, various project components are created, and several functional modules are connected to form a library management system.
The purpose of writing: to realize the addition, deletion, modification of books, query by number, and readers from the query system.
Task overview:
(1) Borrowing material management (add, delete, modify, and query related materials)
(2) Borrow management (including lending operation, book return operation)
(3) Reader management (reader level: can be divided For teachers, students, and define the number of books that can be borrowed for each type of reader)
(4) Statistical analysis (current borrowing and related data status. Statistical analysis of borrowing rankings, data status statistics, borrowing statistics, showing all unreturned books due to the day Analysis of information and other functions)...
2 Introduction to functions and operations

Figure 1 System use case diagram

3 System design analysis
The operations (add, delete, modify, query) of all books' information through file system operations are performed on the memory, and all previous operations can be retained when the program is run again. The
system is divided into seven function modules, respectively Add, delete, modify, query, display, reader system and exit of books.

Code

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
FILE *fp;        //创建文件指针
struct wmy {
    
    
	char  num[88];    //编号
	char name[88];  	//书名
	char writer[88];  	//作者
	char  price[88];     //价格
	char lable[88];    	//类别
	char  time[88];    	//出版时间
	char company[88]; 	//出版单位;
	char  kind ;     	//标记状态是否被借出   1表示可以借阅   2表示已经借阅未归还  0表示不存在这本书
} book;
int cnt=0;
int main() {
    
    

	void reader();   // 读者管理(读者等级:可分为教师、学生,并定义每类读者可借书数量和相关的借阅时间等信息)
	void add();   //添加图书
	void  del();   //删除图书
	char   memu();  //菜单
	void change(); //修改图书信息
	void search(); //查询图书信息
	void output(); //显示所有图书信息

	int zhanghao,mima;             //帐号密码均为666
	cout<<"请输入登录帐号:"<<endl;
	cin>>zhanghao;
	cout<<"密码:"<<endl;
	cin>>mima;
	if(zhanghao==666&&mima==666) {
    
    
		while(1) {
    
    
			switch (memu()) {
    
    
				case  '1':
					add();
					break;
				case  '2':
					output();
					break;
				case  '3':
					del();
					break;
				case  '4':
					change();
					break;
				case  '5':
					search();
					break;
				case  '6':
					reader();
					break;
				case  '7':
					return 0;
					break;
			}
		}
	} else {
    
    
		cout<<"请重新输入:"<<endl;
		main();
	}
	return 0;
}
char  memu() {
    
    
	char x;
	cout<<"欢迎使用图书管理系统"<<endl<<"*********************************************"<<endl;
	cout<<"1.输入图书信息"<<endl;
	cout<<"2.显示所有图书信息"<<endl;
	cout<<"3.删除图书信息" <<endl;
	cout<<"4.修改图书信息" <<endl;
	cout<<"5.查询图书信息" <<endl;
	cout<<"6.读者管理" <<endl;
	cout<<"7.退出图书管理系统" <<endl;
	cout<<"*********************************************"<<endl<<"请选择菜单命令:"<<endl;
	cin>>x;
	return x;        //作为返回值返回到主函数选择功能
}
void add() {
    
    
	struct wmy ac[666];      //创建另一个结构体 作为临时储存源文件信息,
	int i=0;
	FILE *xi=fopen("book.txt","rb+");
	while(fread(&ac[i],sizeof(book),1,xi)==1)
		i++;                                      //每次一字节读book源文件的信息
	int x=i;                                       //确定book里有几本书,从i开始继续添加
	fclose(xi);
	cout<<"请输入图书编号"<<endl;
	cin>>book.num;
	cout<<"请输入书名"<<endl;
	cin>>book.name;
	cout<<"请输入作者"<<endl;
	cin>>book.writer;
	cout<<"请输入出版社"<<endl;
	cin>>book.company;
	cout<<"请输入出版时间"<<endl;
	cin>>book.time;
	cout<<"请输入价格"<<endl;
	cin>>book.price;
	cout<<"请输入分类"<<endl;
	cin>>book.lable;
	book.kind=1;
	ac[x++]=book;                //添加新的图书
	fp=fopen("book.txt","wb");
	for(int j=0; j<x; j++)
		fwrite(&ac[j],sizeof(book),1,fp );      //每次一个字节将新书 的信息写入book
	fclose(fp);                                   //关闭文件,保存数据
}
void del() {
    
    
	FILE *fp1;
	FILE *fp2;
	if((fp1=fopen("book.txt","rb"))== NULL) {
    
            // 以只读的方式 打开,如果指针返回为空,则无法打开
		cout<<endl<<"不能打开book.txt文件"<<endl;
		getchar();
		exit(1);//跳出语句
	}
	if((fp2=fopen("temp.txt","wb"))==NULL) {
    
           //建立一个临时文件temp,以只写的方式打开当temp被占用时无法打开 或指针返回为空时
		cout<<endl<<"不能打开book.txt文件"<<endl;
		getchar();
		exit(1);
	}
	char x[88];
	cout<<"请输入要删除的数目编号:"<<endl;
	cin>>x;
	int y=0;
	while(fread(&book,sizeof(book),1,fp1)==1) {
    
    
		if(strcmp(book.num,x)==0) {
    
    
			y=1;
		} else {
    
    
			fwrite(&book,sizeof(book),1,fp2);
		}
	}
	fclose(fp1);
	fclose(fp2);
	if(y) {
    
    
		cout<<"删除成功"<<endl;
		remove("book.txt");
		rename("temp.txt","book.txt");
	} else
		cout<<"未找到图书"<<endl;
	getchar(),	getchar();
}
void output() {
    
    
	fp=fopen("book.txt","rb");
	cout<<"编号      书名    作者     分类    出版单位    时间     价格"<<endl;
	while(fread(&book,sizeof(book),1,fp)==1) {
    
    
		printf("%s%10s%8s",book.num,book.name,book.writer);
		printf("%10s%8s%12s%10s\n",book.lable,book.company,book.time,book.price  );
	}
	fclose(fp);
	getchar(),	getchar();
}
void change() {
    
    
	FILE *fp1,*fp2;
	int y=1;
	char temp[66],tempname[66],tempwriter[66],templable[66],tempprice[66],tempcompany[66],temptime[66];
	cout<<"请输入要修改的编号:"<<endl;
	cin>>temp;
	if((fp1=fopen("book.txt","rb"))== NULL) {
    
        // 以只读的方式 打开,如果指针返回为空,则无法打开
		cout<<endl<<"不能打开book.txt文件"<<endl;
		getchar();
		exit(1);//跳出语句
	}
	if((fp2=fopen("temp.txt","wb"))==NULL) {
    
         //建立一个临时文件temp,以只写的方式打开当temp被占用时无法打开 或指针返回为空时
		cout<<endl<<"不能打开book.txt文件"<<endl;
		getchar();
		exit(1);
	}

	while(fread(&book,sizeof(book),1,fp1)==1) {
    
    
		if(strcmp(book.num,temp)==0) {
    
    
			y=0;
			cout<<"请输入新的图书名:"<<endl;
			cin>>tempname;
			strcpy(book.name,tempname);
			cout<<"请输入新的作者:"<<endl;
			cin>>tempwriter;
			strcpy(book.writer,tempwriter);
			cout<<"请输入新的分类:"<<endl;
			cin>>templable;
			strcpy(book.lable,templable);
			cout<<"请输入新的出版社:"<<endl;
			cin>>tempcompany;
			strcpy(book.company,tempcompany);
			cout<<"请输入新的出版时间:"<<endl;
			cin>>temptime;
			strcpy(book.time,temptime);
			cout<<"请输入新的价格:"<<endl;
			cin>>tempprice;
			strcpy(book.price,tempprice);

		}
		fwrite(&book,sizeof(book),1,fp2);
	}
	fclose(fp1);
	fclose(fp2);
	if(!y) {
    
    
		cout<<"修改成功"<<endl;
		remove("book.txt");
		rename("temp.txt","book.txt");
	}
	getchar(),	getchar();
}
void reader() {
    
    
	int x;
	cout<<"请输入你的身份:(1学生/2老师)"<<endl;
	cin>>x;
	if(x==1)	cout<<"最多借阅图书数量:4本"<<endl<<"归还期限:一个月"<<endl;
	else if(x==2)	cout<<"最多借阅图书数量:8本"<<endl<<"归还期限:两个月"<<endl;	
	else cout<<"输入错误"<<endl;	
	getchar(),	getchar();
}
void search() {
    
    
	char x[66];
	cout<<"请输入要查询的数目编号:"<<endl;
	int y=0;
	scanf("%s",x);
	while(fread(&book,sizeof(book),1,fp)) {
    
    
		if(strcmp(book.num,x)==0) {
    
    
			y=1;
			cout<<"编号      书名      作者      分类    出版单位    时间    价格"<<endl;
			printf("%s%10s%10s ",book.num,book.name,book.writer);
			printf("%10s%8s%10s%10s\n ",book.lable,book.company,book.time,book.price  );
		}
	}
	if(y==0)	cout<<"未找到书目"<<endl;
	getchar(),	getchar();
}

Guess you like

Origin blog.csdn.net/wmy0536/article/details/106640927