点线面子对象法

建立点,线,面的类(运用的子对象法),并通过点坐标求已知的构成的三角形面积:

#include<iostream>
#include<math.h>
#include<stdlib.h>
using namespace std;
class Point
{
public:
	Point(float a, float b)
	{
		x = a;
		y = b;
	};
	void print()
	{
		cout << "(" << x << "," << y << ")" << endl;
	}
private:
	float x, y;
};

class Line
{
public:
	Line(float x1, float y1, float x2, float y2) :m(x1, y1), n(x2, y2)
	{}
	double Length(float x1, float y1, float x2, float y2);
	void Print();
private:
	Point m, n;
};

double Line::Length(float x1, float y1, float x2, float y2)
{
	return sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));
}
void Line::Print()
{
	m.print();
	n.print();
}

class Triangle
{
public:
	Triangle(float x1, float y1, float x2, float y2, float x3, float y3) :p(x1, y1), q(x2, y2), r(x3, y3),
		L1(x1, y1, x2, y2), L2(x1, y1, x3, y3), L3(x2, y2, x3, y3)
	{}
	double Area(float x1, float y1, float x2, float y2, float x3, float y3);
	double Perimeter(float x1, float y1, float x2, float y2, float x3, float y3);
	void Print();
private:
	Point p, q, r;
	Line L1, L2, L3;
};
double Triangle::Perimeter(float x1, float y1, float x2, float y2, float x3, float y3)
{
	double j, k, l;
	double perimeter;
	j = L1.Length(x1, y1, x2, y2);
	k = L2.Length(x2, y2, x3, y3);
	l = L3.Length(x3, y3, x1, y1);
	return perimeter = j + k + l;
}

double Triangle::Area(float x1, float y1, float x2, float y2, float x3, float y3)
{
	double s, area;
	double j, k, l;
	s = Perimeter(x1, y1, x2, y2, x3, y3) / 2;
	j = L1.Length(x1, y1, x2, y2);
	k = L2.Length(x2, y2, x3, y3);
	l = L3.Length(x3, y3, x1, y1);
	return area = sqrt(s*(s - j)*(s - k)*(s - l));
}
void Triangle::Print()
{
	p.print();
	q.print();
	r.print();
}
void main()
{
	float x1, y1, x2, y2, x3, y3;
	cout << "请输入3个点坐标:";
	cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
	Point p1(x1, y1), p2(x2, y2), p3(x3, y3);
	p1.print();
	p2.print();
	p3.print();
	Line L1(x1, y1, x2, y2), L2(x1, y1, x3, y3), L3(x2, y2, x3, y3);
	Triangle T(x1, y1, x2, y2, x3, y3);
	cout << "三角形周长:" << T.Perimeter(x1, y1, x2, y2, x3, y3) << endl;
	cout << "三角形面积:" << T.Area(x1, y1, x2, y2, x3, y3) << endl;
	system("pause");
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值