u012904555 于 2013.12.26 20:11 提问

#include
using namespace std;
void kaishi();
void yinpin ();
int xuanzhe[6]={0};
void kaishi()
{
cout<<"**********************************************"<<endl;
cout<<"**********************************************"<<endl;
cout<<"*************饮料自动销售机欢迎你*************"<<endl;
cout<<"**********************************************"<<endl;
cout<<"**********************************************"<<endl;

}
void yinpin ()
{
cout <<"输入对应的数字并确定"<<endl;
cout<<"------------------------------------------------------"<<endl;
cout<<"1、可口可乐（3元）2、百事（2元）3、橙汁（3元）4、咖啡（5元）5、纯净水（1.5元）"<<endl;
cout<<"----------------6、完成。------------------------"<<endl;
}
class yinliao
{
public:
yinliao();
void xuanz_yinpin();
void shuqian();
void add();
void xuanz();
void print();
protected:
int sum;//总钱数
};

yinliao::yinliao ()
{
sum=0;
}
void yinliao ::xuanz_yinpin()
{
int p;
int i;
for(i=0;i<7;i++)
{

``````    cin >>p;
if(p==6)//输入6时，返回主页
{
break;
}
else
{
xuanzhe[i]=p;
}
``````

}
}

void yinliao::print()
{
int m;
int j=0;
int k=0;
int q=0;
cout<<"您的选择为："<<endl;
for(m=0;m<7;m++)
{
if(xuanzhe[j]==0)
{
break;
}
else

``````    switch(xuanzhe[j++])
{
case 1:
cout<<" *可口可乐*"<<endl;
break;
case 2:
cout<<" *百事*"<<endl;
break;
case 3:
cout<<" *橙汁*"<<endl;
break;
case 4:
cout<<" *咖啡*"<<endl;
break;
case 5:
cout<<" *纯净水*"<<endl;
break;

}
``````

}
for(m=0;m<6;m++)//对选择的饮料进行打印
{
if(xuanzhe[k]==0)
{
break;
}
else
{
switch(xuanzhe[k++])
{
case 1:
cout<<" 可口可乐"<<endl;
case 2:
cout<<" 百事"<<endl;
break;
case 3:
cout<<" 橙汁"<<endl;
break;
case 4:
cout<<" 咖啡"<<endl;
break;
case 5:
cout<<" 纯净水"<<endl;
break;

``````    }
}
``````

}
}
void yinliao::add()//计算总钱数
{
int a;
int j=0;

for(a=0;a<6;a++)
{
if(xuanzhe[j]==0)
{
break;
}
else
{
switch(xuanzhe[j++])
{
case 1:
sum=sum+3;
break;
case 2:
sum=sum+2;
break;
case 3:
sum=sum+3;
break;
case 4:
sum=sum+5;
break;
case 5:
sum=sum+1.5;
break;
}
cout<<"总钱数为:"<<sum<<"元"<<endl;
}
}
}
void yinliao::shuqian()
{
int money;
while(1)
{
cout<<"请输入钱数并按确定"<<endl;

``````cout<<"    ";

cin>>money;
``````

if(money>=sum)
{
break;
}
else
{
cout<<" ";

cout<<"你输入的钱数有误，请重新输入"<<endl;
}
}
cout<<" ";

cout<<"您输入的钱数为:"<<money<<"元,找零:"<<money-sum<<"元"<<endl;
}

int main()
{

yinliao y1;

int w;

kaishi();

while(1)
{
cout<<endl<<" 请选择："<<endl;
cout<<"1购买 2结账付款"<<endl;
cout<<"---------------------------------------------"<<endl;

cout<<"输入对应数字并按确定."<<endl;
cout<<" "<<endl;

cin>>w;

switch(w)
{
case 1:
yinpin ();
y1.xuanz_yinpin();
y1.print();
break;
case 2:

``````  y1.add();
goto fk;
``````

}
}
fk:
y1.shuqian();
system("pause");
return 0;
}