用c++代码写商家促销某商品一件单价100元,2到5件单价90元,6到10件单价85元,11件及以上单价80元。客户购买n件,需要支付多少钱?
时间: 2023-05-16 19:05:37 浏览: 90
以下是用 C 代码实现商家促销某商品的价格计算:
```c
#include <stdio.h>
int main() {
int n; // 购买的件数
int price; // 单价
int total; // 总价
printf("请输入购买的件数:");
scanf("%d", &n);
if (n == 1) {
price = 100;
} else if (n >= 2 && n <= 5) {
price = 90;
} else if (n >= 6 && n <= 10) {
price = 85;
} else {
price = 80;
}
total = n * price;
printf("您购买了 %d 件商品,需要支付 %d 元。\n", n, total);
return 0;
}
```
如果客户购买 1 件商品,需要支付 100 元;如果购买 2 到 5 件商品,每件商品单价为 90 元,需要支付的总价为 90n 元;如果购买 6 到 10 件商品,每件商品单价为 85 元,需要支付的总价为 85n 元;如果购买 11 件及以上商品,每件商品单价为 80 元,需要支付的总价为 80n 元。
相关问题
用 C++ 来编写超市订单系统,功能包括修改订单号,修改商品名称,修改商品单价,修改商品数量,修改收件人姓名,删除订单
好的,我可以为您提供一个简单的超市订单系统的代码框架,您可以在此基础上进行修改和完善。以下是代码示例:
```cpp
#include <iostream>
#include <vector>
using namespace std;
struct Order {
int id; // 订单号
string name; // 商品名称
double price; // 商品单价
int quantity; // 商品数量
string recipient; // 收件人姓名
};
class Supermarket {
public:
// 添加订单
void addOrder(const Order& order) {
orders_.push_back(order);
}
// 修改订单号
void modifyOrderId(int oldId, int newId) {
for (auto& order : orders_) {
if (order.id == oldId) {
order.id = newId;
break;
}
}
}
// 修改商品名称
void modifyOrderName(int orderId, const string& newName) {
for (auto& order : orders_) {
if (order.id == orderId) {
order.name = newName;
break;
}
}
}
// 修改商品单价
void modifyOrderPrice(int orderId, double newPrice) {
for (auto& order : orders_) {
if (order.id == orderId) {
order.price = newPrice;
break;
}
}
}
// 修改商品数量
void modifyOrderQuantity(int orderId, int newQuantity) {
for (auto& order : orders_) {
if (order.id == orderId) {
order.quantity = newQuantity;
break;
}
}
}
// 修改收件人姓名
void modifyOrderRecipient(int orderId, const string& newRecipient) {
for (auto& order : orders_) {
if (order.id == orderId) {
order.recipient = newRecipient;
break;
}
}
}
// 删除订单
void deleteOrder(int orderId) {
for (auto it = orders_.begin(); it != orders_.end(); ++it) {
if (it->id == orderId) {
orders_.erase(it);
break;
}
}
}
private:
vector<Order> orders_; // 存储订单
};
int main() {
// 初始化订单系统
Supermarket supermarket;
// 添加订单
Order order1{ 1, "商品1", 10.0, 2, "收件人1" };
Order order2{ 2, "商品2", 20.0, 3, "收件人2" };
supermarket.addOrder(order1);
supermarket.addOrder(order2);
// 修改订单
supermarket.modifyOrderId(1, 3);
supermarket.modifyOrderName(2, "商品3");
supermarket.modifyOrderPrice(3, 30.0);
supermarket.modifyOrderQuantity(3, 4);
supermarket.modifyOrderRecipient(2, "收件人3");
// 删除订单
supermarket.deleteOrder(3);
return 0;
}
```
在此代码框架中,我们定义了 `Order` 结构体来存储订单信息,包括订单号、商品名称、商品单价、商品数量和收件人姓名。我们使用 `vector` 容器来存储所有订单,并定义了相应的函数来实现订单的添加、修改和删除等功能。
您可以根据实际需求对代码进行修改和完善,例如添加查找订单、按照不同条件排序等功能。同时,请注意在实际开发中需要考虑数据的安全性和完整性,例如对用户输入进行合法性检查和异常处理等。
编写一个C++程序,要求2-3 商店销售某一商品,商店每天公布统一的折扣(discount)。同时允许销售人员在销售时灵活掌握售价(price),在此基础上,对一次购10件以上者,还可以享受9.8折优惠。现已知当天3名销货员的销售情况为: 销货员号(num) 销货件数(quantity) 销货单价(price) 101 5 23.5 102 12 24.56 103 100 21.5 编程计算出当日此商品的总销售款sum,以及每件商品的平均售价。要求用静态数据成员和静态成员函数。
以下是一个简单的 C++ 程序,实现了上述要求:
```cpp
#include <iostream>
using namespace std;
class Salesman {
public:
Salesman(int num, int quantity, double price) {
this->num = num;
this->quantity = quantity;
this->price = price;
sum += quantity * price * discount();
total_quantity += quantity;
total_price += price * quantity;
count++;
}
static double discount() {
return count >= 10 ? 0.98 : 1.0;
}
static double avg_price() {
return total_price / total_quantity;
}
static double total_sales() {
return sum;
}
private:
int num;
int quantity;
double price;
static double sum;
static int count;
static int total_quantity;
static double total_price;
};
double Salesman::sum = 0;
int Salesman::count = 0;
int Salesman::total_quantity = 0;
double Salesman::total_price = 0;
int main() {
Salesman s1(101, 5, 23.5);
Salesman s2(102, 12, 24.56);
Salesman s3(103, 100, 21.5);
cout << "Total sales: " << Salesman::total_sales() << endl;
cout << "Average price: " << Salesman::avg_price() << endl;
return 0;
}
```
程序定义了一个 `Salesman` 类,包含了销售员号、销售件数、销售单价三个数据成员和一个构造函数、一个静态成员函数 `discount()` 和三个静态成员函数 `avg_price()`、`total_sales()` 和 `discount()`。在构造函数中,根据销售件数和单价计算销售额并存入 `sum` 中,并将销售件数、销售额等数据存入对应的静态数据成员中。在 `discount()` 函数中,根据销售员数是否达到 10 人判断是否享受 9.8 折优惠。在 `avg_price()` 函数中,计算并返回每件商品的平均售价。在 `total_sales()` 函数中,返回当日此商品的总销售款。
在 `main()` 函数中,定义了三个销售员对象,分别记录销售情况。最后输出总销售款和平均售价,通过调用 `Salesman::total_sales()` 和 `Salesman::avg_price()` 静态函数实现。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)