第四周 任务三
//设计一个“正整数”类,并通过一系列的成员函数对其性质进行做出判断或列出相关联的数值。下面给出类声明,请实现各成员函数。另外,模仿已经给出的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 (654);
cout << "和" << nn.getValue() << (nn.isReverse(345)? "是" : "不是") << "逆向数" << endl << endl;
cout << (nn.isDaffodil(153)? "是" : "不是") << "水仙花数" << endl << endl;
cout << (nn.isDaffodil(369)? "是" : "不是") << "水仙花数" << endl << endl;
nn.setValue (1000);
nn.printDaffodils();
cout << endl;
}
//请在下面定义类中的各个成员函数
void NaturalNumber :: setValue (int x) //置数据成员n的值,要求判断是否是正整数
{
if(x > 0)
{
cout << x << "是正整数" << endl ;
}
else
{
cout << x << "不是正整数" << endl ;
}
n = x;
}
int NaturalNumber :: getValue() //返回私有数据成员n的值
{
return n;
}
bool NaturalNumber :: isPrime() //判断数据成员n是否为素数,是返回true,否则返回false
{
for(int i = 2; i < n; ++i)
{
if(n % i == 0)
{
return false;
break;
}
}
return true;
}
void NaturalNumber :: printFactor() //输出数据成员n的所有因子,包括1和n自身
{
for(int i = 1; i <= n; ++i)
{
if(n % i == 0)
{
cout << i << '\t';
}
}
}
bool NaturalNumber :: isPerfect() //判断数据成员n是否为完全数。若一个正整数n的所有小于n的因子之和等于n, 则称n为完全数, 如6=1+2+3是完全数。
{
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)//判断形式参数x是否为数据成员n的逆向数(例321是123的逆向数)。
{
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是否是水仙花数。水仙花数的各位数字立方和等于该数,如153=1*1*1+5*5*5+3*3*3
{
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;
}
还没有评论,来说两句吧...