*商场商品打折。消费在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;
}