代码
//界面
void menu()
{
cout<<"*****欢迎来到图书馆管理系统*****"<<endl;
cout<<"********************************"<<endl;
cout<<"**********1.加入图书************"<<endl;
cout<<"**********2.删除图书************"<<endl;
cout<<"**********3.查找图书************"<<endl;
cout<<"**********4.清空书架************"<<endl;
cout<<"**********5.修改图书************"<<endl;
cout<<"**********6.显示图书信息********"<<endl;
cout<<"**********0.退出系统************"<<endl;
}
//逻辑判断
while(true){
M_menu();
cout<<"请选择功能(0-6):";
cin>>x;
switch(x){
case 1://增
shelf.AddBooks();
break;
case 2://删
shelf.DeleteBooks();
break;
case 3://查找
shelf.CheckBook();
break;
case 4://清空
shelf.Cleanbooks();
break;
case 5://修改
shelf.Changebooks();
break;
case 6://显示全部书籍
shelf.Showbooks();
break;
case 0:
cout << "欢迎下次使用" << endl;
return 0;
default:
cout<<"输入错误,请重新输入"<<endl;
system("pause");
system("cls");
}
}
效果
用STL库中的vector存放书籍。
每次添加后按书名排序
void Shelf::AddBooks()
{
if(book.size()==book.max_size()) cout<<"书架已满"<<endl;
else{
string name,number,writer;
char i;
float price;
cout<<"请输入书名、作者、价格、编号(用空格隔开):"<<endl;
cin>>name>>writer>>price>>number;
cin.get();
i=cin.gcount();
Book n_book;
n_book.name=name;
n_book.writer=writer;
n_book.price=price;
n_book.number=number;
book.push_back(n_book);
sort(book.begin(),book.end(),cmp);
cout<<"添加成功"<<endl;
}
system("pause");
system("cls");
}
根据书名进行删除
由于vector直接删除数据较麻烦,所以可以将要删除的数据和最后一个交换,然后pop
void Shelf::DeleteBooks()
{
cout<<"请输入要删除的书籍名称:";
string name;
cin>>name;
int t=(isExist(book,name));
if(t!=-1)
{
swap(book[t],book[book.size()-1]);
book.pop_back();
sort(book.begin(),book.end(),cmp);
cout<<"删除成功"<<endl;
}
else cout << "无记录" << endl;
system("pause");
system("cls");
}
页面
可以按书名、作者、编号、价格区间进行查询
//界面
void Check_menu()
{
cout<<"********************************"<<endl;
cout<<"*********欢迎来到查询页面*******"<<endl;
cout<<"********************************"<<endl;
cout<<"**********1.按书名查询**********"<<endl;
cout<<"**********2.按作者查询**********"<<endl;
cout<<"**********3.按编号查询**********"<<endl;
cout<<"**********4.按价格区间查询******"<<endl;
cout<<"**********0.退出查询页面********"<<endl;
cout<<"********************************"<<endl;
}
//逻辑判断
void Shelf::CheckBook()
{
system("cls");
while(true){
Check_menu();
cout<<"请选择功能(0-4):";
int n;
cin>>n;
string a;
switch(n){
case 0:
system("cls");
return;
case 1:
cout<<"请输入要查找的书名:";
cin>>a;
isExist(book,a,1);
break;
case 2:
cout<<"请输入要查找的作者:";
cin>>a;
isExist(book,a,2);
break;
case 3:
cout<<"请输入要查找的编号:";
cin>>a;
isExist(book,a,3);
break;
case 4:
cout<<"请输入要查找的价格区间(低到高):";
float l_price,h_price;
cin>>l_price>>h_price;
isExistRange(book,l_price,h_price);
break;
default:
cout<<"输入错误,请重新输入"<<endl;
}
system("pause");
system("cls");
}
}
根据书名修改
void Shelf::Changebooks()
{
cout<<"输入要修改的书名:";
string name;
cin>>name;
int t=isExist(book, name);
if(t!=-1){
cout<<"请输入新名字:";
string name;
cin>>name;
book[t].name = name;
cout<<"请输入新价格:";
int price;
cin>>price;
book[t].price = price;
cout << "请输入新编号:";
string num;
cin >> num;
book[t].number = num;
}
else cout<<"查无此书"<<endl;
system("pause");
system("cls");
}
void Shelf::Showbooks()
{
if(book.size()==0) cout<<"书架为空"<<endl;
else {
for (int i=0;i<book.size();i++) {
cout<<"书籍名称:"<<book[i].name<<"\t";
cout<<"作者:"<<book[i].writer<<"\t";
cout<<"价格:"<<book[i].price<<"\t";
cout<<"编号:"<<book[i].number<<endl;
}
}
system("pause");
system("cls");
}
void isExist(vector<Book> &book,string a,int type)
{
int flag=0;
for(int i=0;i<book.size();i++)
if(type==1&&book[i].name==a||type==2&&book[i].writer==a||type==3&&book[i].number==a) {
flag=1;
cout<<"书名为:"<<book[i].name<<"\t";
cout<<"作者是:"<<book[i].writer<<"\t";
cout<<"书价格为:"<<book[i].price<<"\t";
cout<<"书编号为:"<<book[i].number<<endl;
}
if(flag==0)
cout<<"无记录"<<endl;
}
void isExistRange(vector<Book> &book,float l_price,float h_price)
{
int flag=0;
for(int i=0;i<book.size();i++)
if(book[i].price>=l_price&&book[i].price<=h_price){
flag=1;
cout<<"书名为:"<<book[i].name<<"\t";
cout<<"作者是:"<<book[i].writer<<"\t";
cout<<"书价格为:"<<book[i].price<<"\t";
cout<<"书编号为:"<<book[i].number<<endl;
}
if(flag==0)
cout<<"无记录"<<endl;
}
#include
#include
#include
#include
using namespace std;
struct Book{
string name;
float price;
string number;
string writer;
};
class Shelf{
public:
void AddBooks();
void DeleteBooks();
void CheckBook();
void Cleanbooks();
void Changebooks();
void Showbooks();
private:
vector<Book> book;
};
void M_menu()
{
cout<<"********************************"<<endl;
cout<<"*****欢迎来到图书馆管理系统*****"<<endl;
cout<<"********************************"<<endl;
cout<<"**********1.加入图书************"<<endl;
cout<<"**********2.删除图书************"<<endl;
cout<<"**********3.查找图书************"<<endl;
cout<<"**********4.清空书架************"<<endl;
cout<<"**********5.修改图书************"<<endl;
cout<<"**********6.显示图书信息********"<<endl;
cout<<"**********0.退出系统************"<<endl;
cout<<"********************************"<<endl;
}
void Check_menu()
{
cout<<"********************************"<<endl;
cout<<"*********欢迎来到查询页面*******"<<endl;
cout<<"********************************"<<endl;
cout<<"**********1.按书名查询**********"<<endl;
cout<<"**********2.按作者查询**********"<<endl;
cout<<"**********3.按编号查询**********"<<endl;
cout<<"**********4.按价格区间查询******"<<endl;
cout<<"**********0.退出查询页面********"<<endl;
cout<<"********************************"<<endl;
}
int main()
{
int x;
Shelf shelf;
while(true){
M_menu();
cout<<"请选择功能(0-6):";
cin>>x;
switch(x){
case 1://增
shelf.AddBooks();
break;
case 2://删
shelf.DeleteBooks();
break;
case 3://查找
shelf.CheckBook();
break;
case 4://清空
shelf.Cleanbooks();
break;
case 5://修改
shelf.Changebooks();
break;
case 6://显示全部书籍
shelf.Showbooks();
break;
case 0:
cout << "欢迎下次使用" << endl;
return 0;
default:
cout<<"输入错误,请重新输入"<<endl;
system("pause");
system("cls");
}
}
return 0;
}
bool cmp(Book book1,Book book2)
{
return book1.name<book2.name;
}
void isExist(vector<Book> &book,string a,int type)
{
int flag=0;
for(int i=0;i<book.size();i++)
if(type==1&&book[i].name==a||type==2&&book[i].writer==a||type==3&&book[i].number==a) {
flag=1;
cout<<"书名为:"<<book[i].name<<"\t";
cout<<"作者是:"<<book[i].writer<<"\t";
cout<<"书价格为:"<<book[i].price<<"\t";
cout<<"书编号为:"<<book[i].number<<endl;
}
if(flag==0)
cout<<"无记录"<<endl;
}
void isExistRange(vector<Book> &book,float l_price,float h_price)
{
int flag=0;
for(int i=0;i<book.size();i++)
if(book[i].price>=l_price&&book[i].price<=h_price){
flag=1;
cout<<"书名为:"<<book[i].name<<"\t";
cout<<"作者是:"<<book[i].writer<<"\t";
cout<<"书价格为:"<<book[i].price<<"\t";
cout<<"书编号为:"<<book[i].number<<endl;
}
if(flag==0)
cout<<"无记录"<<endl;
}
void Shelf::AddBooks()
{
if(book.size()==book.max_size()) cout<<"书架已满"<<endl;
else{
string name,number,writer;
char i;
float price;
cout<<"请输入书名、作者、价格、编号(用空格隔开):"<<endl;
cin>>name>>writer>>price>>number;
cin.get();
i=cin.gcount();
Book n_book;
n_book.name=name;
n_book.writer=writer;
n_book.price=price;
n_book.number=number;
book.push_back(n_book);
sort(book.begin(),book.end(),cmp);
cout<<"添加成功"<<endl;
}
system("pause");
system("cls");
}
int isExist(vector<Book> &book,string name)
{
for(int i=0;i<book.size();i++)
if(book[i].name==name) return i;
return -1;
}
void Shelf::DeleteBooks()
{
cout<<"请输入要删除的书籍名称:";
string name;
cin>>name;
int t=(isExist(book,name));
if(t!=-1)
{
swap(book[t],book[book.size()-1]);
book.pop_back();
sort(book.begin(),book.end(),cmp);
cout<<"删除成功"<<endl;
}
else cout << "无记录" << endl;
system("pause");
system("cls");
}
void Shelf::CheckBook()
{
system("cls");
while(true){
Check_menu();
cout<<"请选择功能(0-4):";
int n;
cin>>n;
string a;
switch(n){
case 0:
system("cls");
return;
case 1:
cout<<"请输入要查找的书名:";
cin>>a;
isExist(book,a,1);
break;
case 2:
cout<<"请输入要查找的作者:";
cin>>a;
isExist(book,a,2);
break;
case 3:
cout<<"请输入要查找的编号:";
cin>>a;
isExist(book,a,3);
break;
case 4:
cout<<"请输入要查找的价格区间(低到高):";
float l_price,h_price;
cin>>l_price>>h_price;
isExistRange(book,l_price,h_price);
break;
default:
cout<<"输入错误,请重新输入"<<endl;
}
system("pause");
system("cls");
}
}
void Shelf::Cleanbooks()
{
book.clear();
cout<<"清理完成"<<endl;
system("pause");
system("cls");
}
void Shelf::Changebooks()
{
cout<<"输入要修改的书名:";
string name;
cin>>name;
int t=isExist(book, name);
if(t!=-1){
cout<<"请输入新名字:";
string name;
cin>>name;
book[t].name = name;
cout<<"请输入新价格:";
int price;
cin>>price;
book[t].price = price;
cout << "请输入新编号:";
string num;
cin >> num;
book[t].number = num;
}
else cout<<"查无此书"<<endl;
system("pause");
system("cls");
}
void Shelf::Showbooks()
{
if(book.size()==0) cout<<"书架为空"<<endl;
else {
for (int i=0;i<book.size();i++) {
cout<<"书籍名称:"<<book[i].name<<"\t";
cout<<"作者:"<<book[i].writer<<"\t";
cout<<"价格:"<<book[i].price<<"\t";
cout<<"编号:"<<book[i].number<<endl;
}
}
system("pause");
system("cls");
}
1.之前输入的记录一直保持着很不美观—system(“cls”),作用:清空控制台
2.当输入错误时,会先输出“输入错误,请重新输入“,然后用解决方法1中的函数,清空控制台。但是cls清空速度太快了,在为输出前就清空了—system(“pause”)