#include <iostream>
#include <cmath>
using namespace std;
class Triangle
{
public:
void Setabc(float x,float y,float z); //置三邊的值,注意要能成三角形
void Getabc(float *x,float *y,float *z); //取三邊的值
float Perimeter(void); //計算是三角形的周長
float Area(void); //計算並返回是三角形的面積
private:
float a,b,c; //三邊爲私有數據成員
};
void main(void)
{
Triangle Tri1; //定義三角形的類的一個實例
Tri1.Setabc(4,5,6); //給三角形的三邊取值
float x,y,z;
Tri1.Getabc(&x,&y,&z); //將三邊的值爲xyz賦值
cout<<"三條邊爲:"<<x<<'\t'<<y<<'\t'<<z<<endl;
cout<<"三角形的周長爲:"<<Tri1.Perimeter()<<'\t'<<"面積爲:"<<Tri1.Area()<<endl;
}
void Triangle::Setabc(float x,float y,float z)
{
a = x;
b = y;
c = z;
}
void Triangle::Getabc(float *x,float *y,float *z)
{
*x = a;
*y = b;
*z = c;
}
float Triangle::Perimeter(void)
{
return (a + b+ c);
}
float Triangle::Area(void)
{
float w;
w = (0.5)*(a+b+c);
return (pow(w*(w-a)*(w-b)*(w-c),0.5));
}
第四周作業(一)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.