图形类设计——c++圆、矩形、三角形类的设计

题目要求:
设计三个图形类:Circle(圆)、Rectangle(矩形)、Triangle(三角形);
1、Cirlce 类基本信息:圆心坐标、半径;
Rectangle 类基本信息:长、宽;
Triangle 类基本信息:三个顶点坐标;
其中:成员变量为 private 属性,成员函数为 public 属性;
2、每个图形类有多个构造函数:缺省构造函数、带参数的构造函数;
3、每个图形类有计算图形的面积 GetArea(),显示图形的基本信息函数 Show(),修改基
本信息的函数 Set(形参)。以 Circle 类为例:通过 GetArea()计算圆的面积,Show()函数中显
示圆心坐标、直径、周长、面积等基本信息;Set(int x,int y, int r)函数可以修改圆心坐标和半
径。

这是我们学校的一道作业,其中有很多构造函数在测试用例上是用不到的,如果想调用编写好的构造函数还要在main函数那里进行一些修改。不过代码的大体上已经实现题目的要求了。

#include <iostream>
#include<cmath>
#define PI 3.14
using namespace std;
class Cirlce {
public:
	Cirlce() {
		x = 0;
		y = 0;
		r = 1;
	}
	Cirlce(float r0) {
		x = 0;
		y = 0;
		r = r0;
	}
	Cirlce(float x0, float y0, float r0) {
		x = x0;
		y = y0;
		r = r0;
	}
	float GetArea();
	void c_show();
	void c_set( float x0, float y0, float r0);
private:
	float x;
	float y;
	float r;
};
float Cirlce::GetArea() {
	return PI * r * r;
}
void Cirlce::c_show() {
	cout << "圆心坐标、直径、周长、面积为:" << endl;
	cout << "(" << x << "," << y << ")" << " " << 2 * r << " " << PI * r * 2 << " " << GetArea() << endl;
}
void Cirlce::c_set( float x0, float y0, float r0) {
	x = x0;
	y = y0;
	r = r0;
	cout << "修改后,";
	c_show();
}

class Rectangle {
public:
	Rectangle() {
		wide = 1;
		len = 1;
	}
	Rectangle(float wide0) {
		wide = wide0;
		len = 1;

	}
	Rectangle(float wide0, float len0) {
		wide = wide0;
		len = len0;
	}
	float GetArea();
	void r_show();
	void r_set(float wide0, float len0);
private:
	float wide;
	float len;
};
float Rectangle::GetArea() {
	return wide * len;
}
void Rectangle::r_show() {
	cout << "矩形宽、长、周长、面积为:" << endl;
	cout << wide << "," << len << "," << " " << 2 * (wide + len) << ", " << wide * len << endl;
}
void Rectangle::r_set(float x0, float y0) {
	wide = x0;
	len = y0;

	cout << "修改后,";
	r_show();
}


class Triangle {
public:
	Triangle() {
		a = 1;
		b = 1;
		c = 1;
	}
	Triangle(float a0) {
		a = a0;
		b = 1;

	}
	Triangle(float a0, float b0, float c0) {
		a = a0;
		b = b0;
		c = c0;
	}
	float GetArea();
	void t_show();
	void set_a(float A) { a=A; }
	void set_b(float B) { b=B; }
	void set_c(float C) { c=C; }
private:
	float a;
	float b;
	float c;
};
float Triangle::GetArea() {
	float p = (a + b + c) / 2;
	return sqrt(p * (p - a) * (p - b) * (p - c));
}
void Triangle::t_show() {
	cout << "三角形三边的长、周长、面积为:" << endl;
	cout << a << " " << b << " " << c << "," << a + b + c << "," << GetArea() << endl;
}

void tran(float* arr, Triangle& t) {
	for (int i = 0; i < 6; i++)cin >> arr[i];
	float A, B, C;//三条边的长度。 
	A = sqrt(pow((arr[0] - arr[2]), 2) + pow((arr[1] - arr[3]), 2));
	B = sqrt(pow((arr[0] - arr[4]), 2) + pow((arr[1] - arr[5]), 2));
	C = sqrt(pow((arr[2] - arr[4]), 2) + pow((arr[3] - arr[5]), 2));
	bool flag = true;
	if (A + B <= C)flag = 0;
	if (A + C <= B)flag = 0;
	if (B + C <= A)flag = 0;
	if (flag == true) {
		t.set_a( A);
		t.set_b(B);
		t.set_c(C);
	}
	else {
		cout << "输入的坐标无法构成三角形,请重新输入坐标:";
		tran(arr, t);
	}
}
int main() {
	//圆的部分测试:用引用来进行值的传递 
	cout << "请输入圆的坐标和半径:";
	float x1, y1, r1;
	cin >> x1 >> y1 >> r1;
	Cirlce c1(x1, y1, r1);
	c1.c_show();
	cout << "请问你想修改圆的数据吗?输入1修改,输入0不修改。";
	bool flag;
	if (cin >> flag && flag) {
		cout << "请输入新圆的坐标和半径:";
		cin >> x1 >> y1 >> r1;
		c1.c_set( x1, y1, r1);
	}

	//矩形部分的测试:
	cout << "请输入矩形的宽和长:";
	float wide1, len1;
	cin >> wide1 >> len1;
	Rectangle r2(wide1, len1);
	r2.r_show();
	cout << "请问你想修改矩形的数据吗?输入1修改,输入0不修改。";
	if (cin >> flag && flag) {
		cout << "请输入新宽和长:";
		cin >> wide1 >> len1;
		r2.r_set(wide1, len1);
	}

	//三角形的测试: 
	cout << "请输入三角形顶点的三个坐标:";
	float coordinate[7];
	Triangle t1;
	tran(coordinate, t1);//如果可以的话将坐标转换为a,b,c三条边的长度。并将对象初始化。否则重新输入坐标
	t1.t_show();
	cout << "请问你想修改三角形的数据吗?输入1修改,输入0不修改。";
	if (cin >> flag && flag) {
		cout << "请输入新三角形的坐标:";
		tran(coordinate, t1);
		t1.t_show();
	}
	return 0;
}

运行结果:
在这里插入图片描述

  • 12
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值