Boolean 第三周

为下面的 Rectangle 和 Circle 类重写 getArea 虚函数。然后创建一个数组。 使用一个循环,生成 10 个 Rectangle、10 个 Circle,根据循环遍历顺序为它 们设置 no 编号,位置、长、宽、半径等其他信息取随机 1~10 之间的整数值, 然后将它们加入到创建好的数组中。最后,将这个长度为 20 的数组中所有面 积小于 50 的形状删除。将剩下的形状组成一个新的数组返回。

注意: 1. 补齐任务所需的其他函数。2. 考虑正确的内存管理。3. 使用原生数 组,不使用 vector 等容器。

  1. class Shape
  2. {
  3. int no;
  4. public:
  5. virtual int getArea()=0;
  6. };
  7. class Point
  8. {
  9. int x;
  10. int y;
  11. };
  12. class Rectangle: public Shape
  13. {
  14. int width;
  15. int height;
  16. Point leftUp;
  17. };
  18. class Circle: public Shape
  19. {
  20. Point center;
  21. int radius;
  22. };

.h
 
  
#ifndef __CLASS_DEFINE_H__  
#define __CLASS_DEFINE_H__  
#include<iostream>  
#include<random>  
using namespace std;
const double PI = 3.1415926;

namespace test {
	template<typename T>
	class Shape
	{
	private:
		int no;
	public:
		static int count;
		Shape() :no(++count){ ; }
		Shape(const Shape& other) :no(++count)  { ; };
		Shape& operator=(const Shape& other)
		{
			if (this == &other){
				return *this;
			}
			else
			{
				no =other.no;
				return *this;
				
			}
		}
		~Shape()
		{
			count--;
		}
		int get_no()const { return this->no; }
		static void set_count(const int x)  { count = x; }
		virtual T getArea() = 0;
	};
	class Point
	{
	private:
		int x;
		int y;
	public:
		Point(int x1 = 0, int y1 = 0) : x(x1), y(y1)    { ; }    
		int get_x() const { return x; }
		int get_y() const { return y; }
	};
	
	class Rectangle : public Shape<double>
	{
	private:
		int width;
		int height;
		Point leftUp;
	public:
		Rectangle(int width1 = 0, int height1 = 0, int x1 = 0, int y1 = 0);         
		Rectangle(const Rectangle& other);                            
		Rectangle& operator=(const Rectangle& other);              
		~Rectangle();                                                
		int get_width() const { return width; }
		int get_height() const { return height; }
		const Point& get_leftUp() const { return leftUp; }
		virtual double getArea()    { return (this->height)*(this->width); }
		friend Rectangle& __doequ(Rectangle*, const Rectangle&);
	};
	class Circle : public Shape<double>
	{
	private:
		Point center;
		int radius;
	public:
		Circle(int radius1 = 0, int x2 = 0, int y2 = 0);            //构造函数  
		~Circle();                                              //析构函数 只声明不定义会出无法解析的错误  
		Circle& operator=(const Circle& other);             //拷贝赋值函数  
		int get_radius() const { return radius; }
		const Point& get_center() const { return center; }
		virtual double getArea()    { return PI*(this->radius)*(this->radius); }      //得到面积  
	};

	class Rand_int              //等概率整数的随机数生成器      随机生成器由引擎(负责生成一组随机值或者伪随机数)和一种分布(负责把引擎产生的值映射到某个数学分布上)  
	{
	public:
		Rand_int(int low, int high) : dist{ low, high }
		{
			;
		}           //构造函数  初始化随机数范围  
		int operator()(){ return dist(re); }        //操作符() 重载  得到一个随机int  
	private:
		default_random_engine re;       //默认随机引擎  
		uniform_int_distribution<> dist;//分布:生成的所有整数概率相等  

	};

	typedef struct twotype
	{
		Rectangle p;
		Circle q;
	}twotype;

	/************************************                       The Rectangle class member function define.                     ************************************/

	inline Rectangle::Rectangle(int width1, int height1, int x1, int y1) : width(width1), height(height1), leftUp(Point(x1, y1))    //Rectangle类的构造函数,需要Point有构造函数  
	{
		;
	}

	inline Rectangle::~Rectangle()
	{
		;
	}

