第5周项目多文件组织三角形类

main.cpp

#include <iostream>
#include<cmath>
#include"lei.h"
using namespace std;
int main()
{
    cout<<"输入三角形三个顶点的值:"<<'\n';
    CPoint t1,t2,t3;
    t1.input();
    t2.input();
    t3.input();
    CTriangle s(t1,t2,t3);
    s.setTriangle(t1,t2,t3);
    cout<<"周长是:"<<s.perimeter()<<endl;
    cout<<"面积是:"<<s.area()<<endl;
    if(s.isRightTriangle())
        cout<<"是直角三角形."<<endl;
    else cout<<"不是直角三角形"<<endl;
    if(s.isIsoscelesTriangle())
        cout<<"是等腰三角形";
    else cout<<"不是等腰三角形";
    return 0;
}

lei.h

#ifndef LEI_H_INCLUDED
#define LEI_H_INCLUDED
class CPoint
{
private:
    double x;  // 横坐标
    double y;  // 纵坐标
public:
    CPoint(double xx=0,double yy=0);
    double Distance1(CPoint p) const; //两点之间的距离(一点是当前点——想到this了吗?,另一点为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);//
  float perimeter(void);//计算三角形的周长
  float area(void);//计算并返回三角形的面积
  bool isRightTriangle(); //是否为直角三角形
  bool isIsoscelesTriangle(); //是否为等腰三角形
private:
  CPoint A,B,C; //三顶点
  double a,b,c;//三边长
};


#endif // LEI_H_INCLUDED

l.cpp

#include"lei.h"
#include<cmath>
#include<iostream>
using namespace std;
CPoint::CPoint(double xx,double yy)
{
    x=xx;
    y=yy;
}
double CPoint::Distance1(CPoint p) const
{
    double s;
    s=sqrt((this->x-p.x)*(this->x-p.x)+(this->y-p.y)*(this->y-p.y));
    return s;
}
void CPoint::input()
{
    char c;
    while(1)
    {
        cin>>x>>c>>y;
        if (c==',') break;
        cout<<"输入的数据格式不符合规范,请重新输入\n";
    }
}
void CPoint::output()
{
    cout<<"("<<x<<","<<y<<")"<<endl;
}
void CTriangle::setTriangle(CPoint &X,CPoint &Y,CPoint &Z)
{
    a=X.Distance1(Y);
    b=X.Distance1(Z);
    c=Y.Distance1(Z);
    cout<<"三边长是:"<<a<<" "<<b<<" "<<c;
}
float CTriangle::perimeter(void)
{
    return a+b+c;
}
float CTriangle::area(void)
{
    float p;
    double s;
    p=(a+b+c)/2;
    if(p-a>0&&p-b>0&&p-c>0)
    s=(p*(p-a)*(p-b)*(p-c));
    else cout<<"构不成三角形!"<<endl;
    return sqrt(s);
}
 bool CTriangle::isRightTriangle()
 {
     double max=a;
     if(max<b) max=b;
     if(max<c) max=c;
     if(((max==a)&&(abs(a*a-b*b-c*c)<1e-7))||((max==b)&&(abs(b*b-a*a-c*c)<1e-7))||((max==c)&&(abs(c*c-b*b-a*a)<1e-7)))
        return true;
     return false;
 }
 bool CTriangle::isIsoscelesTriangle()
 {
      if((abs(a-b)<1e-7)||(abs(b-c)<1e-7)||(abs(c-a)<1e-7))
        return true;
     return false;
 }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值