山东科技大学2020年6月8日作业题解

山东科技大学2020年6月8日作业题解

题目一: 动物爱好者

Description

某人是一个狂热的动物爱好者,豢养了大量的各种动物。现在请定义两个类:

  1. Animal类:

(1)string name和int num属性表示该种动物的名称和数量。

(2)无参构造函数。

(3)void setAnimal(string,int)方法,用于设置一个动物的相关属性。

(4)int getNum() const和string getName() const方法用于获得该动物的数量和名称。

(5)重载的赋值运算符=。

  1. AnimalList类:

(1)Animal *animalList和int numOfAnimal属性,用于表示该人豢养的所有动物的列表以及动物的种类数。

(2)构造函数AnimalList(Animal *animals, int n)。

(3)重载的下标运算符[],int operator[](string name),用于返回参数name指定名称的动物的数量,当不存在这种动物时,返回-1。

Input

第一行M>0表示有M种动物,之后有M行,每行第一个字符串表示动物名称,第二个整数是该种动物的数量。

之后一个N>0表示有N个测试用的动物名称,之后又有N行,每行是一个动物名。

Output

输出共N行,格式见样例。

Sample Input

5
Dog 5
Bird 10
Cat 11
Duck 1
Sparrow 66
6
Dog
Bird
Cat
Duck
Sparrow
Bull

Sample Output

There are 5 Dogs.
There are 10 Birds.
There are 11 Cats.
There are 1 Ducks.
There are 66 Sparrows.
There is none Bull.

HINT

注意:不能使用STL。

题目给定代码

