1. 程式人生 > >《第五週任務三》求五個長方體的表面積和體積

《第五週任務三》求五個長方體的表面積和體積

 實驗報告模板1. 本學期的報告均發到csdn部落格。週一上機,週四前要完成本週上機任務併發布博文。

2. 本學期起程式頭部的註釋請自行加入,從本學期起不再統一給出。這是一個程式設計師良好習慣中的一部分,養成這個習慣。這也是展示個人專業品質的一個重要途徑。另外,在程式中需要的地方,也請加註釋。下面是我們一直在用的註釋模板。

(程式頭部註釋開始)

* 程式的版權和版本宣告部分

* Copyright (c) 2011, 煙臺大學計算機學院學生

* All rights reserved.* 檔名稱:

* 作 者: 張斌

* 完成日期:2012年3月26日

* 版 本 號: 5-3-1

* 對任務及求解方法的描述部分

* 輸入描述: 求五個長方體的表面積和體積

* 問題描述:

* 程式輸出:

 * 程式頭部的註釋結束

#include<iostream>

using namespace std;

class Cuboid
{
public:  
	Cuboid();
	Cuboid(double a, double b, double c):length(a),width(b),heigth(c) {}
	void set_cuboid();
	double volume();
	double areas();
	void display();
	
private:
	double length;
	double width;
	double heigth;
	bool is_cuboid(double, double, double);
};


Cuboid::Cuboid()
{
	length = 4;
	
	width = 5;
	
	heigth = 6;
}

int main()
{
	Cuboid c[5] = 
	{
		Cuboid(4, 5, 5),
			
			Cuboid(54, 75, 88),
			
			Cuboid(38, 18, 28),
			
			Cuboid(4, 55, 6),
	};
	
	for(int i = 0; i < 4; ++i)
	{
		c[i].volume();
		
		c[i].areas();
		
		c[i].display();
	}
	
	c[4].set_cuboid();
	
	c[4].volume();
	
	c[4].areas();
	
	c[4].display();
	
	return 0;
}



void Cuboid::set_cuboid()
{
	cout << "請輸入長方體的長、寬、高:" << endl;
	
	while(1)
	{
		cin >> length >> width >> heigth;
		
		if (! is_cuboid(length, width, heigth))
		{
			cout << "資料非法,請重新輸入:" << endl;
		}
		else 
		{
			break;
		}
	}
}

bool Cuboid::is_cuboid(double l, double w, double h)
{
	if(l <= 0 || w <= 0 || h <= 0)
	{
		return false;
	}
	else
	{
		return true;
	}
}

double Cuboid::volume()
{
	
	return (length * width * heigth);
}

double Cuboid::areas()
{
	return (2 * (length * width + length * heigth  + width * heigth));
}

void Cuboid::display()
{
	cout << "長是" << length << ", 寬是" << width << ", 高是" << heigth << " 的長方體體積是" << volume() << ",面積是" << areas() <<endl;
}