第4周實驗報告

 任務1:

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

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

#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);    //將三邊的值爲 x,y,z賦值 

	cout << "三條邊爲:" << x << '\t' << y << '\t' << z << endl; 

	cout << "三角形的周長爲:" << Tri1.Perimeter()<<'\t'<<"面積爲:"<< Tri1.Area()<<endl; 

	system ("pause");
} 
//請在下面定義 Triangle 類中的各個成員函數 

void Triangle::Setabc(float x, float y, float z)
{
	if (!(x + y < z || x + z < y || z + y < x))
	{
		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 p = (a + b + c) / 2;

	return sqrt(p * (p - a)* (p - b) * (p - c)); 
}


任務2:

 

任務感想:利用文件組織項目具有實用性O(∩_∩)O。

 

任務3:

#include <iostream> 

#include <string>

using namespace std; 

class NaturalNumber 
{
private: 

	int n;  

public: 

	void setValue (int x);//置數據成員 n 的值,要求判斷是否是正整數 

	int getValue();   //返回私有數據成員 n的值 

	bool isPrime();   //判斷數據成員 n 是否爲素數,是返回 true,否則返回 false 

	void printFactor();   //輸出數據成員 n的所有因子,包括 1 和n 自身 

	bool isPerfect(); //判斷數據成員 n 是否爲完全數。若一個正整數 n 的所有小於 n 的因子之和等於n,
	//則稱 n 爲完全數,  如 6=1+2+3 是完全數。 
	bool isReverse(int x);//判斷形式參數 x是否爲數據成員 n 的逆向數(例 321 是 123 的逆向數)。 

	bool isDaffodil(int x); //判斷形式參數 x 是否是水仙花數。水仙花數的各位數字立方和等於該數,
	//如 153=1*1*1+5*5*5+3*3*3 
	void printDaffodils(); //顯示所有大於 1,且小於數據成員 n 的水仙花數; 
}; 

int main(void) 
{ 
	NaturalNumber nn; //定義類的一個實例(對象) 

	nn.setValue (6); 

	cout << nn.getValue() << (nn.isPrime()?"是":"不是") <<"素數" <<endl; 

	nn.setValue (37);  

	cout<< nn.getValue() <<(nn.isPrime()?"是":"不是") <<"素數" <<endl; 

	nn.setValue (84);  

	cout<< nn.getValue() <<"的因子有:"; 

	nn.printFactor();

	cout << endl; 

	nn.setValue (6);

	cout<< nn.getValue() <<(nn.isPerfect()?"是":"不是")<<"完全數" <<endl;

	nn.setValue (26);   

	cout<<nn.getValue()<<" 和"<<62<<" "<<((nn.isReverse(62))?"是":"不是") << "逆向數" <<endl; 

	nn.setValue (100000);

	cout << "所有大於0,且小於數據成員的水仙花數有:";

	nn.printDaffodils();   

	system ("pause");

	//隨着成員函數的實現,增加代碼以完成相關的測試。注意判斷類的成員函數需要測試是或否兩種情況……  
} 
//請在下面定義類中的各個成員函數 

void NaturalNumber::setValue (int x)
{
	if (x > 0)
	{
		n = x;
	}
}

int NaturalNumber::getValue()
{
	return n;
}

bool NaturalNumber::isPrime()
{
	for (int i = n / 2; i > 1; --i)
	{
		if (!(n % i))
		{
			return false;
		}
		else 
		{
			return true;
		}
	}
}

void NaturalNumber::printFactor()
{
	cout << n << '\t';

	for (int i = n / 2; i > 0; --i)
	{
		if (!(n % i))
		{
			cout << i << '\t';
		}
	}
}

bool NaturalNumber::isPerfect()
{
	int sum = 0;

	for (int i = n / 2; i > 0; --i)
	{
		if (!(n % i))
		{
			sum += i;
		}
	}
	if (sum == n)
	{
		return true;
	}
	else 
	{
		return false;
	}
}

bool NaturalNumber::isReverse(int x)
{
	int s = 0;

	for (;x != 0;)
	{
		s = s * 10 + x % 10; 

		x = x / 10;
	}
	if (s == n)
	{
		return true;
	}
	else 
	{
		return false;
	}
}

bool NaturalNumber::isDaffodil(int x)
{
	int s = 0, p = x;

	int m;  

	for (;p != 0;)  
	{  
		m = p % 10; 

		s = s + m * m * m; 

		p = p / 10;  
	} 
	if (s == x)
	{
		return true;
	}
	else 
	{
		return false;
	}
}

void NaturalNumber::printDaffodils()
{
	for(int i = 2; i < n; ++i) 
	{
		if(isDaffodil(i)) 
		{
			cout << i <<" ";
		}
	}
	cout << endl; 
}



 任務4:

#include <iostream>

using namespace std;

class Salary   
{
public:    

	void set_salarys( ); 

	void set_number( );

	void add_salarys (int x); 

	void sort_salarys();

	void show_salarys( );  

private:   

	double *p; 

	int number;  
};


int main( )
{
	Salary s; 

	s.set_number( );

	s.set_salarys( );    

	s.add_salarys(250);  

	s.sort_salarys();	 

	s.show_salarys( ); 

	system("pause");

	return 0;
}

void Salary::set_number( )
{
	int m = 0;

	cin >> m;

	number = m;

	p = new double [m];
}

void Salary::set_salarys( )    
{
	for (int x, i = number - 1; i >= 0; --i) 
	{
		cin >> x;

		*(p + i) = x;
	}
}

void Salary::add_salarys(int x)  
{
	for (int i = 0; i < number; ++i)
	{
		*(p + i) += x;
	}
}

void Salary::sort_salarys()  
{
	int j;

	double t;

	for (int i = 0; i < number - 1; ++i)
	{
		for(int j = 0; j < number - i - 1;++j)
		{
			if (*(p + j) < *(p + j + 1))
			{
				t = *(p + j);

				*(p + j) = *(p + j + 1);

				*(p + j + 1) = t;
			}
		}
	}
}

void Salary::show_salarys( )   
{
	for (int i=0; i < number; ++i)
	{
		cout << *(p + i) << " ";
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章