//設計一個“正整數”類,並通過一系列的成員函數對其性質進行做出判斷或列出相關聯的數值。下面給出類聲明,請實現各成員函數。另外,模仿已經給出的main()函數,完成你所設計的各個成員函數的測試。
#include<iostream>
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的水仙花數;
};
void main(void)
{
NaturalNumber nn; //定義類的一個實例(對象)
nn.setValue (6);
cout << nn.getValue() << (nn.isPrime()? "是" : "不是") << "素數" << endl << endl;
nn.setValue (37);
cout << nn.getValue() << (nn.isPrime()? "是" : "不是") << "素數" << endl << endl;
nn.setValue (84);
cout << nn.getValue() << "的因子有:" ;
nn.printFactor();
cout << endl << endl;
nn.setValue (321);
cout << nn.getValue() << (nn.isPerfect()? "是" : "不是") << "完全數" << endl << endl;
nn.setValue (28);
cout << nn.getValue() << (nn.isPerfect()? "是" : "不是") << "完全數" << endl << endl;
nn.setValue (123);
cout << "和" << nn.getValue() << (nn.isReverse(321)? "是" : "不是") << "逆向數" << endl << endl;
nn.setValue (789);
cout << "和" << nn.getValue() << (nn.isReverse(987)? "是" : "不是") << "逆向數" << endl << endl;
cout << (nn.isDaffodil(157)? "是" : "不是") << "水仙花數" << endl << endl;
cout << (nn.isDaffodil(365)? "是" : "不是") << "水仙花數" << endl << endl;
nn.setValue (1000);
nn.printDaffodils();
cout << endl;
}
//請在下面定義類中的各個成員函數
void NaturalNumber :: setValue (int x) //判斷是否是正整數
{
if(x > 0)
{
cout << x << "是正整數" << endl ;
}
else
{
cout << x << "不是正整數" << endl ;
}
n = x;
}
int NaturalNumber :: getValue() //返回私有數據成員n的值
{
return n;
}
bool NaturalNumber :: isPrime() //是否爲素數
{
for(int i = 2; i < n; ++i)
{
if(n % i == 0)
{
return false;
break;
}
}
return true;
}
void NaturalNumber :: printFactor() //輸出數據成員n的所有因子
{
for(int i = 1; i <= n; ++i)
{
if(n % i == 0)
{
cout << i << '\t';
}
}
}
bool NaturalNumber :: isPerfect() //判斷是否爲完全數
{
int m = 0;
for(int i = 1; i < n; ++i)
{
if(n % i == 0)
{
m = m + i;
}
}
if(n == m)
{
return true;
}
else
{
return false;
}
}
bool NaturalNumber :: isReverse(int x)//判斷是否爲數據成員n的逆向數。
{
int a, b, s = 0;
b = x;
while(b != 0)
{
a = b % 10;
b = b / 10;
s = s * 10 + a;
}
cout << x;
if(s == n)
{
return true;
}
else
{
return false;
}
}
bool NaturalNumber :: isDaffodil(int x) //判斷x是否是水仙花數。
{
cout << x;
int m, i, j, s;
m = x % 10;
i = x / 100;
j = (x % 100) / 10;
s = i * i * i + j * j * j + m * m * m;
if(s == x)
{
return true;
}
else
{
return false;
}
}
void NaturalNumber :: printDaffodils() //顯示所有大於1,且小於數據成員n的水仙花數
{
int m, i, j, s, h, num = 0;
if(n <= 100)
{
cout << "沒有水仙花數" << endl;
exit(0);
}
for(h = 100; h < n; ++h)
{
m = h % 10;
i = h / 100;
j = (h % 100) / 10;
s = i * i * i + j * j * j + m * m * m;
if(s == h)
{
cout << h << '\t';
++num;
}
}
if(num == 0) cout << "沒有水仙花數" << endl;
}
第四周作業2
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.