⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tsg.cpp

📁 图书馆管理系统
💻 CPP
字号:
#include "iostream"
#include "stdlib.h"
#include "stdio.h"
#include "malloc.h"
#include "string.h"
#define N 32
#define LEN sizeof(struct book)//book结构体长度
#define LEN2 sizeof(struct readers)//读者结构体长度
using namespace std;
struct book{//图书变量
	char num[10];//书号
	char name[15];//书名
    char author[15];//作者
	char pc[15];//出版社
	int flag;//库存
}a[N],b;
struct date{//日期
	int year;
	int month;
	int day;
};
struct readers{//读者变量
	char identification[10];
	char num[10];
	char name[15];
    char author[15];
	char pc[15];
	struct date p;//日期
}e[N],g;
void get(struct book &b){//输入函数1
	cout<<"请输入书号:"<<endl;
	cin>>b.num;
    cout<<"请输入作者名:"<<endl;
    cin>>b.author;
    cout<<"请输入出版社:"<<endl;
    cin>>b.pc;
    cout<<"请输入存量:"<<endl;
	cin>>b.flag;
}
void Add(struct book a[],struct book &b){//增加库存
    FILE *fp;
	int f=-1;//一个标志
	int i,j;
    if((fp=fopen("book.txt","r"))==NULL){//读文件,若没文件则建新的文件
		fp=fopen("book.txt","a");
		fclose(fp);
		fp=fopen("book.txt","r");
	}
    cout<<"请输入书名:"<<endl;
    cin>>b.name;
	for(i=0;fread(&a[i],LEN,1,fp)==1;i++){//从文件读到结构体数组
		if(strcmp(b.name,a[i].name)==0){//有该书则只增加存量
			a[i].flag++;
			f=1;//改变标志
		}
	}
	fclose(fp);
	fp=fopen("book.txt","w");//从新打开文件并删除文件中内容
	for(j=0;j<i;j++) fwrite(&a[j],LEN,1,fp);//把结构体数组内的资料从新存进文件
	if(f==-1){//如果没该书则要输入相关资料
		get(b);
	    fwrite(&b,LEN,1,fp);
	}
	f=-1;
	fclose(fp);
}
void Delete(struct book a[],struct book &b){//清除库存
	FILE *fp;
	int i,j;
	fp=fopen("book.txt","r");
	cout<<"输入要删除的图书:"<<endl;
	cin>>b.name;
	for(i=0;fread(&a[i],LEN,1,fp)==1;){//从文件读到结构体数组
		if(strcmp(b.name,a[i].name)!=0) i++;//找到该书i则不自加
	}			
	fclose(fp);
    fp=fopen("book.txt","w");
	for(j=0;j<i;j++) fwrite(&a[j],LEN,1,fp);//把结构体数组内的资料从新存进文件
	fclose(fp);
}
void print1(){//输出函数1
    cout<<"   书号";
	cout<<"         书名";
	cout<<"         作者";
	cout<<"         出版社";
	cout<<"         存量"<<endl;
}
void print2(struct book &c){//输出函数2
    printf("%-14s",c.num);
	printf("%-13s",c.name);
	printf("%-13s",c.author);
	printf("%-13s",c.pc);
	printf("%7d\n",c.flag);
}
void print3(){//输出函数3
    cout<<"   书号";
	cout<<"        书名";
	cout<<"        作者";
	cout<<"        出版社";
	cout<<"        ID";
	cout<<"       应归还日期"<<endl;
}
void print4(struct readers &l){//输出函数4
    printf("%-13s",l.num);
	printf("%-12s",l.name);
	printf("%-12s",l.author);
	printf("%-12s",l.pc);
	printf("%-12s",l.identification);
	printf("%3d",l.p.year);
	cout<<"-"<<l.p.month<<"-"<<l.p.day<<endl;
}
void search(struct book &b){//查询
	FILE *fp;
	int f=-1;
	int c;
	int i;
	fp=fopen("book.txt","r");
	cout<<"1.按书号查找:"<<endl;
	cout<<"2.按书名查找:"<<endl;
	cout<<"3.按作者查找:"<<endl;
	cout<<"4.按出版社查找:"<<endl;
	cout<<"5.返回"<<endl;
	cin>>c;
	getchar();
	switch(c){
	case 1:{//按书号查找
		cout<<"输入要查找的图书的书号:"<<endl;
		cin>>b.num;
		print1();
	    for(i=0;fread(&a[i],LEN,1,fp)==1;i++){
			if(strcmp(b.num,a[i].num)==0) print2(a[i]);
		}
		break;
	};
	case 2:{//按书名查找
		cout<<"输入要查找的图书的书名:"<<endl;
		cin>>b.name;
		print1();
	    for(i=0;fread(&a[i],LEN,1,fp)==1;i++){
			if(strcmp(b.name,a[i].name)==0) print2(a[i]);
		}
		break;
	};
	case 3:{//按作者查找
		cout<<"输入要查找的图书的作者:"<<endl;
		cin>>b.author;
		print1();
	    for(i=0;fread(&a[i],LEN,1,fp)==1;i++){
			if(strcmp(b.author,a[i].author)==0) print2(a[i]);
		}
		break;
	};
	case 4:{//按出版社查找
		cout<<"输入要查找的图书的出版社:"<<endl;
		cin>>b.pc;
		print1();
	    for(i=0;fread(&a[i],LEN,1,fp)==1;i++){
			if(strcmp(b.pc,a[i].pc)==0) print2(a[i]);
		}
		break;
	};
	case 5: return;//返回
	}			
	fclose(fp);
}
void borrow(struct readers &g,struct book &b,struct book a[]){//借书
	FILE *fp1;
	FILE *fp2;
	int i,j;
	char k;
	fp1=fopen("book.txt","r");
	fp2=fopen("readers.txt","a");
	cout<<"输入要借的图书的书号:"<<endl;
	cin>>b.num;
	for(i=0;fread(&a[i],LEN,1,fp1)==1;i++){
		if(strcmp(b.num,a[i].num)==0){
			if(a[i].flag>0){
				a[i].flag--;//存量减1
				cout<<"输入读者证件号:"<<endl;
				cin>>g.identification;
				getchar();
				cout<<"输入日期:"<<endl;
				cin>>g.p.year>>k>>g.p.month>>k>>g.p.day;//输入借的日期,例如:2008-3-31
				if(g.p.month+3>12){//存储应归还日期
					g.p.year++;
					g.p.month=(g.p.month+3)-12;
				}
				for(j=0;a[i].num[j]!=NULL;j++){//把借出的书资料存到读者结构体
					g.num[j]=a[i].num[j];
				}
				g.num[j]=NULL;
				for(j=0;a[i].name[j]!=NULL;j++){
					g.name[j]=a[i].name[j];
				}
				g.name[j]=NULL;
				for(j=0;a[i].author[j]!=NULL;j++){
					g.author[j]=a[i].author[j];
				}
				g.author[j]=NULL;
				for(j=0;a[i].pc[j]!=NULL;j++){
					g.pc[j]=a[i].pc[j];
				}
				g.pc[j]=NULL;        
				fwrite(&g,LEN2,1,fp2);//把借出的书资料存到读者文件
			}
		}
	}
	fclose(fp1);
    fclose(fp2);
	fp1=fopen("book.txt","w");
	for(j=0;j<i;j++) fwrite(&a[j],LEN,1,fp1);//改变存量后又重新存进book文件
	fclose(fp1);
}
void return1(struct readers &g,struct book &b,struct book a[]){//归还
    FILE *fp1;
	FILE *fp2;
	int i,j;
	int f=-1;//一个标志
	fp1=fopen("book.txt","r");
	fp2=fopen("readers.txt","a");
	cout<<"请输入要还的书号:";
	cin>>g.num;
	cout<<"输入读者证件号:"<<endl;
	cin>>g.identification;
	getchar();
	for(i=0;fread(&e[i],LEN2,1,fp2)==1;i++){
		if(strcmp(g.num,e[i].num)==0&&strcmp(g.identification,e[i].identification)==0){//找到读者文件的该书
			f=1;//改变标志
			break;
		}
	}
	i--;//该书从读者文件删除
	for(;fread(&e[i],LEN2,1,fp2)==1;i++);//把剩下的读到结构体数组
	fclose(fp2);
	fp2=fopen("readers.txt","w");
	for(j=0;j<i;j++) fwrite(&e[j],LEN2,1,fp2);
	fclose(fp2);
	for(i=0;fread(&a[i],LEN,1,fp1)==1;i++){
		if(strcmp(g.num,a[i].num)==0&&f==1) a[i].flag++;//增加存量
	}
	fclose(fp1);
	fp1=fopen("book.txt","w");
    for(j=0;j<i;j++) fwrite(&a[j],LEN,1,fp1);//增加存量后资料存进文件
	fclose(fp1);
	f=-1;
}
void borrowstatus(struct readers e[]){//输出借书情况
	FILE *fp;
	int i;
	fp=fopen("readers.txt","r");
	print3();
	for(i=0;fread(&e[i],LEN2,1,fp)==1;i++)  print4(e[i]);
	fclose(fp);
}
void borrowsystem(){//借还系统
	int c;
	cout<<"1.借阅"<<endl;
	cout<<"2.归还"<<endl;
	cout<<"3.查看借书情况"<<endl;
	cout<<"4.返回"<<endl;
	cin>>c;
	getchar();
	if(c==1){
		borrow(g,b,a);
	}
	else if(c==2){
		return1(g,b,a);
	}
	else if(c==3){
		borrowstatus(e);
	}
	else return;
}
void main(){
	int c;
	while(1){
		cout<<"1.采编入库"<<endl;
		cout<<"2.清除库存"<<endl;
		cout<<"3.查询"<<endl;
		cout<<"4.借还系统"<<endl;
		cout<<"5.退出"<<endl;
		cin>>c;
		switch(c){
		case 1:Add(a,b); break;
		case 2:Delete(a,b); break;
		case 3:search(b); break;
		case 4:borrowsystem(); break;
		case 5:exit(0); break;
		}
	}
}
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -