实验5-1 图形类设

设计三个图形类

Circle(圆)、Rectangle(矩形)、Triangle(三角形);
1、Cirlce 类基本信息:圆心坐标、半径;Rectangle 类基本信息:长、宽;Triangle 类基本信息:三个顶点坐标;
其中:成员变量为 private 属性,成员函数为public 属性;
2、每个图形类有多个构造函数:缺省构造函数、带参数的构造函数;
3、每个图形类有计算图形的面积GetArea(),显示图形的基本信息函数Show(),修改基本信息的函数Set(形参)。以Circle 类为例:通过GetArea()计算圆的面积,Show()函数中显示圆心坐标、直径、周长、面积等基本信息;Set(int x,int y, int r)函数可以修改圆心坐标和半径。
[实验提示]
1、注意多个构造函数的声明方式与定义实现;
2、当输入数据为不合理数据时(例如:输入的三角形的顶点是否能组成一个三角形),
提示用户输入错误;

C++ 代码实现

#include <iostream>
#include <cmath>
using namespace std;

class Rectangle
{
public:
	Rectangle()
	{
		width = 1;
		length = 1;
	}
	Rectangle(double width, double length)
	{
		this->width = width;
		this->length = length;
	}
	double GetArea();
	void r_show();
	void r_set(double x, double y);
private:
	double width;
	double length;
};
double Rectangle::GetArea()
{
	return width * length;
}
void Rectangle::r_show()
{
	cout << "宽/长/周长/面积分别为:" << endl;
	cout << width << "," << length << "," << 2 * (width + length) << "," << GetArea() << endl;
}
void Rectangle::r_set(double x, double y)
{
	width = x;
	length = y;
	cout << "修改边长,";
	r_show();
}

double PI = 3.14;
class Circle
{
public:
	Circle()
	{
		x = 0; y = 0; r = 1;
	}
	Circle(double x, double y, double r)
	{
		this->x = x; 
		this->y = y;
		this->r = r;
	}
	double GetArea();
	void c_show();
	void c_set(double x0, double y0, double r0);
private:
	double x, y, r;
};
double Circle::GetArea()
{
	return PI * r * r;
}
void Circle::c_show()
{
	cout << "圆心坐标/直径/周长/面积分别为:" << endl;
	cout << "(" << x << "," << y << ")" << "," << 2 * r << "," << 2 * PI * r << "," << GetArea() << endl;
}
void Circle::c_set(double x0, double y0, double r0)
{
	x = x0;
	y = y0;
	r = r0;
	cout << "修改圆心坐标和半径,";
	c_show();
}

class Triangle
{
public:
	Triangle()
	{
		double x1 = 0, y1 = 0, x2 = 0, y2 = 0, x3 = 0, y3 = 0;
		double A = 0, B = 0, C = 0;

	}
	Triangle(double x1, double y1, double x2, double y2, double x3, double y3,double A, double B, double C)
	{
		this->x1 = x1;
		this->y1 = y1;
		this->x2 = x2;
		this->y2 = y2;
		this->x3 = x3;
		this->y3 = y3;
		this->A = A;
		this->B = B;
		this->C = C;
	}
	double GetArea();
	void t_show();
	void t_set(double x1, double y1, double x2, double y2, double x3, double y3);

	double x1, y1, x2, y2, x3, y3, A, B, C;
};
double Triangle::GetArea()
{
	double p = (A + B + C) / 2;
	return sqrt(p * (p - A) * (p - B) * (p - C));
}
void Triangle::t_show()
{
	if (A + B > C && A + C > B && B + C > A)
	{
		cout << "三边(A/B/C)长/周长/面积分别为:" << endl;
		cout << A << " " << B << " " << C << "," << A + B + C << "," << GetArea() << endl;
	}
	else
	{
		cout << "不能构成三角形";
	}
	
	
}
void Triangle::t_set(double x1, double y1, double x2, double y2, double x3, double y3)
{
	this->x1 = x1;
	this->y1 = y1;
	this->x2 = x2;
	this->y2 = y2;
	this->x3 = x3;
	this->y3 = y3;
	double a, b, c;
	a = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
	b = sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3));
	c = sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3));
	A = a;
	B = b;
	C = c;
}
int main()
{
	cout << "【圆】输入圆心坐标和半径:";
	double x1, y1, r1;
	cin >> x1 >> y1 >> r1;
	Circle circl(x1, y1, r1);
	circl.c_show();
	cout << "是否修改数据?(1:是/0:否)";
	int flag;
	cin >> flag;
	if (flag == 1)
	{
		cout << "输入新的圆心和半径:";
		cin >> x1 >> y1 >> r1;
		circl.c_set(x1, y1, r1);
	}

	cout << "【矩形】输入宽和长:";
	double width1, length1;
	cin >> width1 >> length1;
	Rectangle rect(width1, length1);
	rect.r_show();
	
	cout << "是否修改数据?(1:是/0:否)";
	cin >> flag;
	if (flag == 1)
	{
		cout << "输入新的宽和长:";
		cin >> width1 >> length1;
		rect.r_set(width1, length1);
	}

	cout << "【三角形】输入三个顶点坐标:";
	double p1, p2, p3, p4, p5, p6;
	cin >> p1 >> p2 >> p3 >> p4 >> p5 >> p6;
	Triangle tria;
	tria.t_set(p1, p2, p3, p4, p5, p6);
	tria.t_show();
	cout << "是否修改数据?(1:是/0:否)";
	cin >> flag;
	if (flag == 1)
	{
		cout << "输入新的顶点坐标:";
		cin >> p1 >> p2 >> p3 >> p4 >> p5 >> p6;
		tria.t_set(p1, p2, p3, p4, p5, p6);
		tria.t_show();
	}
	
	system("pause");
	return 0;
}

测试

  • Circle 类测试数据:
    圆心 半径 (20,30) 5、(-20,40) 30、(40,-10) -5
  • Rectangle 类测试数据:
    顶点坐标: (20,50)、(40,5)、(-9,10)、(43,-8)
  • Triangle 类测试数据:
    顶点: (20,30)、(40,50)、(40,20)、(10,10)、(50,10)、(35,10)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值