用C++语言做一个简单的图书管理系统,实现增删改查以及读取和保存文件功能
求求哪位大佬可以帮我看看我自己写的这个代码,不知道哪里的问题,录入图书信息的功能实现不了,也不能读取文件信息,其它功能无法测试,也不知道有什么问题,知识太少了,弄了好久都没弄好,想求助大佬帮看看,马上就要ddl了,或者用数组或者用类帮忙写一份代码也行啊(别太难否则我看不懂)谢谢啦啊
#include <windows.h>
#include <iostream>
#include <iomanip>
#include <string.h>
#include <fstream>
using namespace std;
#define BOOK 100
//图书信息
struct Book
{int book_no; //图书编号
char name[30]; //书名
char author[20]; // 作者
char press[20]; // 出版社
char date[15]; //出版日期:YYYY-MM-DD
char kind[15]; //图书类别
double price; //图书单价
int total; //藏书数量
};
//图书类
class book
{
private:
Book bo_array[BOOK]; //图书数组
int booknum; //当前图书记录
public:
book();
~book();
void menu();
int get_booknum(); //获取当前图书记录
void input();
void search(); //查找
void update();
void show();
void delete_();
void load_file();
void save_file();
};
book::book()
{
booknum=0;
}
book::~book()
{
}
int book::get_booknum()
{
return booknum;
}
void book::menu()
{
int choice;
system("cls");
cout<<" 图书管理系统"<<endl;
cout<<"--------------------------------------------"<<endl;
cout<<"\t\t1.录入图书信息"<<endl;
cout<<"\t\t2.浏览图书信息"<<endl;
cout<<"\t\t3.查询图书信息"<<endl;
cout<<"\t\t4.修改图书信息"<<endl;
cout<<"\t\t5.删除图书信息"<<endl;
cout<<"\t\t6.退出系统"<<endl;
cout<<"请选择(1-6):";
cin>>choice;
if(choice>=0&&choice<7)
{
switch(choice)
{
case 1:
input();
break;
case 2:
show();
break;
case 3:
search();
break;
case 4:
update();
break;
case 5:
delete_();
break;
case 6:
save_file();
cout<<"感谢您的使用,系统已退出,再见!"<<endl;
system("pause");
exit(0);
}
}
else
{
cout<<"输入有误!请重新输入!"<<endl;
}
}
int main()
{
book book1;
while(1)
{
book1.menu();
}
return 0;
}
void book::input()
{
Book boo;
get_booknum();
while(1)
{
system("cls");
cout<<"下面请输入(添加)图书信息:"<<endl;
cout<<"请输入图书编号、书名、作者、出版社、出版日期(输入0时退出):"<<endl;
cin>>boo.book_no;
if(boo.book_no==0)
return;
cin>>boo.book_no>>boo.name>>boo.author>>boo.press>>boo.date;
cout<<"请输入图书类别、图书价格(单位:元)、藏书数量:"<<endl;
cout<<"(类别:科学技术、语言文学、政治经济、历史地理、意识形态、艺术)"<<endl;
cin>>boo.kind>>boo.price>>boo.total;
bo_array[booknum]=boo;
booknum++;
}
}
void book::load_file()
{
Book boo;
ifstream infile;
get_booknum();
infile.open("bookfile.txt",ios::in);
if(!infile)
{
cout<<"无数据文件bookfile.txt!";
Sleep(3000);
booknum=0;
return;
}
while(!infile.eof())
{
infile>>boo.book_no>>boo.name>>boo.author>>boo.press>>boo.date>>boo.kind>>boo.price>>boo.total;
if(infile)
{
bo_array[booknum]=boo;
booknum++;
}
else
break;
}
infile.close();
return;
}
void book::save_file()
{
ofstream outfile;
get_booknum();
outfile.open("bookfile.txt",ios::out);
if(!outfile)
{
cout<<"无法将数据保存到文件bookfile.txt中!"<<endl;
Sleep(3000);
return;
}
for(int i=0;i<get_booknum();i++)
{
outfile<<bo_array[i].book_no<<" "<<bo_array[i].name<<" "<<bo_array[i].author<<" "<<bo_array[i].press<<" "<<bo_array[i].date<<" ";
outfile<<bo_array[i].kind<<" "<<bo_array[i].price<<" "<<bo_array[i].total<<endl;
}
outfile.close();
cout<<"已经将"<<booknum<<"个数据保存到文件bookfile.txt中"<<endl;
Sleep(1000);
return;
}
void book::search()
{
Book boo;
int bo_no;
bool judge=0;
cout<<"请输入要查询的图书编号:"<<endl;
cin>>bo_no;
cout<<"查询结果:"<<endl;
for(int i=0;i<get_booknum();i++)
{
if(bo_array[i].book_no==bo_no)
{
judge=1;
cout<<"图书编号 书名 作者 出版社 出版日期 图书类别 图书价格 藏书数量 "<<endl;
cout<<"------------------------------------------------------------------------------------------------------"<<endl;
cout<<setw(10)<<bo_array[i].book_no<<setw(15)<<bo_array[i].name<<setw(15)<<bo_array[i].author<<setw(20)<<bo_array[i].press;
cout<<setw(15)<<bo_array[i].date<<setw(10)<<bo_array[i].kind<<setw(8)<<bo_array[i].price<<setw(10)<<bo_array[i].total<<endl;
}
}
if(judge==0)
{
cout<<"查询不到该编号!"<<endl;
}
system("pause");
system("cls");
}
void book::delete_()
{
bool judge=0;
int bo_no;
char sel;
cout<<"请输入要删除的图书编号:"<<endl;
cin>>bo_no;
for(int i=0;i<get_booknum();i++)
{
if(bo_array[i].book_no==bo_no)
{
judge=1;
cout<<"图书编号 书名 作者 出版社 出版日期 图书类别 图书价格 藏书数量 "<<endl;
cout<<"------------------------------------------------------------------------------------------------------"<<endl;
cout<<setw(10)<<bo_array[i].book_no<<setw(15)<<bo_array[i].name<<setw(15)<<bo_array[i].author<<setw(20)<<bo_array[i].press;
cout<<setw(15)<<bo_array[i].date<<setw(10)<<bo_array[i].kind<<setw(8)<<bo_array[i].price<<setw(10)<<bo_array[i].total<<endl;
cout<<"是否确认删除该条记录?(Y/N)"<<endl;
while(1)
{
cin>>sel;
if(sel=='Y'||sel=='y'||sel=='N'||sel=='n')
{
break;
}
else
{
cout<<"输入有误,请重新输入!"<<endl;
}
}
if(sel=='Y'||sel=='y')
{
for(int j=i;j<get_booknum();j++)
{
bo_array[j].book_no=bo_array[j+1].book_no;
for(int k=0;k<30;k++)
{
bo_array[j].name[k]=bo_array[j].name[k+1];
bo_array[j].author[k]=bo_array[j].author[k+1];
}
}
}
if(sel=='N'||sel=='n')
{
menu();
}
}
}
if(judge==1)
{
cout<<"删除成功!"<<endl;
booknum--;
}
if(judge==0)
{
cout<<"查询不到该编号!"<<endl;
}
system("pause");
system("cls");
}
void book::update()
{
bool judge=0;
int bo_no;
char sel;
cout<<"请输入要修改的图书编号:"<<endl;
cin>>bo_no;
for(int i=0;i<get_booknum();i++)
{
if(bo_array[i].book_no==bo_no)
{
judge=1;
cout<<"图书编号 书名 作者 出版社 出版日期 图书类别 图书价格 藏书数量 "<<endl;
cout<<"------------------------------------------------------------------------------------------------------"<<endl;
cout<<setw(10)<<bo_array[i].book_no<<setw(15)<<bo_array[i].name<<setw(15)<<bo_array[i].author<<setw(20)<<bo_array[i].press;
cout<<setw(15)<<bo_array[i].date<<setw(10)<<bo_array[i].kind<<setw(8)<<bo_array[i].price<<setw(10)<<bo_array[i].total<<endl;
cout<<"是否确认修改该图书信息?(Y/N)"<<endl;
while(1)
{
cin>>sel;
if(sel=='Y'||sel=='y'||sel=='N'||sel=='n')
{
break;
}
else
{
cout<<"输入有误,请重新输入!"<<endl;
}
}
if(sel=='Y'||sel=='y')
{
judge=1;
cout<<"图书编号:";
cin>>bo_array[i].book_no;
cout<<"图书书名:";
cin>>bo_array[i].name;
cout<<"图书作者:";
cin>>bo_array[i].author;
cout<<"出版社:";
cin>>bo_array[i].press;
cout<<"出版日期:";
cin>>bo_array[i].date;
cout<<"修改成功!"<<endl;
}
if(sel=='N'||sel=='n')
{
menu();
}
}
}
if(judge==0)
{
cout<<"查询不到该编号!"<<endl;
}
}
void book::show()
{
cout<<"图书编号 书名 作者 出版社 出版日期 图书类别 图书价格 藏书数量 "<<endl;
cout<<"------------------------------------------------------------------------------------------------------"<<endl;
for(int i=0;i<booknum;i++)
{
cout<<setw(10)<<bo_array[i].book_no;
cout<<setw(15)<<bo_array[i].name;
cout<<setw(15)<<bo_array[i].author;
cout<<setw(20)<<bo_array[i].press;
cout<<setw(15)<<bo_array[i].date;
cout<<setw(10)<<bo_array[i].kind;
cout<<setw(8)<<bo_array[i].price;
cout<<setw(10)<<bo_array[i].total;
cout<<endl;
if((i+1)%10==0)
system("pause");
}
}