int main()
{
    int cases;
    string name;
    int num;
    cin>>cases;
    Animal animals[cases];
    for (int i = 0; i < cases; i++)
    {
        cin>>name>>num;
        animals[i].setAnimal(name, num);
    }
    AnimalList animalList(animals, cases);
 
    cin>>cases;
    for (int i = 0; i < cases; i++)
    {
        cin>>name;
        if (animalList[name] != -1)
            cout<<"There are "<<animalList[name]<<" "<<name<<"s."<<endl;
        else
            cout<<"There is none "<<name<<"."<<endl;
    }
    return 0;
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<typeinfo>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;

class Animal {
public:
    string name;
    int number;
public:
    Animal() {
        number = 0;
    }
    void setAnimal(string s, int num) {
        name = s, number = num;
    }
    int getNum() {
        return number;
    }
    string getName() {
        return name;
    }
    Animal operator = (Animal animal) {
        this->name = animal.name, this->number = animal.number;
        return *this;
    }
};
class AnimalList {
private:
    Animal* animalList;
    int numOfAnimal;
public:
    AnimalList(Animal* animals, int n) {
        numOfAnimal = n;
        animalList = new Animal[numOfAnimal];
        for (R int i = 0; i < n; ++i) {
            animalList[i] = animals[i];
        }
    }
    int operator [] (string name) {
        for (R int i = 0; i < numOfAnimal; ++i) {
            if (animalList[i].name == name) {
                return animalList[i].number;
            }
        }
        return -1;
    }
    ~AnimalList() {
        delete[]animalList;
    }
};

int main()
{
    int Cases;
    string name;
    int num;
    cin >> Cases;
    Animal animals[Cases];
    for (int i = 0; i < Cases; i++)
    {
        cin >> name >> num;
        animals[i].setAnimal(name, num);
    }
    AnimalList animalList(animals, Cases);

    cin >> Cases;
    for (int i = 0; i < Cases; i++)
    {
        cin >> name;
        if (animalList[name] != -1)
            cout << "There are " << animalList[name] << " " << name << "s." << endl;
        else
            cout << "There is none " << name << "." << endl;
    }
    return 0;
}

题目二: 向量的运算

Description

编写类Vector,用于表示一个向量。显然:

  1. 它有三个数据成员,假定均为int类型的量,表示3个方向上的分量。

  2. 定义其无参构造函数,初始化三个分量为0。

3.定义带参构造函数,用于初始化向量。

  1. 重载+、-、、<<和>>运算符。其中“+”和“-”不改变操作数的值,只是返回运算结果;“”实现两种乘法,包括数乘及叉积。

Input

输入有多行,第一行M>0,表示有M个测试用例,之后有M行。

每行包括7个部分:前3个表示一个向量的三个分量,中间3个表示另一个向量的三个分量,最后一个为一个int类型的数。

分量的顺序按照i、j、k给出。

Output

输出见样例。其中:如果某个分量为0,则不输出该数量,除非所有分量均为0,则输出0。如果某个分量为负数,则不应输出其前面的“+”号。

Sample Input

7
1 1 1 1 1 1 3
1 2 3 3 2 1 4
-1 -2 -3 -3 -2 -1 10
1 2 3 -1 2 3 5
1 2 3 1 -2 3 6
1 2 3 1 2 -3 9
1 2 3 -1 -2 -3 5

Sample Output

vect+vec2 nvec1 vec1vec2
2i+2j+2k 3i+3j+3k 0
4i+4j+4k 4i+8j+12k -4i+8j-4k
-4i-4j-4k -10i-20j-30k -4i+8j-4k
4j+6k 5i+10j+15k -6j+4k
2i+6k 6i+12j+18k 12i-4k
2i+4j 9i+18j+27k -12i+6j
0 5i+10j+15k 0

题目给定代码

int main()
{
    Vector vec1, vec2, vec3;
    int cases, n;
    cin>>cases;
    cout<<"vect+vec2\tn*vec1\tvec1*vec2\n";
    for (int i = 0; i < cases; i++)
    {
        cin>>vec1>>vec2>>n;
        vec3 = vec1 + vec2;
        cout<<vec3<<"\t";
        vec3 = vec1 * n;
        cout<<vec3<<"\t";
        vec3 = vec1 * vec2;
        cout<<vec3<<endl;
    }
    return 0;
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<typeinfo>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;

class Vector {
public:
    int x, y, z;
    Vector(int x_x, int y_y, int z_z) {
        x = x_x, y = y_y, z = z_z;
    }
    Vector() {
        x = 0, y = 0, z = 0;
    }
    friend Vector operator + (const Vector& v1, const Vector& v2) {
        return Vector(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
    }
    friend Vector operator - (const Vector& v1, const Vector& v2) {
        return Vector(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
    }
    Vector operator * (const Vector& v) {
        return Vector(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
    }
    Vector operator * (double f) {
        return Vector(x * f, y * f, z * f);
    }
    friend istream& operator >> (istream& input, Vector& v) {
        input >> v.x >> v.y >> v.z;
        return input;
    }
    friend ostream& operator << (ostream& output, Vector& v) {
        if (v.x && v.y < 0 && v.z > 0) {
            output << v.x << "i" << v.y << "j+" << v.z << "k";
        }   
        else if (v.y > 0 && v.z < 0) {
            output << v.x << "i+" << v.y << "j" << v.z << "k";
        }
        else if (v.y < 0 && v.z < 0) {
            output << v.x << "i" << v.y << "j" << v.z << "k";
        }
        else if (v.y == 0 && v.x && v.z < 0) {
            output << v.x << "i" << v.z << "k";
        }
        else if (v.x && v.y && v.z) {
            output << v.x << "i+" << v.y << "j+" << v.z << "k";
        }           
        else if (!v.x && v.y && v.z) {
            output << v.y << "j+" << v.z << "k";
        }           
        else if (v.x && !v.y && v.z) {
            output << v.x << "i+" << v.z << "k";
        }           
        else if (v.x && v.y && !v.z) {
            output << v.x << "i+" << v.y << "j";
        }           
        else if (!v.x && !v.y && v.z) {
            output << v.z << "k";
        }           
        else if (!v.x && v.y && !v.z) {
            output << v.y << "j";
        }          
        else if (v.x && !v.y && !v.z) {
            output << v.x << "i";
        }    
        else {
            output << "0";
        }
        return output;
    }
};

int main()
{
    Vector vec1, vec2, vec3;
    int cases, n;
    cin >> cases;
    cout << "vect+vec2\tn*vec1\tvec1*vec2\n";
    for (int i = 0; i < cases; i++)
    {
        cin >> vec1 >> vec2 >> n;
        vec3 = vec1 + vec2;
        cout << vec3 << "\t";
        vec3 = vec1 * n;
        cout << vec3 << "\t";
        vec3 = vec1 * vec2;
        cout << vec3 << endl;
    }
    return 0;
}

题目三: 老师的工资

Description

假设高中老师和大学老师的工资分别是这么计算的:

1.高中老师的工资是基本工资+奖金,其中奖金是升学的学生数乘以100。

2.大学老师的工资是基本工资+绩效。其中当教学的小时数不到240小时时,每少1个小时,扣20元;当小时数多于240小时时,每多1个小时,多发40元。

如:王老师是一个高中老师,他的基本工资是1000元,有5个学生成功升学,则其工资是1000+5*100=1500元。

刘老师是一个大学老师,基本工资是2000元,如果只完成了200小时的授课,则工资是2000-4020=1200元;如果完成了250小时的授课,则工资是2000+1040=2400元。

现在,定义Teacher、HighSchoolTeacher、UniversityTeacher的构造函数、析构函数、getSalary方法,使得main函数能正确执行并获得样例所示的结果。

Input

输入有多行。

第1行N>0,表示后面有N个测试用例。

每个测试占1行,包括1个字符、1个double数和1个int数,均为正数。

*Output

见样例。

Sample Input

4
h 2900.13 20
u 3911.23 210
u 3911.34 250
h 3911.15 10

Sample Output

Teacher’s constructor.
HighSchoolTeacher’s constructor.
4900.13
HighSchoolTeacher’s deconstructor.
Teacher’s deconstructor.
Teacher’s constructor.
UniversityTeacher’s constructor.
3311.23
UniversityTeacher’s deconstructor.
Teacher’s deconstructor.
Teacher’s constructor.
UniversityTeacher’s constructor.
4311.34
UniversityTeacher’s deconstructor.
Teacher’s deconstructor.
Teacher’s constructor.
HighSchoolTeacher’s constructor.
4911.15
HighSchoolTeacher’s deconstructor.
Teacher’s deconstructor.

题目给定代码

int main()
{
    int N, i, m;
    char ch;
    double basis;
    Teacher *t;
    cin>>N;
    for (i = 0; i < N; i++)
    {
        cin>>ch>>basis>>m;
        if (ch == 'h')
            t = new HighSchoolTeacher(basis, m);
        else if (ch == 'u')
            t  = new UniversityTeacher(basis, m);
        cout<<setprecision(2)<<setiosflags(ios::fixed)<<t->getSalary()<<endl;
        delete t;
    }
    return 0;
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<typeinfo>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;

class Teacher {
protected:
    double base;
    int hour;
public:
    Teacher() {
        cout << "Teacher's constructor.\n";
    }
    virtual ~Teacher() {
        cout << "Teacher's deconstructor.\n";
    }
    virtual double getSalary() = 0;
};
class HighSchoolTeacher : public Teacher{
public:
    double getSalary() {
        return base + hour * 100;
    }
    HighSchoolTeacher(double b, int h) {
        base = b, hour = h;
        cout << "HighSchoolTeacher's constructor.\n";
    }
    ~HighSchoolTeacher() {
        cout << "HighSchoolTeacher's deconstructor.\n";
    }
};
class UniversityTeacher : public Teacher{
public:
    double getSalary() {
        if (hour <= 240) {
            return base - (240 - hour) * 20;
        }
        return base + (hour - 240) * 40;
    }
    UniversityTeacher(double b, int h) {
        base = b, hour = h;
        cout << "UniversityTeacher's constructor.\n";
    }
    ~UniversityTeacher() {
        cout << "UniversityTeacher's deconstructor.\n";
    }
};

int main()
{
    int N, i, m;
    char ch;
    double basis;
    Teacher* t;
    cin >> N;
    for (i = 0; i < N; i++)
    {
        cin >> ch >> basis >> m;
        if (ch == 'h')
            t = new HighSchoolTeacher(basis, m);
        else if (ch == 'u')
            t = new UniversityTeacher(basis, m);
        cout << setprecision(2) << setiosflags(ios::fixed) << t->getSalary() << endl;
        delete t;
    }
    return 0;
}

第四题: 不同交通工具的速度

Description

不同交通工具的速度是不同的。针对自行车、摩托车和汽车分别建立类,来模拟这一情况。

定义Vechicle类,是所有交通工具的父类:

  1. 属性int speed表示交通工具的一般速度。

  2. 静态数据成员int numOfVechicles,表示创建的交通工具的数量。这个值只增不减。

  3. 静态成员函数int getNumOfVechicles(),用于获取交通工具的数量。

  4. 析构函数。输出“A vechicle is deleted.”

  5. 纯虚函数void show().

定义Bike、MotoBike和Car三个类,它们都是Vechicle的子类,且具有:

  1. 构造函数。

  2. 重写show()函数,输出“A 's speed is ? km/h.”,其中“”是bike、motobike或car,根据所在类不同而不同。“?”是speed属性的值。

  3. 析构函数。输出“A * is deleted.”,“*”的含义同上。

定义Person类,表示司机:

  1. 数据成员string name,是人的姓名。

  2. void drive(Vechicle&)方法,输出“$ drives”, 并调用Vechicle类的show()方法。其中“$”是name的值。

Input

第1行N>0,表示有N个测试用例。

每个测试用例包括一个不含空白符的字符串、一个字符和一个整数。

Output

见样例及题目描述。

Sample Input

3
Tom B 15
Jack M 50
Mary C 100

Sample Output

In beginning, we have 0 vechicles.
Tom drives. A bike’s speed is 15km/h.
A bike is deleted.
A vechicle is deleted.
Jack drives. A motobike’s speed is 50km/h.
A motobike is deleted.
A vechicle is deleted.
Mary drives. A car’s speed is 100km/h.
A car is deleted.
A vechicle is deleted.
At the end, we have 3 vechicles.

题目给定代码

int main()
{
    int cases, n;
    char c;
    string name;
    Vechicle* vechicle;
    cout<<"In beginning, we have "<<Vechicle::getNumOfVechicles()<<" vechicles."<<endl;
    cin>>cases;
    for (int i = 0; i < cases; i++)
    {
        cin>>name>>c>>n;
        Person person(name);
        switch (c)
        {
        case 'B':
            vechicle = new Bike(n);
            break;
        case 'M':
            vechicle = new MotoBike(n);
            break;
        case 'C':
            vechicle = new Car(n);
            break;
        }
        person.drive(*vechicle);
        delete vechicle;
    }
    cout<<"At the end, we have "<<Vechicle::getNumOfVechicles()<<" vechicles."<<endl;
    return 0;
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<typeinfo>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;


class Vechicle {
protected:
    int speed;
    static int numOfVechicles;
public:
    Vechicle(int x = 0) : speed(x) {
        ++numOfVechicles;
    }
    virtual ~Vechicle() {
        cout << "A vechicle is deleted." << endl;
    }
    virtual void show() = 0;
    static int getNumOfVechicles() { 
        return numOfVechicles; 
    }
};

class Bike : public Vechicle {
public:
    Bike(int s) : Vechicle(s) {
    
    }
    ~Bike() {
        cout << "A bike is deleted." << "\n";
    }
    void show() {
        cout << "A bike's speed is " << speed << "km/h." << "\n";
    }
};
class MotoBike : public Vechicle {
public:
    MotoBike(int s) : Vechicle(s) {
    
    }
    ~MotoBike() {
        cout << "A motobike is deleted." << "\n";
    }
    void show() {
        cout << "A motobike's speed is " << speed << "km/h." << "\n";
    }
};
class Car : public Vechicle {
public:
    Car(int s) : Vechicle(s) {}
    ~Car() {
        cout << "A car is deleted." << "\n";
    }
    void show() {
        cout << "A car's speed is " << speed << "km/h." << "\n";
    }
};
class Person {
private:
    string name;
public:
    Person(string s) : name(s) {
    
    }
    void drive(Vechicle& v) {
        cout << name << " drives. ";
        v.show();
    }
};
int Vechicle::numOfVechicles = 0;

int main()
{
    int cases, n;
    char c;
    string name;
    Vechicle* vechicle;
    cout << "In beginning, we have " << Vechicle::getNumOfVechicles() << " vechicles." << "\n";
    cin >> cases;
    for (int i = 0; i < cases; i++)
    {
        cin >> name >> c >> n;
        Person person(name);
        switch (c)
        {
        case 'B':
            vechicle = new Bike(n);
            break;
        case 'M':
            vechicle = new MotoBike(n);
            break;
        case 'C':
            vechicle = new Car(n);
            break;
        }
        person.drive(*vechicle);
        delete vechicle;
    }
    cout << "At the end, we have " << Vechicle::getNumOfVechicles() << " vechicles." << endl;
    return 0;
}

**题目五:**水果店

Description

小明经营着一个不大的水果店(似曾相识哦~),只销售苹果、香蕉和桔子。为了促销,小明制定了如下定价策略:

  1. 苹果:按斤论价,每斤P元,买W斤,则需支付W*P元。

  2. 香蕉:半价,每斤P元,买W斤,则需支付W/2*P元。

3.桔子:按斤论价,每斤P元,买W斤。如果W>10,则打半价,即需支付WP/2元;否则如果W>5,则打八折,即需支付WP0.8元;其他情况不打折,即需支付WP元。

请用C++来计算某个顾客采购的水果的总价。该程序至少应有:

  1. Fruit类:是个抽象类,是Apple、Banana和Orange的父类。支持重载的加法运算。

  2. Apple、Banana和Orange类:分别对应于苹果、香蕉和桔子三种水果,每种水果执行不同的定价策略。

Input

输入为多行,每行格式为:

C W P

其中C是水果类型(a、b、o分别代表苹果、香蕉和桔子),W和P分别是顾客购买的相应水果的重量和每斤水果的单价。

Output

输出顾客需支付的总金额。

Sample Input

a 1 1
b 1 1
o 1 1

Sample Output

2.5

HINT

注意包含vector容器的头文件。

需要用多态来实现。

题目给定代码

int main()
{
    vector<Fruit*> fruits;
    vector<Fruit*>::iterator itr;
    char type;
    double weight, price, totalPrice;
    while (cin>>type>>weight>>price)
    {
        switch(type)
        {
        case 'a':
            fruits.push_back(new Apple(weight, price));
            break;
        case 'b':
            fruits.push_back(new Banana(weight,price));
            break;
        case 'o':
            fruits.push_back(new Orange(weight, price));
            break;
        }
    }
    totalPrice = 0;
    for (itr = fruits.begin(); itr != fruits.end(); itr++)
    {
        totalPrice = **itr + totalPrice;
    }
    cout<<totalPrice<<endl;
    return 0;
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<typeinfo>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;

class Fruit{
public:
	double w;
	double p;
	Fruit(double w, double p) :w(w), p(p) {
	
	}
	virtual double total() = 0;
	friend double operator +(Fruit& a, double b){
		return a.total() + b;
	}
};
class Apple :public Fruit{
public:
	Apple(double w, double p) :Fruit(w, p) {
	
	}
	double total(){
		return p * w;
	}
};
class Banana :public Fruit
{
public:
	Banana(double w, double p) :Fruit(w, p) {
	
	}
	double total(){
		return p * w * 0.5;
	}
};
class Orange :public Fruit
{
public:
	Orange(double w, double p) :Fruit(w, p) {
	
	}
	double total(){
		if (w > 10){
			return p * w * 0.5;
		}
		if (w > 5) {
			return p * w * 0.8;
		}
		else {
			return p * w;
		}
	}
};

int main()
{
    vector<Fruit*> fruits;
    vector<Fruit*>::iterator itr;
    char type;
    double weight, price, totalPrice;
    while (cin >> type >> weight >> price)
    {
        switch (type)
        {
        case 'a':
            fruits.push_back(new Apple(weight, price));
            break;
        case 'b':
            fruits.push_back(new Banana(weight, price));
            break;
        case 'o':
            fruits.push_back(new Orange(weight, price));
            break;
        }
    }
    totalPrice = 0;
    for (itr = fruits.begin(); itr != fruits.end(); itr++)
    {
        totalPrice = **itr + totalPrice;
    }
    cout << totalPrice << endl;
    return 0;
}

题目六: 图形计数与求面积

Description

定义三个类:Shape、Circle和Square,其中Shape为抽象类,包括:

  1. 用于记录Shape类及其子类对象(即图形)个数的静态数据成员。

  2. 构造函数与析构函数。

  3. 获得图形个数的静态成员函数 static int getNumOfShapes(),以及

  4. 求图形面积的纯虚函数getArea()。

类Circle是Shape类的子类,包括:

  1. 用于记录Circle类对象(即圆)个数的静态数据成员。

  2. 表示半径的double类型数据成员。

  3. 构造函数和析构函数。

  4. 重写的基类函数getArea(),用于求圆的面积,其中圆周率取值为3.14。

  5. 用于获得圆个数的静态成员函数static int getNumOfCircles()。

类Square也是Shape类的子类,包括:

  1. 用于记录Square类对象(即正方形)个数的静态数据成员。

  2. 表示边长的double类型数据成员。

  3. 构造函数和析构函数。

  4. 重写的基类函数getArea(),用于求正方形的面积。

  5. 用于获得正方形个数的静态成员函数static int getNumOfSquares()。

注意:所有用于记录个数的静态成员只增不减。

Input

第1行N>0,表示有N个测试用例。

每个测试用例分2部分:第1部分是1个字符C或者S,表示产生一个圆或者正方形;第2部分是一个实数,是圆的半径或正方形的边长。

Output

见样例。

其中面积输出2位小数。

Sample Input

2
C 1.1
S 2.34

Sample Output

numOfShapes = 0, numOfCircles = 0, numOfSquares = 0
A shape is created!
A circle is created!
Area = 3.80
A circle is erased!
A shape is erased!
A shape is created!
A square is created!
Area = 5.48
A square is erased!
A shape is erased!
numOfShapes = 2, numOfCircles = 1, numOfSquares = 1

题目给定代码

int main()
{
    int cases;
    char type;
    double data;
    Shape *shape;
    cin>>cases;
    cout<<"numOfShapes = "<<Shape::getNumOfShapes();
    cout<<", numOfCircles = "<<Circle::getNumOfCircles();
    cout<<", numOfSquares = "<<Square::getNumOfSquares()<<endl;
    for (int i = 0; i < cases; i++)
    {
        cin>>type>>data;
        switch(type)
        {
        case 'C':
            shape = new Circle(data);
            break;
        case 'S':
            shape = new Square(data);
            break;
        }
        cout<<"Area = "<<setprecision(2)<<fixed<<shape->getArea()<<endl;
        delete shape;
    }
    cout<<"numOfShapes = "<<Shape::getNumOfShapes();
    cout<<", numOfCircles = "<<Circle::getNumOfCircles();
    cout<<", numOfSquares = "<<Square::getNumOfSquares()<<endl;
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<typeinfo>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;

class Shape{
public:
    static int num;
    Shape() { 
        ++num; 
        cout << "A shape is created!" << "\n"; 
    }
    static int getNumOfShapes() { 
        return num; 
    }
    virtual double getArea() = 0;
    virtual ~Shape() { 
        cout << "A shape is erased!" << "\n"; 
    }
};
class Circle : public Shape{
public:
    double r;
    static int num1;
    Circle(double r = 0) :r(r) { 
        ++num1; 
        cout << "A circle is created!" << "\n"; 
    }
    double getArea() { 
        return 3.14 * r * r; 
    }
    static int getNumOfCircles() { 
        return num1; 
    }
    virtual ~Circle() { 
        cout << "A circle is erased!" << endl; 
    }
};
class Square :public Shape{
public:
    double s;
    static int num2;
    Square(double s = 0) :s(s) { 
        ++num2; cout << "A square is created!" << "\n"; 
    }
    double getArea() { 
        return s * s; 
    }
    static int getNumOfSquares() { 
        return num2; 
    }
    virtual ~Square() { 
        cout << "A square is erased!" << endl; 
    }
};
int Shape :: num = 0;
int Circle :: num1 = 0;
int Square :: num2 = 0;

int main()
{
    int cases;
    char type;
    double data;
    Shape* shape;
    cin >> cases;
    cout << "numOfShapes = " << Shape::getNumOfShapes();
    cout << ", numOfCircles = " << Circle::getNumOfCircles();
    cout << ", numOfSquares = " << Square::getNumOfSquares() << endl;
    for (int i = 0; i < cases; i++)
    {
        cin >> type >> data;
        switch (type)
        {
        case 'C':
            shape = new Circle(data);
            break;
        case 'S':
            shape = new Square(data);
            break;
        }
        cout << "Area = " << setprecision(2) << fixed << shape->getArea() << endl;
        delete shape;
    }
    cout << "numOfShapes = " << Shape::getNumOfShapes();
    cout << ", numOfCircles = " << Circle::getNumOfCircles();
    cout << ", numOfSquares = " << Square::getNumOfSquares() << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值