提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- 系统要求
- 功能
- 1.首页
- 2.退出系统
- 3.添加图书
- 4.删除书籍
- 5.查找书籍
- 6.修改书籍信息
- 7.显示所有图书
- 8.查看书籍是否在书架上
- 总代码
- 收获
系统要求
用c++实现一个可以增删改查的图书管理系统
1.每个书架shelf可以存放一类书(将shelf声明为类),用vector存放(可以存放更多的书)。有一个length来表述书的长度。类中声明许多成员函数,用于实现相关功能。
2.每本书是一个结构体,体内包含书名、价格、编号。
功能
1.首页
功能描述:用户选择功能的界面
封装在函数menu中
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;
}
效果
2.退出系统
case 0:
cout << "欢迎下次使用" << endl;
return 0;
3.添加图书
用STL库中的vector存放。
每次添加后按书名排序
void Shelf::DeleteBooks()
{
cout<<"请输入要删除的书籍名称"<<endl;
string name;
cin>>name;
int t=(isExist(book,name));
if(t!=-1)
{
swap(book[t],book[--length]);
book.pop_back();
sort(book.begin(),book.end(),cmp);
cout<<"删除成功"<<endl;
}
else cout << "查无此书" << endl;
system("pause");
system("cls");
}
4.删除书籍
根据书名进行删除
由于vector直接删除数据较麻烦,所以可以将要删除的数据和最后一个交换,然后pop
void Shelf::DeleteBooks()
{
cout<<"请输入要删除的书籍名称"<<endl;
string name;
cin>>name;
int t=(isExist(book,name));
if(t!=-1)
{
swap(book[t],book[--length]);
book.pop_back();
sort(book.begin(),book.end(),cmp);
cout<<"删除成功"<<endl;
}
else cout << "查无此书" << endl;
system("pause");
system("cls");
}
5.查找书籍
通过书名进行查找
void Shelf::CheckBook()
{
cout<<"请输入要查找的书名"<<endl;
string name;
cin>>name;
int t=isExist(book, name);
if(t!=-1){
cout<<"书名为:"<<book[t].name<<"\t";
cout<<"书价格为:"<<book[t].price<<"\t";
cout<<"书编号为:"<<book[t].number<<endl;
}
else
cout<<"查无此书"<<endl;
system("pause");
system("cls");
}
6.修改书籍信息
根据书名修改
void Shelf::Changebooks()
{
cout<<"输入要修改的书名"<<endl;
string name;
cin>>name;
int t=isExist(book, name);
if(t!=-1){
cout<<"请输入新名字:"<<endl;
string name;
cin>>name;
book[t].name = name;
cout<<"请输入新价格:"<<endl;
int price;
cin>>price;
book[t].price = price;
cout << "请输入新编号: " << endl;
string num;
cin >> num;
book[t].number = num;
}
else cout<<"查无此书"<<endl;
system("pause");
system("cls");
}
7.显示所有图书
void Shelf::Showbooks()
{
if(length==0) cout<<"书架为空"<<endl;
else {
for (int i=0;i<length;i++) {
cout<<"书籍名称:"<<book[i].name<<"\t";
cout<<"价格:"<<book[i].price<<"\t";
cout<<"编号:"<<book[i].number<<endl;
}
}
system("pause");
system("cls");
}
8.查看书籍是否在书架上
在就返回下标。不在就返回-1
int isExist(vector<Book> &book,string name)
{
for(int i=0;i<book.size();i++)
if(book[i].name==name) return i;
return -1;
}
总代码
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
struct Book{
string name;
float price;
string number;
};
class Shelf{
public:
Shelf(){length=0;};
void AddBooks();
void DeleteBooks();
void CheckBook();
void Cleanbooks();
void Changebooks();
void Showbooks();
private:
vector<Book> book;
int length;
};
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;
}
bool cmp(Book book1,Book book2)
{
return book1.name<book2.name;
}
void Shelf::AddBooks()
{
if(length==book.max_size()) cout<<"书架已满"<<endl;
else{
string name,number;
float price;
cout<<"请输入书名、价格、编号(用空格隔开)"<<endl;
cin>>name>>price>>number;
Book n_book;
n_book.name=name;
n_book.price=price;
n_book.number=number;
book.push_back(n_book);
length++;
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<<"请输入要删除的书籍名称"<<endl;
string name;
cin>>name;
int t=(isExist(book,name));
if(t!=-1)
{
swap(book[t],book[--length]);
book.pop_back();
sort(book.begin(),book.end(),cmp);
cout<<"删除成功"<<endl;
}
else cout << "查无此书" << endl;
system("pause");
system("cls");
}
void Shelf::CheckBook()
{
cout<<"请输入要查找的书名"<<endl;
string name;
cin>>name;
int t=isExist(book, name);
if(t!=-1){
cout<<"书名为:"<<book[t].name<<"\t";
cout<<"书价格为:"<<book[t].price<<"\t";
cout<<"书编号为:"<<book[t].number<<endl;
}
else
cout<<"查无此书"<<endl;
system("pause");
system("cls");
}
void Shelf::Cleanbooks()
{
length=0;
book.clear();
cout<<"清理完成"<<endl;
system("pause");
system("cls");
}
void Shelf::Changebooks()
{
cout<<"输入要修改的书名"<<endl;
string name;
cin>>name;
int t=isExist(book, name);
if(t!=-1){
cout<<"请输入新名字:"<<endl;
string name;
cin>>name;
book[t].name = name;
cout<<"请输入新价格:"<<endl;
int price;
cin>>price;
book[t].price = price;
cout << "请输入新编号: " << endl;
string num;
cin >> num;
book[t].number = num;
}
else cout<<"查无此书"<<endl;
system("pause");
system("cls");
}
void Shelf::Showbooks()
{
if(length==0) cout<<"书架为空"<<endl;
else {
for (int i=0;i<length;i++) {
cout<<"书籍名称:"<<book[i].name<<"\t";
cout<<"价格:"<<book[i].price<<"\t";
cout<<"编号:"<<book[i].number<<endl;
}
}
system("pause");
system("cls");
}
int main()
{
int x;
Shelf shelf;
while(true){
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;
}
收获
1.之前输入的记录一直保持着很不美观—system(“cls”),作用:清空控制台
2.当输入错误时,会先输出“输入错误,请重新输入“,然后用解决方法1中的函数,清空控制台。但是cls清空速度太快了,在为输出前就清空了—system(“pause”)