/*Copyright (c) 2011, 煙臺大學計算機學院
* All rights reserved.
* 作 者: 石堯
* 完成日期:2014 年04 月 02日
* 版 本 號:v1.0
*
* 問題描述:對象作爲數據成員。
* 樣例輸入:略.
* 樣例輸出:略。
* 問題分析:通過這種方式,將某些類組合到另外的類當中,當作其中的一個“部件”使用。
*/
#include <iostream>
#include <Cmath>
#include"CTriangle.h"
#include"Cpoint.h"
using namespace std;
int main()
{
Cpoint s(0,0),r(5,3),t(2,3);
CTriangle c(s,r,t);
cout<<"三角形的周長:"<<c.perimeter()<<endl;
cout<<"三角形的面積:"<<c.area()<<endl;
if(c.isRightTriangle())
{
cout<<"此三角形是直角三角形!"<<endl;
}
else
{
cout<<"此三角形不是直角三角形!"<<endl;
}
if(c.isIsoscelesTriangle())
{
cout<<"此三角形是等腰三角形三角形!"<<endl;
}
else
{
cout<<"此三角形是等腰三角形三角形!"<<endl;
}
return 0;
}
#ifndef CTRIANGLE_H_INCLUDED
#define CTRIANGLE_H_INCLUDED
#include"CTriangle.h"
#include"Cpoint.h"
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(void);//計算三角形的周長
double area(void);//計算並返回三角形的面積
bool isRightTriangle(); //是否爲直角三角形
bool isIsoscelesTriangle(); //是否爲等腰三角形
private:
Cpoint A,B,C; //三頂點
};
void CTriangle::setTriangle(Cpoint &X,Cpoint &Y,Cpoint &Z)
{
A=X;
B=Y;
C=Z;
}
double CTriangle::perimeter(void)
{
double a=B.Distance1(C),b=A.Distance1(C),c=B.Distance1(A);
return (a+b+c);
}
double CTriangle::area(void)
{
double a=B.Distance1(C),b=A.Distance1(C),c=B.Distance1(A);
double p,s;
p=(a+b+c)/2;
s=sqrt(p*(p-a)*(p-b)*(p-c));
return s;
}
bool CTriangle::isRightTriangle() //是否爲直角三角形
{
double a=B.Distance1(C),b=A.Distance1(C),c=B.Distance1(A);
if((a*a)+(b*b)==(c*c))
return true;
else
return false;
}
bool CTriangle::isIsoscelesTriangle() //是否爲等腰三角形
{
double a=B.Distance1(C),b=A.Distance1(C),c=B.Distance1(A);
if(a==b||a==c||b==c)
return true;
else
return false;
}
#endif // CTRIANGLE_H_INCLUDED
#ifndef CPOINT_H_INCLUDED
#define CPOINT_H_INCLUDED
#include"CTriangle.h"
#include"Cpoint.h"
class Cpoint
{
private:
double x;//橫座標
double y;//縱座標
public:
Cpoint(double xx=0,double yy=0);
double Distance1(Cpoint p) const;//兩點之間的距離(一點是當前點,另一點爲參數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;
}
#endif // CPOINT_H_INCLUDED
第六週項目3用多文件組織多個類的程序
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.