山东科技大学2020年5月25日作业题解
题目一: 新型乘法运算
Description
定义Integer类,只有一个int类型的属性value。包括如下成员函数:
-
void setValue(int): 设置value为参数的值。
-
int getValue():获取value的值。
-
重载乘法运算。新的乘法定义为:对于数值n,如果乘数是m,那么将n重复m次形成一个新的数。如:34 * 3 = 343434。注意:34 * 1 = 34。
-
重载=运算符。
Input
输入有多行,第1行是Integer类的对象M的属性值。
之后的第2行N表示后面还有N行输入,每行输入是一个正整数,表示对M的乘数。
Output
N个新的Integer对象的值,每个是M的值乘以相应的乘数。假定所有的输出都不会溢出。
Sample Input
1
10
1
2
3
4
5
6
7
8
9
10
Sample Output
1
11
111
1111
11111
111111
1111111
11111111
111111111
1111111111
题目给定代码
int main()
{
Integer M, N;
int a, n, m;
cin>>a;
M.setValue(a);
cin>>n;
while (n--)
{
cin>>m;
N = M * m;
cout<<N.getValue()<<endl;
}
return 0;
}
标程
#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;
class Integer {
private:
int number, add;
public:
void setValue(int n){
number = n;
int num = 0, Number = number;
while (Number) {
Number /= 10, ++num;
}
add = 1;
for (R int i = 0; i < num; ++i) {
add *= 10;
}
}
int getValue() {
return number;
}
Integer operator * (int a) {
Integer n;
n.number = number, n.add = add;
for (R int i = 1; i < a; ++i) {
n.number *= n.add, n.number += number;
}
return n;
}
Integer& operator = (Integer n) {
number = n.number;
return *this;
}
};
int main()
{
Integer M, N;
int a, n, m;
cin >> a;
M.setValue(a);
cin >> n;
while (n--)
{
cin >> m;
N = M * m;
cout << N.getValue() << endl;
}
return 0;
}
题目二: 汽车家族(III)
Description
目前,汽车的能源主要有油类燃料动力(包括汽油、柴油)、电动力两种。现在定义四个类:
-
Car类,包括静态整型数据成员cntOfCars,用于记录产生的汽车对象的个数。一个double型属性,表示汽车的速度。定义其构造函数,其中输出信息见样例。定义setSpeed(double)方法用于设置汽车的速度。
-
OilCar类,是Car类的子类,包括静态整型数据成员cntOfOilCars,记录产生的燃油动力汽车对象的个数。一个double类型的属性,表示汽车百公里油耗。定义其构造函数,其中输出信息见样例。定义show()方法,用于输出如样例所示的信息。定义setOil(double)方法,用于设置汽车的油耗。
-
ElecCar类,是Car类的子类,包括静态整型数据成员cntOfElecCars,记录产生的电动力汽车对象的个数。一个double类型的属性,表示汽车百公里能耗(以KW为单位)。定义其构造和析构函数,其中输出信息见样例。定义show()方法,用于输出如样例所示的信息。定义setPower(double)方法,用于设置汽车的能耗。
-
CarHall类,用于展示上述两类汽车。所以它应该是OilCar和ElecCar类的组合类,包含1个OilCar类的对象指着,1个ElecCar类的对象指针。定义getInfo()方法,用于从标准输入中获取汽车的各种信息;定义showInfo()信息,按照样例的格式输出所有汽车的信息。
Input
输入有多行。
第1行M是一个正整数,表示之后有M辆燃油动力汽车的数据。之后的M行,每行包括2个double类型的值,分别是一辆燃油动力汽车的速度和油耗。
紧接着又是一个正整数N,表示之后有N辆电动力汽车的数据。之后的N行,每行包括2个double类型的值,分别是一辆燃油动力汽车的速度和能耗。
Output
见样例。
Sample Input
3
100 8.44
120 9.22
90.87 10.23
4
120.1 878
90.87 13.4
78.12 249.5
99.87 135.1
Sample Output
The 1th car is created.
The 2th car is created.
The 1th oil-car is created.
The 3th car is created.
The 1th electrical car is created.
The 4th car is created.
The 2th oil-car is created.
The 5th car is created.
The 3th oil-car is created.
The 6th car is created.
The 4th oil-car is created.
The 7th car is created.
The 2th electrical car is created.
The 8th car is created.
The 3th electrical car is created.
The 9th car is created.
The 4th electrical car is created.
The 10th car is created.
The 5th electrical car is created.
We have 3 oil-cars, which are:
An oil-car at speed of 100km/s, with 8.44L/100km.
An oil-car at speed of 120km/s, with 9.22L/100km.
An oil-car at speed of 90.87km/s, with 10.23L/100km.
We have 4 electrical cars, which are:
An electrical car at speed of 120.1km/s, with 878KW/100km.
An electrical car at speed of 90.87km/s, with 13.4KW/100km.
An electrical car at speed of 78.12km/s, with 249.5KW/100km.
An electrical car at speed of 99.87km/s, with 135.1KW/100km.
HINT
由于事先不知道有多少辆车,所以CarHall中的两种汽车成员必须是指针,然后在getInfo()中,在读取到相应的汽车辆数之后,再用new运算符申请空间。
题目给定代码
int main()
{
Car car;
OilCar oilcar;
ElecCar eleccar;
CarHall carhall;
carhall.getInfo();
carhall.showInfo();
return 0;
}
标程
#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;
class Car{
protected:
static int cntofcars;
double v;
public:
Car() {
cout << "The " << cntofcars++ << "th car is created." << endl;
}
void setSpeed(double v1) {
v = v1;
}
};
class OilCar :public Car{
private:
static int cntofcarss;
double o;
public:
OilCar() {
cout << "The " << cntofcarss++ << "th oil-car is created." << endl;
}
void show() {
cout << "An oil-car at speed of " << v << "km/s, with " << o << "L/100km." << endl;
}
void setOil(double oo) {
o = oo;
}
};
class ElecCar :public Car{
private:
static int cntofcarsss;
double e;
public:
ElecCar() {
cout << "The " << cntofcarsss++ << "th electrical car is created." << endl;
}
void show() {
cout << "An electrical car at speed of " << v << "km/s, with " << e << "KW/100km." << endl;
}
void setEle(double oo) {
e = oo;
}
};
class CarHall{
private:
OilCar* a;
ElecCar* b;
int h, l;
public:
~CarHall() {
delete[]a; delete[]b;
}
void getInfo(){
double y, yy, yyy, yyyy;
cin >> h;
a = new OilCar[h];
for (R int i = 0; i < h; ++i){
cin >> y >> yy;
a[i].setSpeed(y), a[i].setOil(yy);
}
cin >> l;
b = new ElecCar[l];
for (R int i = 0; i < l; ++ i){
cin >> yyy >> yyyy;
b[i].setSpeed(yyy), b[i].setEle(yyyy);
}
}
void showInfo(){
int i = 0, j = 0;
cout << "We have " << h << " oil-cars, which are:" << endl;
while (i != h) {
a[i++].show();
}
cout << "We have " << l << " electrical cars, which are:" << endl;
while (j != l) {
b[j++].show();
}
}
};
int Car :: cntofcars = 1;
int OilCar :: cntofcarss = 1;
int ElecCar :: cntofcarsss = 1;
int main() {
Car car;
OilCar oilcar;
ElecCar eleccar;
CarHall carhall;
carhall.getInfo();
carhall.showInfo();
return 0;
}
题目三: Lemon
Description
Jackie开了一家水果店,店里柑橘类(Citrus)水果销售情况特别好,刚刚进货了一些砂糖柑(Tangerine)、葡萄柚(Grapefruit)和柠檬(Lemon)。
他有兴趣研究了柑橘类水果的杂交过程(如下图所示):首先,橘子(Mandarin)、柚子(Pomelo)和香橼(Cirton)都是柑橘类(Citrus)的水果,这是第一层;其次,第二层的水果是橙子(Orange)和青柠(Lime),橙子是橘子和柚子杂交出来的,柚子和香橼杂交出了青柠;最后是第三层,橘柑(砂糖柑)是橘子和橙子杂交的产物,葡萄柚是橙子和柚子杂交的结果,橙子和青柠杂交产生了柠檬。
请你编写程序用派生树来描述柑橘类水果的杂交关系,用继承关系来表示柑橘类水果的杂交过程。
用C++编写Citrus类及其派生树上的各个类来来完成代码,调用格式见“Append Code”。
Citrus类及其派生树上的这些类的属性是名称(name)和重量(weight)、有show()函数用来显示一些信息。各类的构造根据给出的调用代码完成。
Input
输入为多行,每行是一种水果的名称和重量。
Output
按格式输出水果的信息,详细见输出样例。
Sample Input
Tangerine 150
Grapefruit 200
Lemon 1000
Sample Output
Tangerine 150kg, is citrus fruit.
Tangerine 150kg, is mandarin.
Tangerine 150kg, is orange.
Tangerine 150kg, is tangerine.
Grapefruit 200kg, is citrus fruit.
Grapefruit 200kg, is pomelo.
Grapefruit 200kg, is grapefruit.
Lemon 1000kg, is mandarin.
Lemon 1000kg, is pomelo.
Lemon 1000kg, is citron.
Lemon 1000kg, is orange.
Lemon 1000kg, is lime.
Lemon 1000kg, is lemon.
题目给定代码
int main()
{
Citrus *c;
Mandarin *ma;
Pomelo *po;
Citron *ci;
Orange *og;
Lime *li;
Tangerine *ta;
Grapefruit *gr;
Lemon *le;
string name;
double weight;
while(cin >> name >> weight)
{
if(name == "Tangerine")
{
Tangerine tangerine(name, weight);
c = ma = og = ta = &tangerine;
c->show();
ma->show();
og->show();
ta->show();
}
if(name == "Grapefruit")
{
Grapefruit grapefruit(name, weight);
c = po = gr = &grapefruit;
c->show();
po->show();
gr->show();
}
if(name == "Lemon")
{
Lemon lemon(name, weight);
ma = og = &lemon;
ci = li = &lemon;
po = le = &lemon;
ma->show();
po->show();
ci->show();
og->show();
li->show();
le->show();
}
}
}
标程
#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;
class Citrus {
protected:
string Name;
double weight;
public:
Citrus(string name_ = 0, double weight_ = 0) : Name(name_), weight(weight_) {
}
void show(){
cout << Name << " " << weight << "kg, is citrus fruit." << "\n";
}
};
class Mandarin : virtual public Citrus {
public:
Mandarin(string name_ = 0, double weight_ = 0) : Citrus(name_, weight_) {
}
void show(){
cout << Name << " " << weight << "kg, is mandarin." << "\n";
}
};
class Pomelo :virtual public Citrus {
public:
Pomelo(string name_ = 0, double weight_ = 0):Citrus(name_, weight_) {
}
void show(){
cout << Name << " " << weight << "kg, is pomelo." << "\n";
}
};
class Citron : virtual public Citrus {
public:
Citron(string name_ = 0, double weight_ = 0):Citrus(name_, weight_) {
}
void show(){
cout << Name << " " << weight << "kg, is citron." << "\n";
}
};
class Orange : virtual public Mandarin, virtual public Pomelo {
public:
Orange(string name_ = 0, double weight_ = 0) : Mandarin(name_, weight_), Pomelo(name_, weight_), Citrus(name_, weight_) {
}
void show(){
cout << Name << " " << weight << "kg, is orange." << "\n";
}
};
class Lime :virtual public Pomelo, virtual public Citron {
public:
Lime(string name_ = 0, double weight_ = 0)
:Pomelo(name_, weight_), Citron(name_, weight_), Citrus(name_, weight_) {}
void show()
{
cout << Name << " " << weight << "kg, is lime." << endl;
}
};
class Tangerine :virtual public Mandarin, virtual public Orange {
public:
Tangerine(string name_ = 0, double weight_ = 0)
:Mandarin(name_, weight_), Orange(name_, weight_), Pomelo(name_, weight_), Citrus(name_, weight_) {}
void show()
{
cout << Name << " " << weight << "kg, is tangerine." << endl;
}
};
class Grapefruit :virtual public Pomelo, virtual public Orange {
public:
Grapefruit(string name_ = 0, double weight_ = 0)
:Orange(name_, weight_), Pomelo(name_, weight_), Mandarin(name_, weight_), Citrus(name_, weight_) {}
void show()
{
cout << Name << " " << weight << "kg, is grapefruit." << endl;
}
};
class Lemon :virtual public Orange, virtual public Lime {
public:
Lemon(string name_ = 0, double weight_ = 0)
:Orange(name_, weight_), Lime(name_, weight_), Pomelo(name_, weight_), Mandarin(name_, weight_), Citron(name_, weight_), Citrus(name_, weight_) {}
void show()
{
cout << Name << " " << weight << "kg, is lemon." << endl;
}
};
int main()
{
Citrus *c;
Mandarin *ma;
Pomelo *po;
Citron *ci;
Orange *og;
Lime *li;
Tangerine *ta;
Grapefruit *gr;
Lemon *le;
string name;
double weight;
while(cin >> name >> weight)
{
if(name == "Tangerine")
{
Tangerine tangerine(name, weight);
c = ma = og = ta = &tangerine;
c->show();
ma->show();
og->show();
ta->show();
}
if(name == "Grapefruit")
{
Grapefruit grapefruit(name, weight);
c = po = gr = &grapefruit;
c->show();
po->show();
gr->show();
}
if(name == "Lemon")
{
Lemon lemon(name, weight);
ma = og = &lemon;
ci = li = &lemon;
po = le = &lemon;
ma->show();
po->show();
ci->show();
og->show();
li->show();
le->show();
}
}
}
题目四: 分数类的输出
Description
封装一个分数类Fract,用来处理分数功能和运算,支持以下操作:
- 构造:传入两个参数n和m,表示n/m;分数在构造时立即转化成最简分数。
- show()函数:分数输出为“a/b”或“-a/b”的形式,a、b都是无符号整数。若a为0或b为1,只输出符号和分子,不输出“/”和分母。
你设计一个Fract类,使得main()函数能够运行并得到正确的输出。调用格式见append.cc
Input
输入多行,每行两个整数,分别为分子和分母,至EOF结束。输入的分母不会为0;
Output
每行输出一个分数,与输入顺序一致。
分数输出时为最简形式,负号只会出现在最前面,若分母为1或分子为0,则只输出一个整数,即分子部分,而没有“/”和分母部分。
Sample Input
1 3
20 -15
80 150
-9 1
6 6
12 16
-33 -48
6 11
0 -10
Sample Output
1/3
-4/3
8/15
-9
1
3/4
11/16
6/11
0
题目给定代码
#include <cstdio>
int main()
{
int n, m;
while(cin >> n >> m)
{
Fract fr(n, m);
fr.show();
}
}
标程
#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;
inline int abs(int number) {
if (number < 0) {
return -number;
}
return number;
}
class Fract {
private:
int n, m;
char ch;
public:
Fract(int a, int b) {
n = a, m = b;
ch = NULL;
if (a * b < 0) {
ch = '-';
}
while (a) {
int temp = b % a;
b = a, a = temp;
}
n = abs(n / b), m = abs(m / b);
}
void show() {
if (ch == '-') {
cout << ch;
}
if (n == 0 || m == 1) {
cout << n << "\n";
}
else {
cout << n << "/" << m << "\n";
}
}
};
int main()
{
int n, m;
while (cin >> n >> m)
{
Fract fr(n, m);
fr.show();
}
}
题目五: 分数类的类型转换
Description
封装一个分数类Fract,用来处理分数功能和运算,支持以下操作:
- 构造:传入两个参数n和m,表示n/m;分数在构造时立即转化成最简分数。
- show()函数:分数输出为“a/b”或“-a/b”的形式,a、b都是无符号整数。若a为0或b为1,只输出符号和分子,不输出“/”和分母。
- double类型转换函数:用分子除以分母,得到的小数。注意:分子为0时不要输出为“-0”
你设计一个Fract类,使得main()函数能够运行并得到正确的输出。调用格式见append.cc
Input
输入多行,每行两个整数,分别为分子和分母,至EOF结束。输入的分母不会为0;
Output
每行输出一个实数和分数,与输入顺序一致。实数为分子除以分母所得。
分数输出时为最简形式,负号只会出现在最前面,若分母为1或分子为0,则只输出一个整数,即分子部分,而没有“/”和分母部分。
Sample Input
1 3
20 -15
80 150
-9 1
6 6
12 16
-33 -48
6 11
0 -10
Sample Output
0.333333 1/3
-1.33333 -4/3
0.533333 8/15
-9 -9
1 1
0.75 3/4
0.6875 11/16
0.545455 6/11
0 0
题目给定代码
int main()
{
int n, m;
while(cin >> n >> m)
{
Fract fr(n, m);
cout << (double)fr << " ";
fr.show();
}
}
标程
#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;
inline int abs(int number) {
if (number < 0) {
return -number;
}
return number;
}
class Fract {
private:
int n, m;
char ch;
public:
Fract(int a, int b) {
n = a, m = b;
ch = NULL;
if (a * b < 0) {
ch = '-';
}
while (a) {
int temp = b % a;
b = a, a = temp;
}
n = abs(n / b), m = abs(m / b);
}
void show() {
if (ch == '-') {
cout << ch;
}
if (n == 0 || m == 1) {
cout << n << "\n";
}
else {
cout << n << "/" << m << "\n";
}
}
operator double() {
if (ch != '-') {
return (double)n / (double)m;
}
else {
return -(double)n / (double)m;
}
}
};
int main()
{
int n, m;
while (cin >> n >> m)
{
Fract fr(n, m);
cout << (double)fr << " ";
fr.show();
}
}
题目六: 分数类的乘法
Description
封装一个分数类Fract,用来处理分数功能和运算,支持以下操作:
构造:传入两个参数n和m,表示n/m;分数在构造时立即转化成最简分数。
show()函数:分数输出为“a/b”或“-a/b”的形式,a、b都是无符号整数。若a为0或b为1,只输出符号和分子,不输出“/”和分母。
在分数类上重载乘法运算符,进行分数的乘法运算
你设计一个Fract类,使得main()函数能够运行并得到正确的输出。调用格式见append.cc
Input
输入多行,每行四个整数n、m、q、p,分别为两个分数n/m和q/p,至EOF结束。输入的分母不会为0;
Output
每行输出一个分数,为n/m和q/p的乘积,与输入顺序一致。
分数输出时为最简形式,负号只会出现在最前面,若分母为1或分子为0,则只输出一个整数,即分子部分,而没有“/”和分母部分。
Sample Input
1 3 2 3
20 -15 150 80
0 77 -9 1
6 6 4 4
12 16 4 3
-33 -48 6 11
0 -10 360 12
Sample Output
2/9
-5/2
0
1
1
3/8
0
题目给定代码
int main()
{
int n, m, p, q;
while (cin >> n >> m >> q >> p)
{
Fract f1(n, m), f2(q, p);
Fract fr = f1 * f2;
fr.show();
}
}
标程
#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;
inline int abs(int number) {
if (number < 0) {
return -number;
}
return number;
}
class Fract {
private:
int n, m;
char ch;
public:
Fract(int a, int b) {
n = a, m = b;
ch = '+';
if (a * b < 0) {
ch = '-';
}
while (a) {
int temp = b % a;
b = a, a = temp;
}
n = abs(n / b), m = abs(m / b);
}
void show() {
if (ch == '-') {
cout << ch;
}
if (n == 0 || m == 1) {
cout << n << "\n";
}
else {
cout << n << "/" << m << "\n";
}
}
Fract(int a, int b, char c) {
n = a, m = b, ch = c;
if (n == 0) {
ch = '+';
}
if (a * b < 0) {
ch = '-';
}
while (a) {
int temp = b % a;
b = a, a = temp;
}
n = n / b, m = m / b;
}
Fract friend operator * (Fract a, Fract b) {
int x = a.n * b.n, y = a.m * b.m;
if (a.ch == b.ch) {
return Fract(x, y, '+');
}
else {
return Fract(x, y, '-');
}
return Fract(x, y);
}
};
int main()
{
int n, m, p, q;
while (cin >> n >> m >> q >> p)
{
Fract f1(n, m), f2(q, p);
Fract fr = f1 * f2;
fr.show();
}
}