图书管理系统c++语言
时间: 2023-09-01 20:13:07 浏览: 108
图书管理系统 c++
图书管理系统是一个常见的小型应用程序,它可以帮助图书馆或者图书店方便地管理自己的图书信息。下面是一个简单的图书管理系统的 C++ 代码示例,仅供参考:
```c++
#include <iostream>
#include <string>
using namespace std;
class Book {
public:
string name;
string author;
string publisher;
double price;
int quantity;
Book(string n, string a, string p, double pr, int q) {
name = n;
author = a;
publisher = p;
price = pr;
quantity = q;
}
void display() {
cout << "\nName: " << name << endl;
cout << "Author: " << author << endl;
cout << "Publisher: " << publisher << endl;
cout << "Price: " << price << endl;
cout << "Quantity: " << quantity << endl;
}
};
int main() {
Book books[100];
int count = 0;
while (true) {
int choice;
cout << "\n1. Add Book\n2. Display Books\n3. Exit\nEnter your choice: ";
cin >> choice;
if (choice == 1) {
string name, author, publisher;
double price;
int quantity;
cout << "\nEnter Book Details: " << endl;
cout << "Name: ";
cin >> name;
cout << "Author: ";
cin >> author;
cout << "Publisher: ";
cin >> publisher;
cout << "Price: ";
cin >> price;
cout << "Quantity: ";
cin >> quantity;
Book book(name, author, publisher, price, quantity);
books[count] = book;
count++;
cout << "\nBook Added Successfully!" << endl;
}
else if (choice == 2) {
for (int i = 0; i < count; i++) {
books[i].display();
}
}
else if (choice == 3) {
break;
}
else {
cout << "\nInvalid Choice!" << endl;
}
}
return 0;
}
```
这个代码示例中,我们定义了一个 `Book` 类来表示图书信息,包括书名、作者、出版社、价格和数量。然后我们使用一个 `while` 循环来不断接收用户的输入,通过 `switch` 语句来处理用户的选择。如果用户选择添加图书,则需要输入图书的信息,并将其保存到 `books` 数组中;如果用户选择显示图书,则遍历 `books` 数组并调用 `display()` 方法来打印每本书的信息;如果用户选择退出,则退出程序。
阅读全文