当前位置:网站首页>Pta:7-31 journal charges
Pta:7-31 journal charges
2022-06-23 04:37:00 【Sy_ Faker】
The post office offers subscriptions to two periodicals : Magazines and newspapers . Give the framework of the following base class :
class Periodical {
protected:
string title; // name
public:
virtual void display()=0;// Print charges
}
With Periodical Base class , structure Magazine and Newspaper class .
Generate the above class and write the main function , The main function is required to have a base class Periodical Pointer array , Array elements do not exceed 10 individual .
Periodical *pp[10];
The main function is based on the input information , Corresponding establishment Magazine, Newspaper Class object , about Magazine Give the number of subscription periods and the price of each period , about Newspaper Give the number of subscription weeks , Number of publications per week and price per copy .
Input format : One line per test case , The first item is type ,1 by Magazine,2 by Newspaper, The second item is the name , The third item is the unit price ,Magazine The fourth item of is the number of periods ,Newspaper The fourth item is the number of subscription weeks , The fifth item is the number of publications per week .
When the output , Print the name and charge of each journal in turn ( Keep one place after the decimal point ).
sample input :
1 AAA 12.8 6
1 BB 15 3
2 CCCC 2.1 16 3
2 DD 0.7 55 7
1 EEE 18 3
0
sample output :
AAA 76.8
BB 45.0
CCCC 100.8
DD 269.5
EEE 54.0
#include<iostream>
#include<cstring>
#include<iomanip>
using namespace std;
class Periodical
{
protected:
string title;
public:
virtual void display()=0;// Pure virtual function
};
class Magazine:public Periodical
{
int num;
double price;
public:
Magazine(string p,double a,int b)
{
title=p;// Assignment between strings , The same below
price=a;
num=b;
}
virtual void display()// Output , The practice of keeping one decimal place , The same below
{
cout<<setiosflags(ios::fixed)<<title<<setprecision(1)<<" "<<num*price<<endl;
}
};
class Newspaper:public Periodical
{
int num;
int times;
double price;
public:
Newspaper(string p,double a,int b,int t)
{
times=t;
num=b;
price=a;
title=p;
}
virtual void display()
{
cout<<setiosflags(ios::fixed)<<title<<" "<<setprecision(1)<<num*price*times<<endl;
}
};
int main()
{
Periodical *pp[10];// Create a base class pointer array
int k,b,t,count=0;
string str;
double a;
cin>>k;
getchar();// Remember to return " eliminate "
while(k!=0)
{
switch(k)// use switch To determine which category it belongs to
{
case 1:
cin>>str>>a>>b;
pp[count]=new Magazine(str,a,b);// Initialize after derivation
break;
case 2:
cin>>str>>a>>b>>t;
pp[count]=new Newspaper(str,a,b,t);
break;
}
pp[count]->display();// Directly call to output
cin>>k;
getchar();
count++;// Use next pointer
}
return 0;
}
边栏推荐
- JVM调优简要思想及简单案例-为什么需要JVM调优?
- How does flutter achieve different zoom animation effects
- leetcode 91. Decode Ways 解码方法(中等)
- PTA:6-73 函数调用
- Compilation, installation and global configuration section description of haproxy
- Xiaojinwei, chairman of Chenglian Technology: implement the national strategy of data economy and lead the development of new consumption in the digital era!
- There is a problem with redis startup
- Weekly Postgres world news 2022w02
- JD cloud distributed database stardb won the "stability practice pioneer" of China Academy of information technology
- [pycharm] ide Eval resetter
猜你喜欢

如何保证应用程序的安全性

How to ensure application security

支持在 Kubernetes 运行,添加多种连接器,SeaTunnel 2.1.2 版本正式发布!

It supports running in kubernetes, adds multiple connectors, and seatunnel version 2.1.2 is officially released!

JVM调优简要思想及简单案例-为什么需要JVM调优?

Implementation of VGA protocol based on FPGA

Software development in 2022: five realities CIOs should know

How node+express operates cookies

什么是元数据

How to make the page number start from the specified page in word
随机推荐
JVM调优简要思想及简单案例-为什么需要JVM调优?
Sequence table lookup
LabVIEW displays both hexadecimal and normal characters in the same table
Weekly Postgres world news 2022w02
2022年的软件开发:首席信息官应该知道的五个现实
Lighthouse locally deployed TCA code analysis tool
PTA:6-71 时钟模拟
一篇文章学会er图绘制
Create a desktop shortcut to your appimage
Xiaojinwei, chairman of Chenglian Technology: implement the national strategy of data economy and lead the development of new consumption in the digital era!
12 excellent practices of wireless network security
最新编程语言排行榜
Svg+js smart home monitoring grid layout
[binary tree] flip equivalent binary tree
Section 2: spingboot unit test
Latest programming language rankings
Leetcode 1208. Try to make the strings equal as much as possible (finally solved, good night)
It supports running in kubernetes, adds multiple connectors, and seatunnel version 2.1.2 is officially released!
Online text filter less than specified length tool
Avltree - arbre de recherche binaire équilibré