使用c++模拟用户可以自己选购商品的超市购物系统
时间: 2024-01-08 15:02:23 浏览: 30
好的,我可以为你提供一些指导。首先,你需要设计一个商品类,其中包括商品的名称、价格、数量等属性。然后,你需要设计一个购物车类,其中包括添加商品、删除商品、计算总价等方法。接下来,你需要设计一个超市类,其中包括商品列表、促销活动、付款等方法。最后,你可以在主函数中编写用户界面,让用户可以通过输入选择商品、添加到购物车、结账等操作。希望这些指导可以帮助你开始编写超市购物系统的模拟程序。
相关问题
用c++写一个可以模拟用户自主选择商品的超市购物系统购物
好的,我可以为您提供一个基本的超市购物系统的C++代码。以下是代码:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;
// 商品信息
struct Item {
string name;
double price;
};
// 购物车
class Cart {
private:
vector<Item> items; // 商品列表
double total; // 购物车总价
public:
Cart() {
total = 0;
}
// 添加商品到购物车
void addItem(Item item) {
items.push_back(item);
total += item.price;
}
// 显示购物车列表
void showItems() {
cout << "购物车列表:" << endl;
cout << setw(10) << left << "商品名称"
<< setw(10) << left << "价格" << endl;
for (Item item : items) {
cout << setw(10) << left << item.name
<< setw(10) << left << item.price << endl;
}
cout << "总计:" << total << endl;
}
};
// 商品列表
vector<Item> items = {
{"牛奶", 5.0},
{"面包", 3.0},
{"苹果", 2.5},
{"香蕉", 1.5},
{"饼干", 2.0}
};
int main() {
Cart cart; // 创建购物车
// 显示商品列表
cout << "商品列表:" << endl;
cout << setw(10) << left << "商品编号"
<< setw(10) << left << "商品名称"
<< setw(10) << left << "价格" << endl;
for (int i = 0; i < items.size(); i++) {
Item item = items[i];
cout << setw(10) << left << i + 1
<< setw(10) << left << item.name
<< setw(10) << left << item.price << endl;
}
// 用户选择商品
while (true) {
cout << "请输入要购买的商品编号(输入0结束):" << endl;
int index;
cin >> index;
if (index == 0) {
break;
}
if (index < 1 || index > items.size()) {
cout << "无效的编号,请重新输入!" << endl;
continue;
}
Item item = items[index - 1];
cart.addItem(item);
cout << item.name << "已加入购物车!" << endl;
}
// 显示购物车列表
cart.showItems();
return 0;
}
```
该程序实现了一个简单的超市购物系统。用户可以选择购买不同的商品,并将其添加到购物车中。程序最终会显示购物车列表,包括购买商品的名称、价格和总价。
使用c++模拟一个较为完整的超市购物系统
好的,以下是一个简单的超市购物系统的 C++ 实现:
```cpp
#include <iostream>
#include <vector>
#include <map>
#include <iomanip>
using namespace std;
// 商品类
class Product {
public:
string name; // 商品名称
double price; // 商品单价
Product(string n, double p) : name(n), price(p) {}
};
// 购物车类
class ShoppingCart {
public:
vector<Product> products; // 商品列表
void addProduct(Product p) {
products.push_back(p);
}
double totalPrice() {
double sum = 0;
for (Product p : products) {
sum += p.price;
}
return sum;
}
};
// 超市类
class SuperMarket {
public:
map<string, Product> products; // 商品列表
void addProduct(string name, double price) {
products[name] = Product(name, price);
}
void displayProducts() {
cout << "商品列表:" << endl;
cout << left << setw(20) << "名称" << setw(10) << "单价" << endl;
for (auto item : products) {
cout << left << setw(20) << item.second.name << setw(10) << item.second.price << endl;
}
}
ShoppingCart createShoppingCart() {
return ShoppingCart();
}
};
// 主函数
int main() {
SuperMarket market;
market.addProduct("苹果", 5.0);
market.addProduct("香蕉", 3.0);
market.addProduct("橙子", 4.0);
market.displayProducts();
ShoppingCart cart = market.createShoppingCart();
while (true) {
cout << "请输入要添加到购物车的商品名称(输入 q 结束):";
string name;
cin >> name;
if (name == "q") {
break;
}
if (market.products.count(name) == 0) {
cout << "找不到该商品,请重新输入!" << endl;
} else {
cart.addProduct(market.products[name]);
}
}
double totalPrice = cart.totalPrice();
cout << "您的购物车:" << endl;
cout << left << setw(20) << "名称" << setw(10) << "单价" << endl;
for (Product p : cart.products) {
cout << left << setw(20) << p.name << setw(10) << p.price << endl;
}
cout << "总价:" << totalPrice << endl;
return 0;
}
```
这个程序实现了一个简单的超市购物系统,包括商品类、购物车类、超市类。可以通过 `SuperMarket` 类添加商品,通过 `ShoppingCart` 类创建购物车,往购物车里添加商品,计算购物车的总价。