关闭

第六周实验报告4

189人阅读 评论(0) 收藏 举报
#include<iostream>
using namespace std;
#include<Cmath>

class CPoint
{
private:
	double x;  // 横坐标
	double y;  // 纵坐标
public:

	
	CPoint(double xx=0,double yy=0);
	double Distance(CPoint p) const;   // 两点之间的距离(一点是当前点,另一点为参数p)
    void input();  //以x,y 形式输入坐标点
	void output(); //以(x,y) 形式输出坐标点

	
};
class CTriangle
{
public:
	CTriangle(CPoint &X, CPoint &Y, CPoint &Z):A(X),B(Y),C(Z){}
	void setTriangle(CPoint &X, CPoint &Y, CPoint &Z);
	double perimeter();
	double area();
	bool isRightTriangle();
	bool isIsoscelesTriangle();
private:
	CPoint A,B,C;
};
void CPoint::input()
{

	char m;
	

	while(1)
	{
        cin>>x>>m>>y;
		if(m !=',')
		{
			cout<<"输入错误"<<endl;
	  
		}
		else
			break;
	}
}

void CPoint::output()
{
	cout <<"("<< x <<","<< y<<")"<<endl;
}


 double CPoint::Distance(CPoint p) const
{
	double n;
	n=sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
	return n;
}


CPoint::CPoint(double xx, double yy)
{
	x=xx;
	y=yy;
}


void CTriangle::setTriangle(CPoint &X, CPoint &Y, CPoint &Z)
{
	A=X;
	B=Y;
	C=Z;
}

double CTriangle::perimeter()
{
	double d1=A.Distance(B),d2=A.Distance(C),d3=B.Distance(C);
	return (d1+d2+d3);
}

double CTriangle::area()
{
	double d1,d2,d3,s;
	d1=A.Distance(B);
	d2=A.Distance(C);
	d3=B.Distance(C);
    s=(d1+d2+d3)/2;
	return sqrt(s*(s-d1)*(s-d2)*(s-d3));
}
bool CTriangle::isRightTriangle()
{
	double d1,d2,d3;
	d1=A.Distance(B);
	d2=A.Distance(C);
	d3=B.Distance(C);
	if((abs(d1*d1+d2*d2-d3*d3)<0.000001) || (abs(d1*d1+d3*d3-d2*d2)<0.000001) || (abs(d2*d2+d3*d3-d1*d1)<0.000001))
	{
		return true;
	}
	else
		return false;
}
bool CTriangle::isIsoscelesTriangle()
{
	double d1,d2,d3;
	d1=A.Distance(B);
	d2=A.Distance(C);
	d3=B.Distance(C);
	if((abs(d1-d2)<0.000001) || (abs(d1-d3)<0.000001) || (abs(d2-d3)<0.000001) )
	{
		return true;
	}
	else 
		return false;
}
int main()
{
	CTriangle tri1(CPoint(0,0),CPoint(0,2),CPoint(2,0));
	cout<<"周长是:"<<tri1.perimeter()<<endl;
	cout<<"面积是:"<<tri1.area()<<endl;
	cout<<"三角形"<<(tri1.isRightTriangle()?"是":"不是")<<"直角三角形"<<endl;
    cout<<"三角形"<<(tri1.isIsoscelesTriangle ()?"是":"不是")<<"等腰三角形"<<endl;
    return 0;
}
	



学会了新的表示方法!!!但是对构造函数还是不是很理解!

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:32630次
    • 积分:860
    • 等级:
    • 排名:千里之外
    • 原创:55篇
    • 转载:0篇
    • 译文:2篇
    • 评论:18条
    文章分类
    最新评论