当前位置:网站首页>PTA: price of 7-65 beverage

PTA: price of 7-65 beverage

2022-06-23 04:38:00 Sy_ Faker

A tea bar offers three kinds of drinks : tea 、 Coffee and milk . The local tea should be added 50% Service charge , Other teas should be added 20% Service charge ; Freshly ground coffee needs to be added 100% Service charge , Add other coffee 20% Service charge ; No service charge for milk , The service charge is accurate to one decimal place .

Give the following base class framework :

Class Drink { protected:

int NO; // Number

int amount; // Number

float price; // The unit price

public:

virtual void display()=0;// Output costs

}

With Drink Base class , build Tea, Coffee and Milk Three categories .

Generate the above class , And write the main function , The main function is required to have a base class Drink Pointer array , Array elements do not exceed 10 individual .

Drink *pd[10];

The main function is based on the input information , Corresponding establishment Tea, Coffee or Milk Class object , And give the charge .

Input format : One line per test case , Each line gives basic information about the drink , The first is the type of drink , Cha Wei 1, Coffee is 2, Milk is 3. Next is the number of the application (100-999), Then the quantity and unit price . For tea , Next, enter a region code , among 1 Representing local . For coffee , Next, enter a machining code , among 1 Stands for present grinding . Last act 0. Request output number and charge ( After the decimal point 1 Decimal place ).

Tips : Using virtual function to realize polymorphism

sample input :

1 106 3 33 1

1 103 2 20 2

3 109 1 15

2 107 2 15.8 1

2 232 3 21 29

0

sample output :

106 148.5

103 48.0

109 15.0

107 63.2

232 75.6

#include<iostream>
using namespace std;
class Drink 
{
     
	protected:
		int NO; // Number 
		int amount; // Number 
		float price; // The unit price 
	public:
		Drink(int n,int a,float p):NO(n),amount(a),price(p){
    ;}
		virtual void display()=0;// Output costs 
};
class Tea:public Drink
{
    
	int pd;
	public:
		Tea(int n,int a,float p,int k):Drink(n,a,p){
    pd=k;}
		virtual void display()
		{
    
			float temp=amount*price;
			if(pd==1)temp*=1.5;
			else temp*=1.2;
			printf("%d %.1f\n",NO,temp);
		}
};
class Coffee:public Drink
{
    
	int pd;
	public:
		Coffee(int n,int a,float p,int k):Drink(n,a,p){
    pd=k;}
		virtual void display()
		{
    
			float temp=amount*price;
			if(pd==1)temp*=2;
			else temp*=1.2;
			printf("%d %.1f\n",NO,temp);
		}
};
class Milk:public Drink
{
    
	public:
		Milk(int n,int a,float p):Drink(n,a,p){
    ;}
		virtual void display()
		{
    
			float temp=amount*price;
			printf("%d %.1f\n",NO,temp);
		}
};
int main()
{
    
	Drink *pd[10];
	int type;
	cin>>type;
	int n,a,pp,i=0;
	float p;
	while(type!=0)
	{
    
		cin>>n>>a>>p;
		switch(type)
		{
    
			case 1:
				cin>>pp;
				pd[i]=new Tea(n,a,p,pp);
				break;
			case 2:
				cin>>pp;
				pd[i]=new Coffee(n,a,p,pp);
				break;
			case 3:
				pd[i]=new Milk(n,a,p);
				break;
		}
		pd[i++]->display();
		cin>>type;
	}
	delete *pd;
}
原网站

版权声明
本文为[Sy_ Faker]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206222259538456.html