用c++写一个网上购书控制台系统
时间: 2023-12-31 19:05:39 浏览: 34
好的,我可以为您提供一个简单的参考实现,但由于篇幅限制,无法提供完整的代码。
首先,我们需要定义一些数据结构来存储书籍信息和用户信息。例如:
```c++
struct Book {
string name;
string author;
int price;
int quantity;
};
struct User {
string name;
string password;
vector<Book> cart;
};
```
其中,Book 结构体存储书籍的名称、作者、价格和库存量,User 结构体存储用户的用户名、密码和购物车中的书籍列表。
接下来,我们需要实现一些功能函数,例如:
- 登录功能函数:用户输入用户名和密码,程序根据用户输入检查用户信息是否正确,并返回相应的用户结构体。
- 注册功能函数:用户输入用户名和密码,程序将新用户信息存储到用户信息列表中。
- 浏览书籍功能函数:用户输入关键字,程序根据关键字从书籍信息列表中查询符合条件的书籍,并将结果输出到控制台。
- 加入购物车功能函数:用户输入要购买的书籍名称和数量,程序根据用户输入从书籍信息列表中查询符合条件的书籍,并将其添加到用户的购物车中。
- 查看购物车功能函数:程序输出用户购物车中的书籍列表和总价格。
- 结算功能函数:用户输入支付方式,程序根据用户输入计算总价并输出到控制台。
以上只是一个简单的购书系统的功能,您可以根据实际情况进行扩展和改进。
下面是一个简单的参考实现,供您参考:
```c++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Book {
string name;
string author;
int price;
int quantity;
};
struct User {
string name;
string password;
vector<Book> cart;
};
vector<Book> books;
vector<User> users;
User* login()
{
string name, password;
cout << "请输入用户名:";
cin >> name;
cout << "请输入密码:";
cin >> password;
for (auto& user : users) {
if (user.name == name && user.password == password) {
return &user;
}
}
cout << "用户名或密码错误!" << endl;
return nullptr;
}
void registerUser()
{
string name, password;
cout << "请输入用户名:";
cin >> name;
cout << "请输入密码:";
cin >> password;
for (auto& user : users) {
if (user.name == name) {
cout << "用户名已存在!" << endl;
return;
}
}
users.push_back({ name, password });
cout << "注册成功!" << endl;
}
void addBook()
{
string name, author;
int price, quantity;
cout << "请输入书名:";
cin >> name;
cout << "请输入作者:";
cin >> author;
cout << "请输入价格:";
cin >> price;
cout << "请输入数量:";
cin >> quantity;
for (auto& book : books) {
if (book.name == name && book.author == author) {
book.quantity += quantity;
cout << "添加成功!" << endl;
return;
}
}
books.push_back({ name, author, price, quantity });
cout << "添加成功!" << endl;
}
void browseBooks()
{
string keyword;
cout << "请输入关键字:";
cin >> keyword;
for (auto& book : books) {
if (book.name.find(keyword) != string::npos || book.author.find(keyword) != string::npos) {
cout << book.name << "\t" << book.author << "\t" << book.price << "\t" << book.quantity << endl;
}
}
}
void addToCart(User* user)
{
string name;
int quantity;
cout << "请输入书名:";
cin >> name;
cout << "请输入数量:";
cin >> quantity;
for (auto& book : books) {
if (book.name == name) {
if (book.quantity < quantity) {
cout << "库存不足!" << endl;
return;
}
book.quantity -= quantity;
for (auto& b : user->cart) {
if (b.name == name) {
b.quantity += quantity;
cout << "添加成功!" << endl;
return;
}
}
user->cart.push_back({ name, book.author, book.price, quantity });
cout << "添加成功!" << endl;
return;
}
}
cout << "书籍不存在!" << endl;
}
void viewCart(User* user)
{
int total = 0;
for (auto& book : user->cart) {
cout << book.name << "\t" << book.author << "\t" << book.price << "\t" << book.quantity << endl;
total += book.price * book.quantity;
}
cout << "总价:" << total << endl;
}
void checkout(User* user)
{
int total = 0;
for (auto& book : user->cart) {
total += book.price * book.quantity;
}
cout << "总价:" << total << endl;
cout << "请选择支付方式:" << endl;
cout << "1. 支付宝" << endl;
cout << "2. 微信" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
cout << "使用支付宝支付成功!" << endl;
break;
case 2:
cout << "使用微信支付成功!" << endl;
break;
default:
cout << "无效的选择!" << endl;
break;
}
user->cart.clear();
}
int main()
{
books.push_back({ "C++ Primer", "Lippman", 99, 10 });
books.push_back({ "Effective C++", "Meyers", 59, 5 });
books.push_back({ "The C++ Programming Language", "Stroustrup", 89, 8 });
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 登录" << endl;
cout << "2. 注册" << endl;
cout << "3. 添加书籍" << endl;
cout << "4. 浏览书籍" << endl;
cout << "5. 加入购物车" << endl;
cout << "6. 查看购物车" << endl;
cout << "7. 结算" << endl;
cout << "8. 退出" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1: {
auto user = login();
if (user) {
cout << "登录成功!" << endl;
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 浏览书籍" << endl;
cout << "2. 加入购物车" << endl;
cout << "3. 查看购物车" << endl;
cout << "4. 结算" << endl;
cout << "5. 注销" << endl;
int choice2;
cin >> choice2;
switch (choice2) {
case 1:
browseBooks();
break;
case 2:
addToCart(user);
break;
case 3:
viewCart(user);
break;
case 4:
checkout(user);
break;
case 5:
cout << "注销成功!" << endl;
goto END_LOGIN;
default:
cout << "无效的选择!" << endl;
break;
}
}
}
break;
}
case 2:
registerUser();
break;
case 3:
addBook();
break;
case 4:
browseBooks();
break;
case 5:
cout << "请先登录!" << endl;
break;
case 6:
cout << "请先登录!" << endl;
break;
case 7:
cout << "请先登录!" << endl;
break;
case 8:
return 0;
default:
cout << "无效的选择!" << endl;
break;
}
}
END_LOGIN:
goto END_MAIN;
END_MAIN:
return 0;
}
```
注意:本实现中使用了 `goto` 语句,这是为了方便实现简单的注销功能,实际开发中不建议使用 `goto` 语句。