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

📄 图书借阅系统.cpp

📁 图书借阅系统 1. 每本书有书号、书名和作者姓名; 2. 读者有借书证号、读者姓名、最大借阅量; 3. 能提供若干本书由读者进行选择
💻 CPP
字号:
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
class lending
{
public:
	void inputbook();//设置录入图书函数
	void inputreader();//设置录入读者函数
	void printbook();//设置打印图书情况函数
	void printreader();//设置打印读者情况函数
	void printborrow();//设置打印读者借书的情况
	void search();//设置查询图书函数
	void Delete();//设置查处图书函数
	void exchange();//设置修改图书函数
	void borrow();//设置读者借书情况
    void place();//设置截面函数
private:
	 string title;//书名
	 int booknumber;//书号
	 string writer;//作者
     long lendnumber;//借书证号
	 string readername;//读者姓名
	 int maxlending;//最大借书量

};
void lending::inputbook()//输入图书信息
{
	int i,j=1;
	ofstream outfile("f1.dat",ios::app);//建立文件,用于存储录入的内容
	if(!outfile)
	{
		cerr<<"Open f1.dat error!"<<endl;
	    exit(1);
	}
	while(i)
	{
		cout<<"请输入第"<<j<<"本书的书名:";
		cin>>title;
        outfile<<title<<'\t';
		cout<<"请输入第"<<j<<"本书的书号:";
		cin>>booknumber;
	    outfile<<booknumber<<'\t';
		cout<<"请输入第"<<j<<"本书的作者:";
		cin>>writer;
	    outfile<<writer<<'\t'<<endl;
		j++;
		cout<<"继续录入?"<<"(1.是;0.否.)"<<endl;
		cin>>i;
	}
	outfile.close();
    ofstream coutfile("f1.dat1",ios::app);
	if(!outfile)
	{
		cerr<<"Open f1.dat1 error!"<<endl;
	    exit(1);
	}
	coutfile<<j-1<<'\t'<<endl;
	coutfile.close();
	
}
void lending::inputreader()//输入读者信息
{
	int n,m(1);
	ofstream outfile1("f2.dat",ios::app);//建立文件,用于存储录入的内容
	if(!outfile1)
	{
		cerr<<"Open f2.dat error!"<<endl;
	    exit(1);
	}
	outfile1<<"最大借阅量为5本"<<endl;
	cout<<"请先录入读者的信息:"<<endl;
	while(n)
	{
		cout<<"请输入第"<<m<<"位读者的借书证号:";
		cin>>lendnumber;
		outfile1<<lendnumber<<'\t';
		cout<<"请输入第"<<m<<"位读者的姓名:";
		cin>>readername;
        outfile1<<readername<<'\t'<<endl;
		m++;
		cout<<"继续录入?"<<"(1.是;0.否.)"<<endl;
		cin>>n;
	}
		outfile1.close();
		ofstream coutfile("f2.dat2",ios::app);
	if(!coutfile)
	{
		cerr<<"Open f2.dat2 error!"<<endl;
	    exit(1);
	}
	coutfile<<m-1<<'\t'<<endl;
	coutfile.close();
}
void lending::printbook()
{
	int i,p;
    ifstream in("f1.dat1");
    in>>p;
	p=p+p;
	in.close();
	ifstream infile("f1.dat",ios::in);
	char line[50];
	cout<<"书名"<<'\t'<<"书号"<<'\t'<<"作者"<<endl;
	for(i=1;i<=p;i++)
	{
	    infile.getline(line,50,'\n');
	    cout<<line<<endl;
	}
	infile.close();
}
void lending::printreader()
{
	int n,p;
	ifstream in("f2.dat2");
    in>>p;
	p=p+p;
	in.close();
	ifstream infile("f2.dat",ios::in);
	char line[100];
	cout<<"借书证号"<<"  "<<"读者"<<endl;
	for(n=1;n<=p;n++)
	{
		infile.getline(line,100,'\n');
		cout<<line<<endl;
	}
	infile.close();
}
void lending::printborrow()
{
	char line[100];
	ifstream in("f3.dat");
	cout<<"书名"<<'\t'<<"书号"<<'\t'<<"作者"<<endl;
	in.getline(line,100,'\n');
	cout<<line<<endl;
}
void lending::search()
{
a:
ifstream fin; 
int flag=0,b,length; 
char str[20]; 
char f[200];char g[200]; 
fin.open("f1.dat"); 
if(!fin)
{ 
    cout<<"Open f1.dat error....."<<endl; 
    exit(1); 
}
while(1)
{
    cout<<"请输入书名:"; 
    cin>>str; 
    strcat(str,"\t"); 
    length=strlen(str); 
    cout<<"------------------------------------------------"<<endl; 
    while(fin.getline(f,199))
	{ 
    strcpy(g,f); 
    if(strncmp(str,f,length)==0)
	{ 
    flag=1; 
    break; 
	} 
	}
    if(flag==1)
	{
        cout<<"所查询的书已经找到:"<<'\n'; 
        cout<<"---------------------------------------------"<<endl; 
        cout<<"书名"<<'\t'<<"书号"<<'\t'<<"作者"<<'\t'<<endl;
        cout<<g<<endl; 
        cout<<"---------------------------------------------"<<endl; 
        flag=0; 
        cout<<"要继续查询按1,否则按0:"<<endl; 
        cin>>b; 
        if(b==0)break;
	}
    else 
	{
        fin.close(); 
        cout<<"没有此书,继续按1,否则按0..."<<endl; 
        cin>>b; 
        if(b==0)break; 
        goto a;
	}
} 
fin.close(); 
cout<<"查询完毕,按任意键返回......"<<endl; 
} 

