西加加:类的组合

(题目好像是三个坐标围成的三角形,求每两点间的距离和三角形的周长、面积

 

版本1.0:


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

class Point
{
	double x1, y1;
	public:
		Point(double x = 0, double y = 0)
		{
			x1 = x; y1 = y;
		}
	
		void SetPoint(double x, double y)
		{
			x1 = x; y1 = y;
		}
		double Getx()
		{
			return x1;
		}
		double Gety()
		{
			return y1;
		}
		void Show()
		{
			cout << "Point(" << x1 << ',' << y1 << ")\n";
		}
};

class Line
{
	Point P1, P2;
	public:
		Line(Point p1, Point p2):P1(p1),P2(p2){  } 
		double getd()
		{
			double x=(P2.Getx()-P1.Getx());
			double y=(P2.Gety()-P1.Gety());
			return (sqrt)(x*x+y*y); 
		}
		
};

class Triangle
{
	Point P1, P2, P3;
	public:
		Triangle(Point p1,Point p2,Point p3):P1(p1),P2(p2),P3(p3) {	}
		void settriangle(Point p1,Point p2,Point p3)
		{
			P1 = p1;
			P2 = p2;
			P3 = p3;
		}
		double Line1()
		{
			
			Line L1(P1,P2);  //创建临时的线段对象,为了能调用其函数来计算距离
			return L1.getd();
			/*
			double x=(P2.Getx()-P1.Getx());  //避免了重复代码 
			double y=(P2.Gety()-P1.Gety());
			return (sqrt)(x*x+y*y);*/
		}
		double Line2()
		{
			
			Line L2(P2,P3);
			return L2.getd();
			/*
			double x=(P2.Getx()-P3.Getx());
			double y=(P2.Gety()-P3.Gety());
			return (sqrt)(x*x+y*y);*/
		}
		double Line3()
		{
			
			Line L3(P1,P3);
			return L3.getd();
			/*
			double x=(P3.Getx()-P1.Getx());
			double y=(P3.Gety()-P1.Gety());
			return (sqrt)(x*x+y*y);*/
		}
		double getc()
		{
			return (Line1()+Line2()+Line3());
		}
		double gets()
		{
			double x = getc()/2;
			return (sqrt(x*(x - Line1())*(x - Line2())*(x - Line3())));
		}
};

int main()
{
	Point P1(0,0);
	P1.Show();
	Point P2(0,2);
	P2.Show();
	Point P3(2,0);
	P3.Show(); 
	Triangle T(P1, P2, P3);
	cout << "distance1: " << T.Line1() << endl;
	cout << "distance2: " << T.Line2() << endl;
	cout << "distance3: " << T.Line3() << endl;
	cout << "周长为: " << T.getc() << endl;
	cout << "面积为:" << T.gets() << endl;
	return 0;
 } 

运行结果:

 

 

版本2.0:

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

class Point
{

	int x1, y1;
public:
	Point(int x = 0, int y = 0)
	{
		x1 = x; y1 = y;
	}

	void SetPoint(int x, int y)
	{
		x1 = x; y1 = y;
	}

	void GetPoint(int* px, int* py)
	{
		*px = x1; *py = y1;
	}

	void Show()
	{
		cout << "Point(" << x1 << ',' << y1 << ")\n";
	}
};

class Line
{

	Point p1, p2;
public:
	Line(int xx1=0, int yy1=0, int xx2=1, int yy2=1):p1(xx1,yy1),p2(xx2,yy2){  }
                 Line(Point& start, Point& end): p1(start), p2(end) {  }

	void SetPoint1(int xx1, int yy1)
	{
		p1.SetPoint(xx1, yy1);
	}

	void SetPoint2(int xx2, int yy2)
	{
		p2.SetPoint(xx2, yy2);
	}

	void GetPoint1(int* px1, int* py1)
	{
		p1.GetPoint(px1, py1);
	}

	void GetPoint2(int* rx2, int* ry2)
	{
		p2.GetPoint(rx2, ry2);
	}

	double LineLen()
	{
		double len;
		int x1=0,  x2=0,  y1=0,  y2=0;
		GetPoint1(&x1, &y1);
		GetPoint2(&x2, &y2);
		len = sqrt((double)((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)));
		return(len);
	}

	void Show()
	{
		p1.Show();
                                  p2.Show();
		cout << "Line Length=" << LineLen() << endl;
	}
};

class Triangle
{

	Point p1, p2, p3;
public:
	Triangle(int xx1, int yy1, int xx2, int yy2, int xx3, int yy3):p1(xx1,yy1), p2(xx2,yy2), p3(xx3,yy3){}

	Triangle(Point& one, Point& two, Point& three): p1(one), p2(two), p3(three) {  }

	void SetPoint1(int xx1, int yy1)
	{
		p1.SetPoint(xx1, yy1);
	}

	void SetPoint2(int xx2, int yy2)
	{
		p2.SetPoint(xx2, yy2);
	}

	void SetPoint3(int xx3, int yy3)
	{
		p3.SetPoint(xx3, yy3);
	}

	void GetPoint1(int* px1, int* py1)
	{
		p1.GetPoint(px1, py1);
	}

	void GetPoint2(int* px2, int* py2)
	{
		p2.GetPoint(px2, py2);
	}

	void GetPoint3(int* px3, int* py3)
	{
		p3.GetPoint(px3, py3);
	}

	double LineLen12()
	{
		//int px1=0, px2=0, py1=0, py2=0;
		//GetPoint1(&px1, &py1);
		//GetPoint2(&px2, &py2);

		//Line l(px1,py1,px2,py2);
		Line l(p1,p2);  //创建临时的线段对象,为了能调用其函数来计算距离
                                  return l.LineLen();
                  
	}

	double LineLen13()
	{
		Line l(p1,p3);//创建临时的线段对象,为了能调用其函数来计算距离
                                  return l.LineLen();
	}

	double LineLen23()
	{
		Line l(p2,p3);//创建临时的线段对象,为了能调用其函数来计算距离
                                  return l.LineLen();
	}

	double AroundLen()
	{
		return (LineLen12() + LineLen23() + LineLen13());
	}

	double Area()
	{
		double area, s, a, b, c;

		a = LineLen12();
		b = LineLen23();
		c = LineLen13();
		s = (a + b + c) / 2;
		area = sqrt(s * (s - a) * (s - b) * (s - c));
		return(area);
	}

	void Show()
	{
  		Line l1(p1,p2); //创建临时的线段对象,为了能调用它自己的代码显示信息
		l1.Show();

		Line l2(p2,p3);
		l2.Show();

		Line l3(p1,p3);
		l3.Show();

	
		cout << "LineLen12=" << LineLen12() << endl;
		cout << "LineLen13=" << LineLen13() << endl;
		cout << "LineLen23=" << LineLen23() << endl;
		cout << "AroundLen=" << AroundLen() << endl;
		cout << "Area=" << Area() << endl;
	}

};

int main()
{
	cout << "测试三角形类:\n";
	Triangle tri(0, 0, 0, 2, 2, 0);

	tri.Show();

	return 0;
}

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值