2012年第四周任務一

  實驗內容:設計一個三角形,求出它的周長和麪積* 程序頭部註釋開始  * 程序的版權和版本聲明部分  * Copyright (c) 2012, 煙臺大學計算機學院學生  * All rights reserved.  * 文件名稱:                               * 作    者:   張馨                            * 完成日期:     2011 2  年  03    月  12 日  * 版 本號:             * 對任務及求解方法的描述部分  * 輸入描述:要排序的數據在程序中初始化  * 問題描述:實現冒泡排序  * 程序輸出:排序後的結果  * 程序頭部的註釋結束(此處也刪除了斜槓)  
#include <iostream>
#include <math.h>
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);//將三邊的值爲x,y,z賦值
	cout<<"三條邊爲:"<<x<<'\t'<<y<<'\t'<<z<<endl;
	cout<<"三角形的周長爲:"<<Tri1.Perimeter()<<'\t'<<"面積爲:"<<Tri1.Area()<<endl;
}
//請在下面定義Triangle類中的各個成員函數
void Triangle ::Setabc(float x,float y,float z)
{
	if(x+y>z && x+z>y && y+z>x)
	{
		a=x,b=y,c=z;
	}
	else
	{cout<<"您輸入的數不能構成三角形!"<<endl;
	   exit(0);

	}
}
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 p;
	p=(a+b+c)/2;
	return sqrt(p*(p-a)*(p-b)*(p-c));
}


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