	Rectangle& Rectangle::operator=(const Rectangle& other)
	{
		if (this == &other)
		{
			return *this;
		}
		Shape<double>::operator=(other);
		this->width = other.width;
		this->height = other.height;
		this->leftUp = other.leftUp;
		return *this;
	}
	inline Circle::Circle(int radius2, int x2, int y2) :radius(radius2), center(Point(x2, y2))
	{
		;
	}

	inline Circle::~Circle()
	{
		;
	}

	Circle& Circle::operator=(const Circle& other)              //拷贝赋值函数  
	{
		if (this == &other)
		{
			return *this;
		}
		Shape<double>::operator=(other);
		center = other.center;
		radius = other.radius;
		return *this;
	}

	
	void print()
	{
		;
	}
	template<class type, class... types>    
	void print(const type& x, const types&... next)
	{
		cout << x << endl;
		print(next...);
	}
	ostream& operator<<(ostream& os, test::Rectangle& rec)            
	{
		return os << ' ' << "NO." << rec.get_no() << ' ' << '(' << "长=" << rec.get_height() << ',' << "宽=" << rec.get_width() << ',' << "面积=" << rec.getArea() << ')' << "      " << "坐标=" << '(' << rec.get_leftUp().get_x() << ',' << rec.get_leftUp().get_y() << ')' << endl;
	}

	ostream& operator<<(ostream& os, test::Circle& cir)          
	{
		return os << ' ' << "NO." << cir.get_no() << ' ' << '(' << "半径=" << cir.get_radius() << ',' << "面积=" << cir.getArea() << ')' << "      " << "坐标=" << '(' << cir.get_center().get_x() << ',' << cir.get_center().get_y() << ')' << endl;
	}

	twotype* create_array(twotype *buff, twotype *buff2, const int& count)
	{
		Rand_int randx{ 1, 10 };

		print("初次创建:");
		for (int i = 0; i < count; i++)
		{
			Rectangle a(randx(), randx(), randx(), randx());
			Circle b(randx(), randx(), randx());
			buff[i].p = a;
			buff[i].q = b;
			cout << buff[i].p << endl;
			cout << buff[i].q << endl;
		}
		print("删除面积小于之后:");
		for (int i = 0; i < count; i++)
		{
			if (buff[i].p.getArea() >= 50)
			{
				buff2[i].p = buff[i].p;
				cout << buff2[i].p << endl;
			}
			if (buff[i].q.getArea() >= 50)
			{
				buff2[i].q = buff[i].q;
				cout << buff2[i].q << endl;
			}
		}


		return buff;
	}

}

#endif  




.cpp
#include<iostream>  
#include"Shape.h"  
#include<random>  

using namespace std;
int test::Shape<double>::count = 0;
const int x = 10;

int main(void)
{
	//guo::Rectangle r1{ 2, 2, 1, 1 };  
	//guo::Circle c1{ 2, 3, 4 };  
	//guo::Rectangle *a1[x];  
	//guo::Circle *a2[x];  
	test::twotype a3[x];
	test::twotype buff2[x];


	//cout << "矩形r1:" << r1 << endl;  

	//cout << "圆形c1:" << c1 << endl;  

	//cout << "sizeof(r1)=" <<sizeof(r1) << endl;     //继承证明验证  

	/*guo::print("请输入创建数组的大小(20):");*/      //自定义print()函数验证  
	//cin >> x;  

	test::create_array(a3, buff2, x);                    //创建删除一体  

	/*printArray(a3, x);*/



	getchar();
	getchar();
	return 0;
}



在生成随机值时,借鉴了他人的实现方法,,不知道是否是最优实现。
class Rand_int              //等概率整数的随机数生成器      随机生成器由引擎(负责生成一组随机值或者伪随机数)和一种分布(负责把引擎产生的值映射到某个数学分布上)  
	{
	public:
		Rand_int(int low, int high) : dist{ low, high }
		{
			;
		}           //构造函数  初始化随机数范围  
		int operator()(){ return dist(re); }        //操作符() 重载  得到一个随机int  
	private:
		default_random_engine re;       //默认随机引擎  
		uniform_int_distribution<> dist;//分布:生成的所有整数概率相等  

	};
本次作业感觉难度稍大,仍有部分不太清楚的地方,需要继续研究本次代码。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值