1. 程式人生 > >4_2三角形類(2)

4_2三角形類(2)

/*
* 程式的版權和版本宣告部分
* Copyright (c)2012, 煙臺大學計算機學院學生
* All rightsreserved.
* 檔名稱: object.cpp
* 作者:紀子龍
* 完成日期: 2013年3  月 21 日
* 版本號: v1.0
* 輸入描述:無
* 問題描述:設計求三角形周長和麵積的類。
* 程式輸出:三角形的周長和麵積
*/

#include<iostream>
#include<Cmath>
using namespace std;
class Triangle
{
public:
	inline	void setA(double x)//置三邊的值,注意要能成三角形
	{
		a=x;
	}
	inline	void setB( double x)
	{
		b=x;
	}
	inline	void setC( double x)
	{
		c=x;
	}
	inline	int getA()//取三邊的值
	{
	return a;
	}
	inline	int getB()
	{
		return b;
	}
	inline	int getC()
	{	
		return c;
	}
	double perimeter(void);//計算三角形的周長
	double area(void);//計算並返回三角形的面積
	bool	isTriangle();//判斷是否為三角型
private:
	double a,b,c; //三邊為私有成員資料
};
double Triangle::perimeter(void)//計算三角形的周長
{
	return a+b+c;
}
double Triangle::area(void)//計算並返回三角形的面積
{
	int s=(a+b+c)/2;
	return sqrt(s*(s-a)*(s-b)*(s-c));
}
bool	Triangle::isTriangle()
{
	if((a+b)>c&&(a-b)<c)
		return true;
	else 
		return false;
}
int main()
{	
	Triangle tri1;	//定義三角形類的一個例項(物件)
	double x,y,z;
	cout<<"請輸入三角形的三邊:";
	cin>>x>>y>>z;
	tri1.setA(x);tri1.setB(y);tri1.setC(z);	//為三邊置初值
	if(tri1.isTriangle())
	{ 
		cout<<"三條邊為:"<<tri1.getA()<<','<<tri1.getB()<<','<<tri1.getC()<<endl;
		cout<<"三角形的周長為:"<< tri1.perimeter()<<'\t'<<"面積為:"<< tri1.area()<<endl;
	}
	else
		cout<<"不能構成三角形"<<endl;
	system("pause");
	return 0;
}
執行結果: