能有偿吗? 这个题有点费时间啊。时间就是金钱。。。
对于我这种初学者,好难得说。
#include
#include
#include "stdio.h"
using namespace std;
class moneycounter
{ public:
moneycounter():input_money(0.0f)
{
}
~moneycounter()
{
}
void getgukemoney();
float money_from_buyer();
void clear();
void return_money(float);
private:
float input_money;
};
class goodsinfo
{ public:
goodsinfo():name(""),price(0.0f),total(0){}
~goodsinfo(){}
void set_goods(string,float,int);
string goods_name();
float goods_price();
int goods_number();
private: string name;
float price;
int total;
};
class drinkmachine
{ public: drinkmachine();
~drinkmachine(){} void showchoices();
void inputmoney();
bool goodsitem(int);
void return_allmoney();
private: moneycounter moneyctr;
goodsinfo v_goods[5];
};
void moneycounter::getgukemoney()
{ float money;
cout<
input_money+=money;
cout<
float moneycounter::money_from_buyer()
{ return input_money;
}
void moneycounter::clear()
{ input_money=0.0f; return;
}
void moneycounter::return_money(float change)
{
cout<
void goodsinfo::set_goods(string n,float p,int num)
{
name=n;
price=p;
total=num;
} string goodsinfo::goods_name()
{
return name;
}
float goodsinfo::goods_price()
{
return price;
}
int goodsinfo::goods_number()
{
return total;
} //******************************************* drinkmachine::drinkmachine()
{
v_goods[0].set_goods("橙汁",3,20);
v_goods[1].set_goods("咖啡",5,0);
v_goods[2].set_goods("纯净水",1.5,20);
v_goods[3].set_goods("可口可乐",2,30);
v_goods[4].set_goods("百事可乐",2,28);
return;
}
void drinkmachine::showchoices()
{ cout.precision(2);
cout.setf(ios::fixed);
cout<
{
cout< }
cout<<"5 退款并且退出"<
void drinkmachine::inputmoney()
{
cout<
return;
}
bool drinkmachine::goodsitem(int select)
{
int number=v_goods[select].goods_number();
if(number>0)
{
if(moneyctr.money_from_buyer()>=v_goods[select].goods_price())
{
float change=moneyctr.money_from_buyer()-v_goods[select].goods_price();
cout<
{
moneyctr.return_money(change);
}
return true;
}
else
{
cout<
}
else
{ cout<
return false;
}
void drinkmachine::return_allmoney()
{
cout<
void main()
{
drinkmachine dri;
string buf;
bool go_on(true),cash_on(true),got_it(true);
cout<
{
while(cash_on)
{
dri.inputmoney();
cout<
if(buf=="n" || buf=="no")
{
cash_on=false;
}
}
dri.showchoices();
cin>>buf;
int select=atoi(buf.c_str());
if(select==5)
{
dri.return_allmoney();
go_on=false;
}
else
{
got_it=dri.goodsitem(select);
if(got_it)
{
go_on=false;
}
else
{
cout<
if(buf=="y" || buf=="yes")
{
cash_on=true; go_on=true;
}
else
{
dri.return_allmoney();
go_on=false;
}
}
}
}
cout<