EX 10

你人生的起点并不是那么重要,重要的是你最终抵达到哪里

                           —— 巴菲特

Problem A: 让动物们叫起来吧!

题面:

main函数

int main()
{
    int cases;
    string name;
    char type;
    Animal *animal;
    cin>>cases;
    for (int i = 0; i < cases; i++)
    {
       cin>>name>>type;
       switch(type)
       {
        case 'A':
            animal = new Duck(name);
            break;
        case 'B':
            animal = new Turkey(name);
            break;
        case 'C':
            animal = new Cock(name);
            break;
       }
       animal->sound();
    }
    return 0;
}

考点:虚基类,纯虚函数

AC代码:

#include <bits/stdc++.h>
using namespace std;
class Animal
{
    friend class Cock;
    friend class Turkey;
    friend class Duck;
    string name;
public:
    Animal(string _name):name(_name){}
    virtual void sound()=0;
};
class Cock :public Animal
{
public:
    Cock(string name):Animal(name){}
    void sound()
    {
        cout << name ;
        cout << " is a cock, and it can crow." << endl;
    }
};
class Turkey :public Animal
{
public:
    Turkey(string name):Animal(name){}
    void sound()
    {
        cout << name ;
        cout << " is a turkey, and it can gobble." << endl;
    }
};
class Duck :public Animal
{
public:
    Duck(string name):Animal(name){}
    void sound()
    {
        cout << name ;
        cout << " is a duck, and it can quack." << endl;
    }
};

Problem B: 点之间的距离

题面:

main函数:

int main()
{
    Point p, p1(10, 10), p2(20, 20);
    PointArray pArr;
    double x, y;
    while(cin>>x>>y)
    {
        p.set(x, y);
        pArr.append(p);
    }
    cout<<p2 - p1<<endl;
    cout<<pArr.getMaxDis()<<endl;
    return 0;
}

考点:类的组合,vector的使用

AC代码:

#include <bits/stdc++.h>
using namespace std;
class Point
{
    double x,y;
public:
    Point():x(0),y(0){}
    Point(double _x,double _y):x(_x),y(_y){}
    double operator -(Point &b)
    {
        return sqrt((x-b.x)*(x-b.x)+(y-b.y)*(y-b.y));
    }
    void set(double x1,double y1)
    {
        x  =x1;
        y = y1;
    }
};
class PointArray
{
    vector<Point> vec;
public:
    void append(const Point&b)
    {
        vec.push_back(b);
    }
    double getMaxDis()
    {
        double d = -1;
        int len = vec.size();
        for(int i = 0;i<len;i++)
        {
            for(int j = i+1;j<len;j++)
            {
                if(vec[i]-vec[j]>d)
                    d = vec[i]-vec[j];
            }
        }
        return d;
    }
};

Problem C: 求图形的面积和体积

题面:

main函数:

int main()
{
    Graphic *gp[100];
    int cases;
    cin>>cases;
    for(int i = 0; i < cases; ++i)
    {
        string types;
        double value;
        cin>>types>>value;
        if(types == "square")
            gp[i] = new Square(value);
        if(types == "cube")
            gp[i] = new Cube(value);
        if(types == "ball")
            gp[i] = new Ball(value);
        if(types == "circle")
            gp[i] = new Circle(value);
    }
    for(int i = 0; i < cases; ++i)
    {
        gp[i]->show();
        delete gp[i];
    }
}

考点:虚基类

AC代码:

#include <bits/stdc++.h>
using namespace std;
const double PI = 3.14;
class  Graphic
{
    friend class Square;
    friend class Cube;
    friend class Ball;
    friend class Circle;
    double v;
public:
     Graphic(double _x):v(_x){}
     virtual void show()=0;
};
class Square:public  Graphic
{
public :
    Square(double _v):Graphic(_v){}
    void show()
    {
        cout << "Square, side:" << v << ", area:" << v*v << ".\n";
    }
};
class Cube:public  Graphic
{
public :
    Cube(double _v):Graphic(_v){}
    void show()
    {
        cout << "Cube, side:" << v << ", area:" << 6*v*v << ", volume:" << v*v*v << ".\n";
    }
};
class Ball:public  Graphic
{
public :
    Ball(double _v):Graphic(_v){}
     void show()
    {
        cout << "Ball, radius:" << v << ", area:" << 4*PI*v*v << ", volume:" << 4/3*PI*v*v*v << ".\n";
    }
};
class Circle:public  Graphic
{
public :
    Circle(double _v):Graphic(_v){}
     void show()
    {
        cout << "Circle, radius:" << v << ", area:" << PI*v*v << ".\n";
    }
};
 

吐糟一下:球类的体积公式居然不需要处理精度,QAQ WA1发。

Problem D: 薪酬计算

题面:

main函数:

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;
    }
 
}

考点:虚基类,进制转化,各种分类的细节

AC代码:

#include <bits/stdc++.h>
using namespace std;
class Person
{
public:
    string name;
    virtual void input()=0;
    Person(string _name):name(_name){}
    virtual void show()=0;
    virtual int pay()=0;
};
class Manager:public Person
{
    int m;
public:
    Manager(string _s):Person(_s){}
    void input()
    {
        cin >> m;
    }
    void show()
    {
        cout << name ;
        cout << " (Manager)";
        //cout << m << endl;
    }
    int pay()
    {
        return m*1000;
    }
};
class Employee:public Person
{
    int m,s;
public:
    Employee(string _s):Person(_s){}
    void input()
    {
        cin >> m >> s;
    }
    void show()
    {
        cout << name ;
        cout << " (Employee)";
        //cout << 12*m+s*m << endl;
    }
    int pay()
    {
        return (12+s)*m*1000;
    }
};
class HourlyWorker:public Person
{
    int m,h;
public:
    HourlyWorker(string _s):Person(_s){}
    void input()
    {
        cin >> m >> h;
    }
    void show()
    {
        cout << name ;
        cout << " (HourlyWorker)";
    }
    int pay()
    {
        return m*h;
    }
};
class CommWorker:public Person
{
    int m,h;
public:
    CommWorker(string _s):Person(_s){}
    void input()
    {
        cin >> m>> h;
    }
    void show()
    {
        cout << name ;
        cout << " (CommWorker)";
        //cout << m*2+0.2*h << endl;
    }
    int pay()
    {
        return (m*12+0.02*h)*1000;
    }
};
 

再次吐槽:为什么int过了,double过不了!!!!

 

posted @ 2019-06-11 12:36  浅花迷人  阅读(344)  评论(0编辑  收藏  举报