第五週項目3-用多文件組織多個類的程序

問題及代碼:

/*

 *Copyright (c) 2015,煙臺大學計算機學院

*All rights reserved.

*文件名稱:test.cop

*作者:

*完成日期:2015年4月7日

*版本號:v1.0

*

*問題描述: 

*輸入描述:

*輸出描述:
main.cpp
#include <iostream>
#include"CPoint.h"
using namespace std;

int main()
{
    double a,b;
    CPoint X,Y,Z;
    cout<<"輸入X,Y,Z的座標:"<<endl;
    X.input();
    Y.input();
    Z.input();

    CTriangle ct1(X,Y,Z);
    cout<<"輸出三角形的周長:";
    a=ct1.perimeter();
    cout<<a<<endl;
    cout<<"輸出三角形的面積:";
    b=ct1.area();
    cout<<b<<endl;
    if(ct1.isRightTriangle()==true)
    cout<<"是直角三角形"<<endl;
    else
        cout<<"不是直角三角形"<<endl;
    if(ct1.isIsoscelesTriangle()==true)
        cout<<"是等腰三角形"<<endl;
    else
        cout<<"不是等腰三角形"<<endl;

    return 0;
}

CPoint.h
#ifndef CPOINT_H_INCLUDED
#define CPOINT_H_INCLUDED
class CPoint
{
private:
  double x;  // 橫座標
  double y;  // 縱座標
public:
  CPoint(double xx=0,double yy=0);
  double Distance1(CPoint p) const; //兩點之間的距離(一點是當前點——想到this了嗎?,另一點爲p)
  double Distance0() const;          // 到原點(0,0)的距離
  CPoint SymmetricAxis(char style) const;//style取'x','y'和'o'分別表示按x軸, y軸, 原點對稱
  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; //三頂點
};


#endif // CPOINT_H_INCLUDED

CPoint.cpp
#include <iostream>
#include "CPoint.h"
#include <Cmath>
#include<stdio.h>
using namespace std;
void CTriangle::setTriangle(CPoint &X,CPoint &Y,CPoint &Z)
{
    A=X;
    B=Y;
    C=Z;
}
float CTriangle::perimeter()
{
    double length;
    length=A.Distance1(B)+B.Distance1(C)+C.Distance1(A);
    return length;

}
float CTriangle::area()
{
  double a=B.Distance1(C),b=C.Distance1(A),c=A.Distance1(B);
  double s = (a + b + c) / 2;
  return sqrt(s * (s - a) * (s - b) * (s - c));
}
bool CTriangle::isRightTriangle()
{
    double a=B.Distance1(C),b=C.Distance1(A),c=A.Distance1(B);

    if(c*c==a*a+b*b||b*b==c*c+a*a||a*a==b*b+c*c)
        return true;
    else
        return false;

}
bool CTriangle::isIsoscelesTriangle()
{
    double a=B.Distance1(C),b=C.Distance1(A),c=A.Distance1(B);
    if(a==b||a==c||b==c)
        return true;
    else
        return false;
}
double CPoint::Distance0()const
{
    double d;
    d=sqrt(x*x+y*y);
    return d;
}
void CPoint::input()
{
    char f;
    while(1)
    {
        cin>>x;
        f=getchar();
        cin>>y;
        if(f==',')
            break;
         cout<<"輸入有錯誤,請重新輸入"<<endl;
         fflush(stdin);
    }
}
void CPoint::output()
{
    cout<<"("<<x<<","<<y<<")"<<endl;
}
CPoint CPoint::SymmetricAxis(char style)const
{
    CPoint p(this->x,this->y);
switch (style)
{
case 'x':

    p.y=-y;break;
case 'y':
    p.x=-x;break;
case 'o':
    p.x=-x,p.y=-y;
    return p;
}
}
CPoint::CPoint(double xx,double yy)
{
    x=xx;
    y=yy;
}
double CPoint::Distance1(CPoint p) const
{
    double d;
    d=sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));
    return d;
}

運行結果:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章