c++课程设计图书管理系统
时间: 2023-12-28 18:05:58 浏览: 92
以下是一个简单的C++图书管理系统的示例:
```cpp
#include <iostream>
#include <string>
using namespace std;
struct Book {
string name;
double price;
int number;
};
struct ArrayBooks {
Book* book;
int length;
};
// 显示图书信息
void showBooks(ArrayBooks* abs) {
if (abs->length == 0) {
cout << "书架为空" << endl;
} else {
for (int i = 0; i < abs->length; ++i) {
cout << "书籍名字 : " << abs->book[i].name << "\t";
cout << "书籍价格 : " << abs->book[i].price << "\t";
cout << "书籍编号 : " << abs->book[i].number << endl;
}
}
}
// 判断书籍是否存在
int isExist(ArrayBooks* abs, string name) {
for (int i = 0; i < abs->length; ++i) {
if (abs->book[i].name == name) {
return i;
}
}
return -1;
}
// 查找书籍
void checkBook(ArrayBooks* abs) {
cout << "输入你要查找的书籍" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1) {
cout << "书名为:" << abs->book[ret].name << "\t";
cout << "书价格为:" << abs->book[ret].price << "\t";
cout << "书编号为:" << abs->book[ret].number << endl;
} else {
cout << "查无此书" << endl;
}
}
int main() {
ArrayBooks abs;
abs.length = 0;
abs.book = new Book[100];
// 添加书籍
abs.book[abs.length].name = "书籍1";
abs.book[abs.length].price = 10.99;
abs.book[abs.length].number = 1;
abs.length++;
abs.book[abs.length].name = "书籍2";
abs.book[abs.length].price = 19.99;
abs.book[abs.length].number = 2;
abs.length++;
// 显示图书信息
showBooks(&abs);
// 查找书籍
checkBook(&abs);
delete[] abs.book;
return 0;
}
```
阅读全文