某超市开展促销活动,购买价值满1000元可享受8.8折优惠,价值满5000元可享受7.8折优惠,大于10000元可享受6.8折优惠;此外,如顾客持有会员卡,另可叠加9.5折,如顾客没有会员卡,可选择办理会员卡,卡费260。用c++编写程序,在收银机上实现以上功能,即对于任意顾客实际消费后结账,显示其需要支付的综合折后费用。 功能要求 1、要求每结算完一位顾客后,程序能接着运行,计算下一位顾客; 2、至少定义2种不同类型的自定义函数; 3、要求判断是否时,用字符y与n;
时间: 2024-03-27 12:41:04 浏览: 8
好的,我会根据您的要求进行回答,以下是c++代码:
```
#include<iostream>
using namespace std;
//定义函数:计算折扣后的价格
double calculateDiscount(double totalPrice)
{
double discount = 1;
if(totalPrice >= 10000)
{
discount = 0.68;
}
else if(totalPrice >= 5000)
{
discount = 0.78;
}
else if(totalPrice >= 1000)
{
discount = 0.88;
}
return totalPrice * discount;
}
//定义函数:计算会员卡折扣后的价格
double calculateMembershipDiscount(double totalPrice)
{
double discount = 0.95;
return totalPrice * discount;
}
int main()
{
while(true)
{
double totalPrice;
char hasMembershipCard;
cout << "请输入顾客总消费金额:" << endl;
cin >> totalPrice;
cout << "是否有会员卡?(y/n)" << endl;
cin >> hasMembershipCard;
if(hasMembershipCard == 'y')
{
cout << "顾客需要支付:" << calculateMembershipDiscount(totalPrice) << "元" << endl;
}
else
{
cout << "是否申请会员卡?(y/n)" << endl;
char applyMembershipCard;
cin >> applyMembershipCard;
if(applyMembershipCard == 'y')
{
cout << "顾客需要支付:" << calculateMembershipDiscount(totalPrice + 260) << "元" << endl;
}
else
{
cout << "顾客需要支付:" << calculateDiscount(totalPrice) << "元" << endl;
}
}
cout << "是否继续结算下一位顾客?(y/n)" << endl;
char continueCheckout;
cin >> continueCheckout;
if(continueCheckout == 'n')
{
break;
}
}
return 0;
}
```
代码解析:
1. calculateDiscount函数用于计算折扣后的价格,根据题目要求,分别判断总价是否大于10000元、5000元和1000元,根据不同的条件返回不同的折扣。
2. calculateMembershipDiscount函数用于计算会员卡折扣后的价格,根据题目要求,直接返回总价乘以会员卡的折扣。
3. 在主函数中,使用while循环来持续结算顾客的消费。
4. 接收顾客的总消费金额和是否有会员卡,根据不同的条件调用不同的函数来计算价格。
5. 如果顾客没有会员卡,再次询问是否申请会员卡,如果申请,将会员卡费用加入总价,并计算会员卡折扣后的价格。
6. 最后询问是否继续结算下一位顾客,如果选择不继续,则跳出循环,程序结束。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)