当前位置:网站首页>PTA:7-65 饮料的价格
PTA:7-65 饮料的价格
2022-06-23 03:47:00 【Sy_Faker】
一个茶吧提供三类饮料:茶、咖啡和牛奶。其中本地茶要另加50%的服务费,其它茶要加20%的服务费;现磨咖啡要加100%的服务费,其它咖啡加20%的服务费;牛奶不加服务费,服务费精确到小数点一位。
给出下面的基类框架:
Class Drink { protected:
int NO; //编号
int amount; //数量
float price; //单价
public:
virtual void display()=0;//输出费用
}
以Drink为基类,构建出Tea, Coffee和Milk三个类。
生成上述类,并编写主函数,要求主函数中有一个基类Drink指针数组,数组元素不超过10个。
Drink *pd[10];
主函数根据输入的信息,相应建立Tea, Coffee或Milk类对象,并给出收费。
输入格式:每个测试用例占一行,每行给出饮料的基本信息,第一个为饮料的类型,茶为1,咖啡为2,牛奶为3。接下来是申请的编号(100-999),然后是数量和单价。对于茶叶来说,接下来输入一个地区代码,其中1代表本地。对于咖啡来说,接下来要输入一个加工代码,其中1代表现磨。最后一行为0。 要求输出编号和收费(小数点后保留1位小数)。
提示:应用虚函数实现多态
输入样例:
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
输出样例:
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; //编号
int amount; //数量
float price; //单价
public:
Drink(int n,int a,float p):NO(n),amount(a),price(p){
;}
virtual void display()=0;//输出费用
};
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;
}
边栏推荐
- [deep learning] deep learning reasoning framework tensorrt MNN openvino onnxruntime
- 无线网络安全的12个优秀实践
- Analysis on the current situation of the Internet of things in 2022
- Dynamics 365 插件中权限操作
- 静态查找表和静态查找表
- Compilation, installation and global configuration section description of haproxy
- Imitation 360 desktop suspended ball plug-in
- Sequence table lookup
- What is the difference between redistemplate and CacheManager operation redis
- svg d3.js生成tree树状图
猜你喜欢

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

Review the SQL row column conversion, and the performance has been improved

Implementation of VGA protocol based on FPGA

【一起上水硕系列】Day Three - preview4

移动端城市列表排序js插件vercitylist.js

AI 视频云 VS 窄带高清,谁是视频时代的宠儿

Deploying Apache pulsar on kubesphere

JD cloud distributed database stardb won the "stability practice pioneer" of China Academy of information technology

mysql存储引擎之Myisam和Innodb的区别

理想汽车×OceanBase:当造车新势力遇上数据库新势力
随机推荐
Static lookup tables and static lookup tables
Code refactoring Guide
8 key indicators to measure technology debt in 2022
Latest programming language rankings
What is metadata
无线网络安全的12个优秀实践
Pyspark, paid for data cleaning and uploading to the database
Black horse PostgreSQL, why is it black in the end
【深度学习】深度学习推理框架 TensorRT MNN OpenVINO ONNXRuntime
Tables de recherche statiques et tables de recherche statiques
[two points] leetcode1011 Capacity To Ship Packages Within D Days
P1363 幻象迷宫(dfs)
Flutter怎么实现不同缩放动画效果
leetcode 91. Decode Ways 解码方法(中等)
深度学习 TensorFlow入门
Pytorch---使用Pytorch的预训练模型实现四种天气分类问题
SVG+JS智能家居监控网格布局
靜態查找錶和靜態查找錶
PTA:7-87 集合的模拟实现(类模板)
华为联机对战服务玩家快速匹配后,不同玩家收到的同一房间内玩家列表不同