c++ - 我提出什么条件才能进一步增加 5% 的折扣?
问题描述
要支付的金额通常是通过将单价乘以销售数量来计算的。但是,以下适用:
- 如果售出的数量大于或等于降价数量,则对该商品给予 10% 的折扣。
- 如果给予折扣,并且要支付的新金额超过 1000.00 美元,则再给予 5% 的折扣。
- 如果未给予折扣,并且要支付的金额超过 1000.00 美元,则给予 5% 的折扣
#include <iostream>
using namespace std;
int main(){
int items;
int i=0;
int item_num;
double unit_price;
double price_break;
double quantity_sold;
double amount_paid;
double discount=0.00;
double net_payment;
amount_paid=unit_price*quantity_sold;
if(quantity_sold>=price_break){
discount=0.1*amount_paid;
new_amount=amount_paid-discount
//Quantity sold is more than price break which places discount at 10%
net_payment=new_amount;
}else{
//Need help with this. How do I further add 5%?
if((quantity_sold>=price_break)&&(new_amount>1000)){
discount=0.1*amount_paid;
net_payment=amount_paid-discount;
}
else{
if(amount_paid>1000){
// discount is 5% since the amount to be paid exceeds $1000
discount=0.05*amount_paid;
net_payment=amount_paid-discount;
}
else{
if(quantity_sold<price_break){
//No discount and the amount to be paid doesn't exceeds $1000
net_payment=amount_paid;
}
}
}
}
解决方案
由于第二个和第三个条件导致同样的事情发生,您可以将规则简化如下:
首先,如果售出的数量大于或等于价格中断数量,则对该商品给予 10% 的折扣。
无论在第 1 步中是否给予折扣,如果要支付的金额超过 1,000 美元,则给予 5% 的折扣。
因此,只需摆脱else
外部的块,if
而是只需测试金额是否超过 1,000 美元。
amount_paid=unit_price*quantity_sold;
if(quantity_sold>=price_break){
// apply first discount if quantity is high enough
discount=0.1*amount_paid;
amount_paid-=discount;
}
if(amount_paid>1000){
// discount is 5% since the amount to be paid exceeds $1000
discount=0.05*amount_paid;
amount_paid-=discount;
}
推荐阅读
- python - 如何在不提供所有字段值的情况下保存 django 模型的对象?
- javascript - 如何使用 Office Js 设置多个单元格属性?
- android - 如果未选择任何项目,则带有暴露下拉菜单的 TextInputLayout 不会返回其原始形式(提示中心垂直)
- sql-server - 联合表自身限制为最大行数
- influxdb - Chronograf UI 显示同一时间间隔的所有点
- reactjs - 反应搜索框->调用地图功能时功能不起作用
- mysql - 如何使用动态 mysql 制作先验表
- javascript - 在检查有效的 Javascrip 对象时,此类型始终是对象
- android - notifyDataSetChange 不适用于适配器
- linux - 如何设置自定义 systemd 网络配置文件搜索路径