第五週任務1-2

/* (程序頭部註釋開始)
* 程序的版權和版本聲明部分
* Copyright (c) 2011, 煙臺大學計算機學院學生 
* All rights reserved.
* 文件名稱:                              
* 作    者:劉楊                             
* 完成日期:2012 年 3 月 21 日
* 版 本 號:          

* 對任務及求解方法的描述部分
* 輸入描述: 
* 問題描述: 
* 程序輸出: 
* 程序頭部的註釋結束
*/

#include<iostream>
#include<cmath>
using namespace std;
class Triangle
{
public:
	Triangle(){a=1;b=1;c=1;} //()設計默認構造函數,即不指定參數時,默認各邊長爲;  
	float Perimeter(void);
	float Area(void);
	void showMessage();
private:
	float a, b, c; 
};

void Triangle:: showMessage()
{
	cout << "三角形的三邊長分別爲:" << a << "," << b << "," << c << endl;
	cout << "該三角形的周長爲:" << Perimeter() << endl << "面積爲:" << Area() << endl << endl;
}

void main(void)
{
	Triangle Tri1;	
	Tri1.showMessage();
}

float Triangle :: Perimeter(void)//計算三角形的周長   
{  
    float d;  
    d=a+b+c;  
    return d;  
}  
  
float Triangle :: Area(void)//計算並返回三角形的面積   
{  
    float p, s;  
    p=(a+b+c)/2;  
    s=sqrt(p*(p-a)*(p-b)*(p-c));  
    return s;  
}  
 
運行結果:

發佈了37 篇原創文章 · 獲贊 4 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章