美文网首页
[GeekBand][C++面向对象高级编程]第三周作业

[GeekBand][C++面向对象高级编程]第三周作业

作者: 散夜霜 | 来源:发表于2017-01-23 23:08 被阅读0次

    编译器坏了,不得已用文本交作业。
    以下为两个文件的代码:
    Shape.h

    #ifndef __MYSHAPE__
    #define __MYSHAPE__
    
    #include <iostream>
    
    const double PI = 3.1415926;
    class Shape;
    class Point;
    class Rectangle;
    class Circle;
    
    inline std::ostream& operator <<(std::ostream& os, const Point& r);
    
    class Shape
    {
    private:
        int no;
        static int count;
    public:
        Shape()
            :no(count++)
        {}
        void set_serial_number(int n) { no = n; }
        int get_serial_number() const { return no; }
        virtual int getArea() = 0;          // return the area of the object
        virtual void information() = 0;     // print the information of the object
        virtual ~Shape() { count--; }
    };
    
    int Shape::count = 0;
    
    class Point
    {
    private:
        int x;
        int y;
    public:
        Point(int m = 0, int n = 0)
            :x(m), y(n)
        {}
        int get_x() const { return x; }
        int get_y() const { return y; }
    };
    
    class Rectangle :public Shape
    {
    private:
        int width;
        int height;
        Point leftUp;
    public:
        Rectangle(int w = 0, int h = 0, int x = 0, int y = 0)
            :width(w), height(h), leftUp(x, y)
        {}
        Rectangle& operator =(const Rectangle& other);
        virtual int getArea() { return width*height; }
        virtual void information();
    };
    
    class Circle :public Shape
    {
    private:
        Point center;
        int radius;
    public:
        Circle(int x = 0, int y = 0, int r = 0)
            :center(x, y), radius(r)
        {}
        Circle& operator=(const Circle& other);
        virtual int getArea() { return int(PI*radius*radius); }
        virtual void information();
    };
    
    inline std::ostream& operator <<(std::ostream& os, const Point& r)
    {
        return os << '(' << r.get_x() << ',' << r.get_y() << ')';
    }
    
    inline Rectangle & Rectangle::operator=(const Rectangle & other)
    {
        this->set_serial_number(other.get_serial_number());
        this->width = other.width;
        this->height = other.height;
        this->leftUp = other.leftUp;
        return *this;
    }
    
    inline void Rectangle::information()
    {
        std::cout
            << "Rectangle   No." << get_serial_number()
            << "    LeftUp: " << leftUp
            << "    Width: " << width
            << "    Height: " << height
            << std::endl;
        return;
    }
    
    inline Circle & Circle::operator=(const Circle & other)
    {
        this->set_serial_number(other.get_serial_number());
        this->center = other.center;
        this->radius = other.radius;
        return *this;
    }
    
    inline void Circle::information()
    {
        std::cout
            << "Circle      No." << get_serial_number()
            << "    Center: " << center
            << "    Radius: " << radius
            << std::endl;
        return;
    }
    
    #endif // !__MYSHAPE__
    

    Shape-test.cpp

    #include <iostream>
    #include <random>
    #include "Shape.h"
    
    Shape** create_array(int count = 20, int Rectangle_amount = 10);
    Shape** filter_array(Shape** Shape_array = nullptr, int count = 20, int Rectangle_amount = 10);
    void delete_array(Shape** Shape_array = nullptr, int count = 20);
    void print_array(Shape** Shape_array = nullptr, int count = 20);
    
    const int n = 20;
    const int rn = 10;
    
    int main()
    {
        std::cout << "以下是生成的形状:" << std::endl;
        Shape** Shape_array;
        Shape_array = create_array(n, rn);
        print_array(Shape_array);
        std::cout << "以下是经过剪辑形成的新形状数列:" << std::endl;
        Shape** new_array = filter_array(Shape_array, n, rn);
        print_array(new_array);
        delete_array(new_array);
        delete_array(Shape_array);
        system("pause");
        return 0;
    }
    
    Shape** create_array(int count, int Rectangle_amount)
    {
        std::random_device rd;
        Shape** Shape_array = new Shape*[count];
    
        for (int i = 0; i < count; i++)
        {
            if (i < Rectangle_amount)
            {
                Shape_array[i] = new Rectangle(rd() % 10 + 1, rd() % 10 + 1, rd() % 10 + 1, rd() % 10 + 1);
            } 
            else
            {
                Shape_array[i] = new Circle(rd() % 10 + 1, rd() % 10 + 1, rd() % 10 + 1);
            }
        }
    
        return Shape_array;
    }
    
    Shape** filter_array(Shape** Shape_array, int count, int Rectangle_amount)
    {
        Shape** new_array{ nullptr };
        int rn{ 0 };
        int cn{ 0 };
    
        for (int i = 0; i < count; i++)
        {
            if (Shape_array[i]->getArea() >= 50)
            {
                if (i < Rectangle_amount)
                {
                    rn++;
                } 
                else
                {
                    cn++;
                }
            }
        }
    
        static int filter_amount = rn + cn;
    
        new_array = new Shape*[filter_amount];
    
        for (int i = 0, n = 0; i < filter_amount; i++, n++)
        {
            if (i < rn)
            {
                new_array[i] = new Rectangle;
                *(new_array[i]) = *(Shape_array[n]);
            }
            else
            {
                new_array[i] = new Circle;
                *(new_array[i]) = *(Shape_array[n]);
            }
        }
    
        return new_array;
    }
    
    void delete_array(Shape ** Shape_array, int count)
    {
        for (int i=0;i<count;i++)
        {
            delete Shape_array[i];
        }
        delete[]Shape_array;
        return;
    }
    
    void print_array(Shape ** Shape_array, int count)
    {
        for (int i = 0; i < count; i++)
        {
            Shape_array[i]->information();
        }
        return;
    }
    

    相关文章

      网友评论

          本文标题:[GeekBand][C++面向对象高级编程]第三周作业

          本文链接:https://www.haomeiwen.com/subject/mnfzbttx.html