商品打折

*商场商品打折。消费在400元以上打9折,800元以上打8折。1000元以上打7.5折。消费金额可分次累计。如果把“消费”定义为一个类,那么每次消费都可以定义为一个对象···可是因为可以打折,两次消费金额的累加结果不是简单的相加···比如第一次消费300元,第二次消费了200元,那么总金额应该是(300+200)*0.9=450元。
请编写“消费”类定义并为该类重载“+”运算符,以实现有多次消费时,用“+”求累计消费金额,并能将累计的消费金额输出。要求使用某标记使已经打过折的“消费”不再参加打折。
*/

#include <iostream>
using namespace std;
/*判断打折时候本次钱数算在内.以往的消费以实际收入计算.*/
class consume
{
float sum;           //所有物品标价的和
float discountSum;   //商店实际所收的钱数
float dis;           //总的优惠钱数
 public:
consume();
consume(float);
~consume();
void consume::operator +=(consume &);
    void displayStatus(); 
};

consume::consume()
{
    sum=0.0;
    discountSum=0.0;
    dis=0.0;
}
consume::consume(float money)
{
    sum=money;
    if(money<400)discountSum+=money;
    else if(money<800)discountSum=money*0.9;
    else if(money<1000)discountSum=money*0.8;
    else if(money>=1000)discountSum=money*0.75;
    dis=sum-discountSum;
}   
consume::~consume(){}
void consume::operator+=(consume &another)
{
    sum+=another.sum;
    int ds=discountSum;ds+=another.sum;   //实际消费+本次金额(实际物品价格)
    if(ds<400)discountSum+=another.sum;
    else if(ds<800)discountSum+=another.sum*0.9;
    else if(ds<1000)discountSum+=another.sum*0.8;
    else discountSum=another.sum*0.75;
    dis=sum-discountSum;
}   
void consume::displayStatus()
{
 cout<<"没有打折前你应付的消费金额为:"<<sum<<endl;
 cout<<"打折后你实际付的消费金额为:"<<discountSum<<endl;
 cout<<"总优惠金额为:"<<dis<<endl;
}

int main()
{
 consume everyConsume;
 cout<<"请输入下一个金额:(以-1结束)"<<endl;
 float m=0.0;
 cin>>m;
 while(m!=-1)
 {
     consume con(m);
     everyConsume+=con;
     everyConsume.displayStatus();
     cout<<"请输入下一个金额:(以-1结束)"<<endl;
     cin>>m;
 }   
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值