用c++语言编译图书管理系统项目
时间: 2023-11-30 19:26:04 浏览: 68
好的,下面是一个简单的图书管理系统项目的C++代码示例,供参考:
```cpp
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;
// 定义图书结构体
struct Book {
int id; // 书籍编号
char name[50]; // 书名
char author[50]; // 作者
int year; // 出版年份
int quantity; // 数量
};
// 全局变量
Book books[100];
int book_num = 0;
// 添加图书
void add_book() {
cout << "请输入书籍编号: ";
cin >> books[book_num].id;
cout << "请输入书名: ";
cin >> books[book_num].name;
cout << "请输入作者: ";
cin >> books[book_num].author;
cout << "请输入出版年份: ";
cin >> books[book_num].year;
cout << "请输入数量: ";
cin >> books[book_num].quantity;
book_num++;
cout << "添加成功!" << endl;
}
// 删除图书
void delete_book() {
int id;
cout << "请输入要删除的书籍编号: ";
cin >> id;
int i;
for (i = 0; i < book_num; i++) {
if (books[i].id == id) {
break;
}
}
if (i == book_num) {
cout << "未找到该书籍!" << endl;
} else {
for (; i < book_num - 1; i++) {
books[i] = books[i + 1];
}
book_num--;
cout << "删除成功!" << endl;
}
}
// 修改图书信息
void modify_book() {
int id;
cout << "请输入要修改的书籍编号: ";
cin >> id;
int i;
for (i = 0; i < book_num; i++) {
if (books[i].id == id) {
break;
}
}
if (i == book_num) {
cout << "未找到该书籍!" << endl;
} else {
cout << "请输入修改后的书名: ";
cin >> books[i].name;
cout << "请输入修改后的作者: ";
cin >> books[i].author;
cout << "请输入修改后的出版年份: ";
cin >> books[i].year;
cout << "请输入修改后的数量: ";
cin >> books[i].quantity;
cout << "修改成功!" << endl;
}
}
// 查询图书信息
void query_book() {
int id;
cout << "请输入要查询的书籍编号: ";
cin >> id;
int i;
for (i = 0; i < book_num; i++) {
if (books[i].id == id) {
cout << "书籍编号: " << books[i].id << endl;
cout << "书名: " << books[i].name << endl;
cout << "作者: " << books[i].author << endl;
cout << "出版年份: " << books[i].year << endl;
cout << "数量: " << books[i].quantity << endl;
break;
}
}
if (i == book_num) {
cout << "未找到该书籍!" << endl;
}
}
// 显示所有图书信息
void show_all_books() {
cout << "书籍编号\t书名\t作者\t出版年份\t数量" << endl;
for (int i = 0; i < book_num; i++) {
cout << books[i].id << "\t" << books[i].name << "\t" << books[i].author << "\t" << books[i].year << "\t" << books[i].quantity << endl;
}
}
// 主函数
int main() {
int choice;
while (true) {
cout << "1. 添加图书" << endl;
cout << "2. 删除图书" << endl;
cout << "3. 修改图书信息" << endl;
cout << "4. 查询图书信息" << endl;
cout << "5. 显示所有图书信息" << endl;
cout << "6. 退出" << endl;
cout << "请选择操作:";
cin >> choice;
switch (choice) {
case 1:
add_book();
break;
case 2:
delete_book();
break;
case 3:
modify_book();
break;
case 4:
query_book();
break;
case 5:
show_all_books();
break;
case 6:
exit(0);
default:
cout << "输入有误,请重新输入!" << endl;
break;
}
}
return 0;
}
```
这个代码示例实现了一个简单的图书管理系统,可以实现添加、删除、修改、查询和显示所有图书信息等功能。您可以根据自己的需求进行修改和优化。
阅读全文