发布时间:2023-05-13 16:30
题目一: 农夫果园
Description
秋天到了,果园里的水果成熟了,商贩们来收水果了,农夫们都希望自己的水果能卖个好价钱。
现在果园里有三种水果正在销售,苹果(Apple)、香蕉(Banana)、梨(Pear)。每次销售都会记录下水果的种类、单价和总量,input()函数可以读取每条销售记录的单价和总量,total()函数可以计算出这次销售的总价。
但是,销售员在记录时忙中出错,各中水果的单价和总量的单位没有统一。单价是每公斤的价格,而水果是按箱记录的。其中,苹果一箱按30公斤计算,香蕉一箱按25公斤计算,梨一箱按20公斤计算。每种水果每次销售的总价是“单价总量每箱公斤数”。
现在,你来设计一个程序帮忙计算果园卖出水果的总价。由于total()函数对每种水果的计算方式都不一样,因此使用多态来实现。
你设计并实现这个水果类的派生体系,使得main()函数能够运行并得到正确的输出。调用格式见append.cc
Input
输入的第一个整数n,表示后面有n条水果收购的记录。每条记录分为3部分,水果种类、单价和总量。
Output
输出为一行,表示整个果园卖出水果的总价。
Sample Input
5
Apple 4.2 100
Banana 8.8 50
Apple 4.5 200
Banana 7.8 100
Pear 3.7 100
Sample Output
Total Price : 77500
题目给定代码
int main()
{
Fruit* fruit;
string fruit_name;
double sum = 0.0;
int cases;
cin >> cases;
for (int i = 1; i <= cases; i++)
{
cin >> fruit_name;
if (fruit_name == \"Apple\")
fruit = new Apple();
if (fruit_name == \"Banana\")
fruit = new Banana();
if (fruit_name == \"Pear\")
fruit = new Pear();
fruit->input();
sum += fruit->total();
delete fruit;
}
cout << \"Total Price : \" << sum << endl;
return 0;
}
标程
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;
class Fruit {
protected:
double weight, price;
public:
virtual void input() = 0;
virtual double total() = 0;
Fruit() {
}
};
class Apple : public Fruit {
public:
void input() {
cin >> weight >> price;
}
double total() {
return weight * price * 30;
}
};
class Banana : public Fruit {
public:
void input() {
cin >> weight >> price;
}
double total() {
return weight * price * 25;
}
};
class Pear : public Fruit {
public:
void input() {
cin >> weight >> price;
}
double total() {
return weight * price * 20;
}
};
int main()
{
Fruit* fruit;
string fruit_name;
double sum = 0.0;
int cases;
cin >> cases;
for (int i = 1; i <= cases; i++)
{
cin >> fruit_name;
if (fruit_name == \"Apple\")
fruit = new Apple();
if (fruit_name == \"Banana\")
fruit = new Banana();
if (fruit_name == \"Pear\")
fruit = new Pear();
fruit->input();
sum += fruit->total();
delete fruit;
}
cout << \"Total Price : \" << sum << endl;
return 0;
}
题目二: 薪酬计算
Description
某公司有经理(Manager)、雇员(Employee)、小时工(HourlyWorker)和营销人员(CommWorker)四类员工(Person),他们的薪酬计算方法各不一样:
Manager采用年薪制;
Employee按月计酬,方法是:月基本工资+奖金,奖金是指奖励几个月工资;
HourlyWorker是按工作时间计酬,方法是:工作小时*每小时工资;
CommWorker按月计酬,方法是:月基本工资+年销售额的2%;
由于每类员工(Person)的工资输入(input函数)、工资计算办法(pay函数)、显示信息(show函数)均不一样,因此使用多态来实现。
请仔细阅读append.cc代码,并设计并实现这个员工类的派生体系,使main()函数能够运行并得到正确的输出。
Input
输入的第一个整数n,表示用n组测试样例。
每组测试样例占一行,分别为员工职位、员工姓名、基础工资base、附加项k(Manager无此项)。
员工职位为Manager,其base为年薪,单位为千元;
员工职位为Employee,其base为月薪,单位为千元,附加项为奖励k个月的工资;
员工职位为HourlyWorker,其base为时薪,单位为元,附加项为年工作k个小时;
员工职位为CommWorker,其base为月薪,单位为千元,附加项是年销售额k千元;
Output
按格式输出每个人的姓名、职位和最终计算的年薪,详细格式见sample。
Sample Input
6
Manager Zhang3 200
Employee Li4 8 5
Employee Wang5 10 3
HourlyWorker Zhao6 45 650
CommWorker Liu7 5 300
CommWorker Sun8 6 180
Sample Output
Zhang3 (Manager) Annual Salary is 200000.
Li4 (Employee) Annual Salary is 136000.
Wang5 (Employee) Annual Salary is 150000.
Zhao6 (HourlyWorker) Annual Salary is 29250.
Liu7 (CommWorker) Annual Salary is 66000.
Sun8 (CommWorker) Annual Salary is 75600.
题目给定代码
int main()
{
Person *person;
string name, job;
int cases;
cin >> cases;
for(int i = 1; i <= cases; ++i)
{
cin >> job >> name;
if(job == \"Manager\")
person = new Manager(name);
if(job == \"Employee\")
person = new Employee(name);
if(job == \"HourlyWorker\")
person = new HourlyWorker(name);
if(job == \"CommWorker\")
person = new CommWorker(name);
person->input();
person->show();
cout << \" Annual Salary is \" << person->pay() << \".\" << endl;
}
}
标程
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;
class Person {
protected:
string name;
int money;
public:
virtual void input() = 0;
virtual int pay() = 0;
virtual void show() = 0;
};
class Manager : public Person {
public:
Manager(string s) {
name = s;
}
void input() {
cin >> money;
}
int pay() {
return money * 1000;
}
void show() {
cout << name << \" (Manager)\";
}
};
class Employee : public Person {
private:
int k;
public:
Employee(string s) {
name = s;
}
void input() {
cin >> money >> k;
}
int pay() {
return (k + 12) * money * 1000;
}
void show() {
cout << name << \" (Employee)\";
}
};
class HourlyWorker : public Person {
private:
int k;
public:
HourlyWorker(string s) {
name = s;
}
void input() {
cin >> money >> k;
}
int pay() {
return money * k;
}
void show() {
cout << name << \" (HourlyWorker)\";
}
};
class CommWorker : public Person {
private:
int k;
public:
void input() {
cin >> money >> k;
}
CommWorker(string s) {
name = s;
}
int pay() {
return money * 1000 * 12 + 20 * k;
}
void show() {
cout << name << \" (CommWorker)\";
}
};
int main()
{
Person* person;
string name, job;
int cases;
cin >> cases;
for (int i = 1; i <= cases; ++i)
{
cin >> job >> name;
if (job == \"Manager\")
person = new Manager(name);
if (job == \"Employee\")
person = new Employee(name);
if (job == \"HourlyWorker\")
person = new HourlyWorker(name);
if (job == \"CommWorker\")
person = new CommWorker(name);
person->input();
person->show();
cout << \" Annual Salary is \" << person->pay() << \".\" << endl;
}
return 0;
}
题目三: 驾驶员与汽车
Description
我们知道,目前我国的驾照大致可分为A、B、C三种,其中C证只能开小型客车(货车),B证可开中、小型客车(货车),A证没有限制。现在请定义如下几个类:
Automobile:抽象类,具有数据成员double speed,纯虚函数virtual void run() const = 0。
六种车型,即小型车Benz、Buick;中型车Zhongba、Beiqi;以及大型车Dayu、Jianghuai。它们都是Automobile的子类。
Driver类,具有string name和char type两个数据成员,前者是司机的名字,后者是司机持有的驾照类型(A、B或C)。提供Drive(Automobile *)方法,根据驾照类型判定该司机是否可以驾驶指定的汽车。
Input
输入分多行。第一行是一个整数M>0,表示之后有M个测试用例。
每个测试用例包括四部分:司机姓名(不含空白符)、驾照类型(A、B或C)、车型(分别用字母a~f表示六种车型,对应的车型可以从main()中看出)以及该车的行驶速度(double类型范围内的正数)。
Output
输出共M行,每个测试用例对应一行输入,具体格式参见样例。
Sample Input
4
zhangsan C a 100.33
lisi C d 100.33
wangwu B f 100.33
Tom A e 300.00
Sample Output
Driver zhangsan can drive Benz at speed of 100.33km/h.
A Benz is erased!
An automobile is erased!
Driver lisi cannot drive bus.
A Beiqi is erased!
An automobile is erased!
Driver wangwu cannot drive large bus.
A Jianghuai is erased!
An automobile is erased!
Driver Tom can drive Dayu at speed of 300.00km/h.
A Dayu is erased!
An automobile is erased!
HINT
1.使用typeid来判定一个基类指针实际指向的对象的类型。
2.注意:append.cc中已经给出了Driver类的一个方法,不要在Driver类重复定义了。
题目给定代码
void Driver::Drive(Automobile *automobile)
{
switch (type)
{
case \'A\':
cout<<\"Driver \"<<name<<\" can drive \";
automobile->run();
break;
case \'B\':
if (typeid(*automobile) == typeid(Dayu) || typeid(*automobile) == typeid(Jianghuai))
cout<<\"Driver \"<<name<<\" cannot drive large bus.\"<<endl;
else
{
cout<<\"Driver \"<<name<<\" can drive \";
automobile->run();
}
break;
case \'C\':
if (typeid(*automobile) != typeid(Benz) && typeid(*automobile) != typeid(Buick))
cout<<\"Driver \"<<name<<\" cannot drive bus.\"<<endl;
else
{
cout<<\"Driver \"<<name<<\" can drive \";
automobile->run();
}
break;
}
}
int main()
{
string name;
char type;
double speed;
char automobileType;
int cases;
Automobile *automobile;
cin>>cases;
for (int i = 0; i < cases; i++)
{
cin>>name>>type>>automobileType>>speed;
Driver driver(name, type);
switch (automobileType)
{
case \'a\':
automobile = new Benz(speed);
break;
case \'b\':
automobile = new Buick(speed);
break;
case \'c\':
automobile = new Zhongba(speed);
break;
case \'d\':
automobile = new Beiqi(speed);
break;
case \'e\':
automobile = new Dayu(speed);
break;
case \'f\':
automobile = new Jianghuai(speed);
break;
}
driver.Drive(automobile);
delete automobile;
}
return 0;
}
标程
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;
class Automobile
{
protected:
double speed;
public:
Automobile(double s) : speed(s) { }
virtual ~Automobile() { cout << \"An automobile is erased!\" << endl; }
public:
virtual void run() const = 0;
};
class Benz :virtual public Automobile
{
public:
Benz(double s) : Automobile(s) { }
~Benz()
{
cout << \"A Benz is erased!\" << endl;
}
void run() const { cout << fixed << setprecision(2) << \"Benz at speed of \" << speed << \"km/h.\" << endl; }
};
class Buick :virtual public Automobile
{
public:
Buick(double s) : Automobile(s) { }
~Buick()
{
cout << \"A Buick is erased!\" << endl;
}
void run() const { cout << fixed << setprecision(2) << \"Buick at speed of \" << speed << \"km/h.\" << endl; }
};
class Zhongba :virtual public Automobile
{
public:
Zhongba(double s) : Automobile(s) { }
~Zhongba()
{
cout << \"A Zhongba is erased!\" << endl;
}
void run() const { cout << fixed << setprecision(2) << \"Zhongba at speed of \" << speed << \"km/h.\" << endl; }
};
class Beiqi :virtual public Automobile
{
public:
Beiqi(double s) : Automobile(s) { }
~Beiqi()
{
cout << \"A Beiqi is erased!\" << endl;
}
void run() const { cout << fixed << setprecision(2) << \"Beiqi at speed of \" << speed << \"km/h.\" << endl; }
};
class Dayu :virtual public Automobile
{
public:
Dayu(double s) : Automobile(s) { }
~Dayu()
{
cout << \"A Dayu is erased!\" << endl;
}
void run() const { cout << fixed << setprecision(2) << \"Dayu at speed of \" << speed << \"km/h.\" << endl; }
};
class Jianghuai :virtual public Automobile
{
public:
Jianghuai(double s) : Automobile(s) { }
~Jianghuai()
{
cout << \"A Jianghuai is erased!\" << endl;
}
void run() const { cout << fixed << setprecision(2) << \"Jianghuai at speed of \" << speed << \"km/h.\" << endl; }
};
class Driver
{
private:
string name;
char type;
public:
Driver(string n, char t) : name(n), type(t) { }
void Drive(Automobile*);
};
void Driver::Drive(Automobile *automobile)
{
switch (type)
{
case \'A\':
cout<<\"Driver \"<<name<<\" can drive \";
automobile->run();
break;
case \'B\':
if (typeid(*automobile) == typeid(Dayu) || typeid(*automobile) == typeid(Jianghuai))
cout<<\"Driver \"<<name<<\" cannot drive large bus.\"<<endl;
else
{
cout<<\"Driver \"<<name<<\" can drive \";
automobile->run();
}
break;
case \'C\':
if (typeid(*automobile) != typeid(Benz) && typeid(*automobile) != typeid(Buick))
cout<<\"Driver \"<<name<<\" cannot drive bus.\"<<endl;
else
{
cout<<\"Driver \"<<name<<\" can drive \";
automobile->run();
}
break;
}
}
int main()
{
string name;
char type;
double speed;
char automobileType;
int cases;
Automobile *automobile;
cin>>cases;
for (int i = 0; i < cases; i++)
{
cin>>name>>type>>automobileType>>speed;
Driver driver(name, type);
switch (automobileType)
{
case \'a\':
automobile = new Benz(speed);
break;
case \'b\':
automobile = new Buick(speed);
break;
case \'c\':
automobile = new Zhongba(speed);
break;
case \'d\':
automobile = new Beiqi(speed);
break;
case \'e\':
automobile = new Dayu(speed);
break;
case \'f\':
automobile = new Jianghuai(speed);
break;
}
driver.Drive(automobile);
delete automobile;
}
return 0;
}
题目四: 来开个书店吧
Description
某出版社可出版图书和磁带。其中图书按照每页的价格乘以页数进行定价,磁带根据每10分钟的价格乘以磁带录音的分钟数进行定价。请定义Publicatioin、Book、Tape以及BookStore四个类。其中:
1)数据成员double price表示单价(对于书,是每页的价格;对于磁带,是每10分钟录音的价格)。
2)数据成员int length表示出版物的长度,对于书,是页数;对于磁带, 是分钟数。
3)成员函数getTotalPrice()用于返回一个出版物的定价。
4)构造函数Publication(double, int)用于构造一个出版物。
5)成员函数double getPrice() const和int getLength()用于返回出版物的单价及长度。
6)析构函数。
1)构造函数Book(double,int)。
2)重写父类的getTotalPrice返回定价,定价为单价乘以长度(即页数)。
3)析构函数。
1)构造函数Tape(double,int)。
2)重写父类的getTotalPrice返回定价。注意:price属性是每10分钟录音的单价,而磁带的长度不一定是10的整数倍。计算定价时,不足10分钟部分,按照10分钟计算。
3)析构函数。
4.BookStore是书店,具有数据成员Publications **pubs,是书店拥有的出版物列表;int num表示书店拥有的出版物数量。成员函数int getNumOfBook()和int getNumOfTape()分别计算书店中拥有的Book和Tape的数量。该类已经在appcode code中给出。
Input
输入分多行。
第一行是整数M>0,表示有M个测试用例。
每个测试占一行,分为三部分:第一部分是出版物类型(B表示Book,T表示Tape)、单价和数量(页数或分钟数)。
Output
见样例。
Sample Input
3
B 0.10 201
T 0.50 100
T 0.40 105
Sample Output
Call Publication’s constructor!
Call Book’s constructor!
Call Publication’s constructor!
Call Tape’s constructor!
Call Publication’s constructor!
Call Tape’s constructor!
Call Publication’s constructor!
Call Book’s constructor!
Call Publication’s constructor!
Call Tape’s constructor!
Call Publication’s constructor!
Call Tape’s constructor!
There are 1 books and 2 tapes. Their total price is 29.50.
Call Book’s de-constructor!
Call Publication’s de-constructor!
Call Tape’s de-constructor!
Call Publication’s de-constructor!
Call Tape’s de-constructor!
Call Publication’s de-constructor!
Call Book’s de-constructor!
Call Publication’s de-constructor!
Call Tape’s de-constructor!
Call Publication’s de-constructor!
Call Tape’s de-constructor!
Call Publication’s de-constructor!
Call BookStore’s de-constructor!
HINT
使用typeid判断对象指针指向的实际对象的类型。
题目给定代码
class BookStore
{
private:
Publication **pubs;
int num;
public:
BookStore(Publication **p, int n)
{
pubs = new Publication*[n];
num = n;
for (int i = 0; i < n; i++)
{
if (typeid(*(p[i])) == typeid(Book))
{
pubs[i] = new Book(p[i]->getPrice(), p[i]->getLength());
}
else
{
pubs[i] = new Tape(p[i]->getPrice(), p[i]->getLength());
}
}
}
int getNumOfBook()
{
int c = 0;
for (int i = 0; i < num; i++)
{
if (typeid(*(pubs[i])) == typeid(Book))
c++;
}
return c;
}
int getNumOfTape()
{
int c = 0;
for (int i = 0; i < num; i++)
{
if (typeid(*(pubs[i])) == typeid(Tape))
c++;
}
return c;
}
~BookStore()
{
for (int i = 0; i < num; i++)
{
delete pubs[i];
}
delete[] pubs;
cout<<\"Call BookStore\'s de-constructor!\\n\";
}
};
int main()
{
int cases, date;
char type;
double total,price;
Publication **pub;
cin>>cases;
pub = new Publication*[cases];
for (int i = 0; i < cases; i++)
{
cin>>type>>price>>date;
switch(type)
{
case \'B\':
pub[i] = new Book(price,date);
break;
case \'T\':
pub[i] = new Tape(price,date);
break;
}
}
BookStore bookStore(pub, cases);
cout<<\"There are \"<<bookStore.getNumOfBook()<<\" books and \"<<bookStore.getNumOfTape()<<\" tapes.\";
total = 0;
for (int i = 0; i < cases; i++)
{
total += pub[i] -> getTotalPrice();
}
cout<<\" Their total price is \"<<setprecision(2)<<fixed<<total<<\".\"<<endl;
for (int i = 0; i < cases; i++)
{
delete pub[i];
}
delete[] pub;
return 0;
}
标程
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;
class Publication {
protected:
double price;
int length;
public:
virtual double getTotalPrice() = 0;
Publication(double a, int b) {
price = a, length = b;
cout << \"Call Publication\'s constructor!\" << \"\\n\";
}
double getPrice() {
return price;
}
int getLength() {
return length;
}
virtual ~Publication() {
cout << \"Call Publication\'s de-constructor!\" << \"\\n\";
}
};
class Book : public Publication {
public:
Book(double s, int b) : Publication(s, b) {
cout << \"Call Book\'s constructor!\" << \"\\n\";
}
double getTotalPrice() {
return price * length;
}
~Book() {
cout << \"Call Book\'s de-constructor!\" << \"\\n\";
}
};
class Tape : public Publication{
public:
Tape(double s, int b) : Publication(s, b) {
cout << \"Call Tape\'s constructor!\" << \"\\n\";
}
double getTotalPrice() {
double count1 = 0;
if (length % 10 != 0) {
count1 += 1 + (length / 10);
}
else {
count1 = length / 10;
}
return count1 * 1.0 * price;
}
~Tape() {
cout << \"Call Tape\'s de-constructor!\" << \"\\n\";
}
};
class BookStore
{
private:
Publication** pubs;
int num;
public:
BookStore(Publication** p, int n)
{
pubs = new Publication * [n];
num = n;
for (int i = 0; i < n; i++)
{
if (typeid(*(p[i])) == typeid(Book))
{
pubs[i] = new Book(p[i]->getPrice(), p[i]->getLength());
}
else
{
pubs[i] = new Tape(p[i]->getPrice(), p[i]->getLength());
}
}
}
int getNumOfBook()
{
int c = 0;
for (int i = 0; i < num; i++)
{
if (typeid(*(pubs[i])) == typeid(Book))
c++;
}
return c;
}
int getNumOfTape()
{
int c = 0;
for (int i = 0; i < num; i++)
{
if (typeid(*(pubs[i])) == typeid(Tape))
c++;
}
return c;
}
~BookStore()
{
for (int i = 0; i < num; i++)
{
delete pubs[i];
}
delete[] pubs;
cout << \"Call BookStore\'s de-constructor!\\n\";
}
};
int main()
{
int cases, date;
char type;
double total, price;
Publication** pub;
cin >> cases;
pub = new Publication * [cases];
for (int i = 0; i < cases; i++)
{
cin >> type >> price >> date;
switch (type)
{
case \'B\':
pub[i] = new Book(price, date);
break;
case \'T\':
pub[i] = new Tape(price, date);
break;
}
}
BookStore bookStore(pub, cases);
cout << \"There are \" << bookStore.getNumOfBook() << \" books and \" << bookStore.getNumOfTape() << \" tapes.\";
total = 0;
for (int i = 0; i < cases; i++)
{
total += pub[i]->getTotalPrice();
}
cout << \" Their total price is \" << setprecision(2) << fixed << total << \".\" << endl;
for (int i = 0; i < cases; i++)
{
delete pub[i];
}
delete[] pub;
return 0;
}