当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

X24cxx series EEPROM chip C language universal reading and writing program

Halcon glue line detection - template matching, pose transformation, glue width, glue continuity detection

Leetcode 1208. 尽可能使字符串相等(终于解决,晚安)

Cool mouse following animation JS plug-ins 5

volatile 与线程的那些事

深度学习 TensorFlow入门

There is a problem with redis startup

【Pytorch】用自动微分求sin(x)的导数

Twitter cooperates with Shopify to introduce merchant products into twitter shopping

How to ensure application security
随机推荐
距离度量 —— 余弦距离(Cosine Distance)
How node+express operates cookies
Create a desktop shortcut to your appimage
[learn FPGA programming from scratch -40]: Advanced - Design - competition and risk
语料库数据处理个案实例(分词和分句、词频统计、排序)
TS进阶之infer
How MySQL deletes a row of data in a table
2022年烷基化工艺考题及模拟考试
OpenJudge NOI 1.13 49:计算对数
[acnoi2022] no way without guessing
X24cxx series EEPROM chip C language universal reading and writing program
What are the characteristics of SRM supplier management system developed by manufacturing enterprises
[binary tree] completeness test of binary tree
制造型企业开发的SRM供应商管理系统特点是什么
OpenJudge NOI 1.13 51:古代密码
PTA:7-58 图书音像出租管理
Does the network disk also roll inside?
IDEA-导入模块
x64dbg 基本使用技巧
基于FPGA的VGA协议实现