|
发表于 2023-10-18 11:59:38
|
显示全部楼层
你看这样可以吗?
- #include <iostream>
- #include <stdlib.h>
- using namespace std;
- enum PRICE_RANGE
- {
- PRICE_50 = 0,
- PRICE_50_100 = 1,
- PRICE_100_200 = 2,
- PRICE_200 = 3
- };
- int get_price_level(int quantity)
- {
- if (quantity < 50)
- {
- return PRICE_50;
- }
- else if (quantity >= 50 && quantity < 100)
- {
- return PRICE_50_100;
- }
- else if (quantity >= 100 && quantity < 200)
- {
- return PRICE_100_200;
- }
- else if (quantity >= 200)
- {
- return PRICE_200;
- }
- }
- void get_total_price(int quantity, double *total_price)
- {
- int level = get_price_level(quantity);
- switch (level)
- {
- case PRICE_50:
- *total_price = quantity * 10;
- break;
- case PRICE_50_100:
- *total_price = quantity * 10 * 0.95;
- break;
- case PRICE_100_200:
- *total_price = quantity * 10 * 0.9;
- break;
- case PRICE_200:
- *total_price = quantity * 10 * 0.85;
- break;
- default:
- break;
- }
- }
- int main (void)
- {
- int quantity = 0;
- double total_price = 0;
- cout << "请输入商品的购买数量:";
- cin >> quantity;
- if (quantity <= 0)
- {
- cout << "购买商品数量不能小于或等于0" << endl;
- exit(EXIT_FAILURE);
- }
- get_total_price(quantity, &total_price);
- cout << "购买商品数量是:,"<< quantity <<"商品总价是:" << total_price << endl;
- exit (EXIT_SUCCESS);
- }
复制代码 |
评分
-
查看全部评分
|