void lending::Delete() 
{ 
char str[10][80],buf[20]; 
a: 
int i=0,flag=100,c=0; 
ifstream fin("f1.dat"); 
if(!fin)
{
    cout<<"Open f1.dat error....."<<endl; 
    exit(-1); 
} 
cout<<"请输入你要删除的书名:"; 
cin>>buf; 
strcat(buf,"\t");    
while(fin.getline(str[i],80))
{
    if(strncmp(str[i],buf,strlen(buf))==0)
	{ 
    flag=i; 
	} 
i++; 
} 
fin.close(); 
if(flag==100)
{
    cout<<"没有你所要删除的书!重新按1,否则0....."<<endl; 
    int b; 
    cin>>b; 
    if(b==1)
		goto a;
     else c=1; 
} 
if(c==0)
{
    ofstream fout("f1.dat"); 
    for(int j=0;j<i;j++){ 
    if(j==flag)continue; 
    fout<<str[j]<<endl; 
} 
fout.close(); 
cout<<"已找到要删除的书并已删除,继续按1,否则0....."<<endl; 
int b; 
cin>>b; 
if(b==1)goto a; 
} 
cout<<"操作已经结束,按任意键返回......"<<endl;  
}


void lending::borrow()
{
a:
ifstream fin; 
int flag=0,b,length; 
char str[20]; 
char f[200];char g[200]; 
fin.open("f1.dat"); 
if(!fin)
{ 
    cout<<"Open f1.dat error....."<<endl; 
    exit(1); 
}
while(1)
{
    cout<<"请输入你要借的书名:"; 
    cin>>str; 
    strcat(str,"\t"); 
    length=strlen(str); 
    cout<<"------------------------------------------------"<<endl; 
    while(fin.getline(f,199))
	{ 
    strcpy(g,f); 
    if(strncmp(str,f,length)==0)
	{ 
    flag=1; 
    break; 
	} 
	}
    if(flag==1)
	{
        cout<<"借书成功,你借的书已经输入你的文件:"<<'\n'; 
        cout<<"---------------------------------------------"<<endl; 
        cout<<"书名"<<'\t'<<"书号"<<'\t'<<"作者"<<'\t'<<endl;
        cout<<g<<endl; 
        cout<<"---------------------------------------------"<<endl;
		ofstream borrow("f3.dat",ios::app);
		borrow<<g;
		borrow.close();
        flag=0; 
        cout<<"要继续借按1,否则按0:"<<endl; 
        cin>>b; 
        if(b==0)break;
	}
    else 
	{
        fin.close(); 
        cout<<"没有此书,继续按1,否则按0..."<<endl; 
        cin>>b; 
        if(b==0)break; 
        goto a;
	}
} 
fin.close(); 
cout<<"借书完毕,按任意键返回......"<<endl; 
} 

void lending::exchange()
{ 
char str[10][80],buf[20]; 
a: 
int i=0,flag=100,c=0; 
ifstream fin("f1.dat"); 
if(!fin)
{
    cout<<"Open f1.dat error....."<<endl; 
    exit(-1); 
} 
cout<<"请输入你修改的书名:"; 
cin>>buf; 
strcat(buf,"\t");    
while(fin.getline(str[i],80))
{
    if(strncmp(str[i],buf,strlen(buf))==0)
	{ 
    flag=i; 
	} 
i++; 
} 
fin.close(); 
if(flag==100)
{
    cout<<"没有此书!重新按1,否则0....."<<endl; 
    int b; 
    cin>>b; 
    if(b==1)
		goto a;
     else c=1; 
} 
if(c==0)
{
    ofstream fout("f1.dat"); 
    for(int j=0;j<i;j++)
	{	
    if(j==flag)continue; 
    fout<<str[j]<<endl; 
	} 
fout.close();
ofstream yes("f1.dat",ios::app);
cout<<"请输入新的图书信息:"<<endl;
cout<<"书名:";cin>>title;cout<<endl;yes<<title<<'\t';
cout<<"书号:";cin>>booknumber;cout<<endl;yes<<booknumber<<'\t';
cout<<"作者:";cin>>writer;cout<<endl;yes<<writer<<'\t'<<endl;
cout<<"书已经修改,继续按1,否则0....."<<endl; 
int b; 
cin>>b; 
if(b==1)goto a; 
} 
cout<<"操作已经结束,按任意键返回......"<<endl;  
}

void lending::place()
{
	cout<<"欢迎进入图书借阅系统:"<<endl;
    cout<<setw(40)<<"1.录入图书信息(添加)"<<endl;
    cout<<setw(40)<<"2.录入读者信息(添加)"<<endl;
    cout<<setw(34)<<"3.图书打印情况"<<endl;
    cout<<setw(34)<<"4.读者打印信息"<<endl;
    cout<<setw(30)<<"5.图书查询"<<endl;
	cout<<setw(30)<<"6.图书删除"<<endl;
	cout<<setw(30)<<"7.图书修改"<<endl;
	cout<<setw(34)<<"8.读者借书情况"<<endl;
	cout<<setw(38)<<"9.读者借书打印信息"<<endl;
	cout<<"执行操作,请输入阿拉伯数字(1~9)"<<endl;
}


void main()
{
	int ch;
menu: 	
	lending a;
    a.place();
	cin>>ch;
	while(ch!=0)
	{
	switch(ch)
	{
	   case 1:a.inputbook();goto menu;break;
	   case 2:a.inputreader();goto menu;break;
	   case 3:a.printbook();goto menu;break;
	   case 4:a.printreader();goto menu;break;
	   case 5:a.search();goto menu;break;
	   case 6:a.Delete();goto menu;break;
	   case 7:a.exchange();goto menu;break;
	   case 8:a.borrow();goto menu;break;
	   case 9:a.printborrow();goto menu;break;
	}
	}

}

⌨️ 快捷键说明

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