第四周 任务三
来源:互联网 发布:淘宝hd登录接口不存在 编辑:程序博客网 时间:2024/04/30 06:34
#include<iostream>
#include<Cmath>
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;
nn.setValue (37);
cout<<nn.getValue()<<(nn.isPrime()?"是":"不是")<<"素数" <<endl;
nn.setValue (84);
cout<<nn.getValue()<<"的因子有:";
nn.printFactor();
nn.setValue (6);
cout<<nn.getValue()<<(nn.isPerfect()?"是":"不是")<<"完数" <<endl;
nn.setValue (8);
cout<<nn.getValue()<<(nn.isPerfect()?"是":"不是")<<"完数" <<endl;
nn.setValue (456);
cout<<"654"<<(nn.isReverse(654)?"是":"不是")<<nn.getValue()<<"逆向数" <<endl;
nn.setValue (677);
cout<<"766"<<(nn.isReverse(766)?"是":"不是")<<nn.getValue()<<"的逆向数" <<endl;
nn.setValue (153);
cout<<"153"<<(nn.isDaffodil(153)?"是":"不是")<<"水仙花数" <<endl;
nn.setValue (456);
cout<<"362"<<(nn.isDaffodil(362)?"是":"不是")<<"水仙花数" <<endl;
nn.setValue (6789);
nn.printDaffodils();
}
void NaturalNumber::setValue (int x)//置数据成员n 的值,要求判断是否是正整数
{
n=x;
}
int NaturalNumber::getValue() //返回私有数据成员n 的值
{
return n;
}
bool NaturalNumber::isPrime() //判断数据成员n 是否为素数,是返回true,否则返回false
{
bool prime=true;
for(int i=2;i<=sqrt(n);++i)
if(n%i==0)
{
prime=false;
break;
}
return prime;
}
void NaturalNumber::printFactor() //输出数据成员n 的所有因子,包括1 和n 自身
{
for(int i=1;i<=n;++i)
{
if(n%i==0)
cout<<i<<" ";
}
cout<<endl;
}
bool NaturalNumber::isPerfect() //判断数据成员n 是否为完全数。若一个正整数n 的所有小于n 的因子之和等于n, 则称n 为完全数, 如6=1+2+3 是完全数。
{
int s=0;
for(int i=1;i<n;++i)
{
if(n%i==0)
s=s+i;
}
if(s==n)
return true;
else
return false;
}
bool NaturalNumber::isReverse(int x)//判断形式参数x 是否为数据成员n 的逆向数(例321 是123 的逆向数)。
{
int a,s=0;
while(x>0)
{
a=x%10;
s=s*10+a;
x=x/10;
}
if(s==n)
return true;
else
return false;
}
bool NaturalNumber::isDaffodil(int x) //判断形式参数x 是否是水仙花数。水仙花数的各位数字立方和等于该数,如153=1*1*1+5*5*5+3*3*3
{
int a,s=0;
while(x>0)
{
a=x%10;
s=s+a*a*a;
x=x/10;
}
if(s==n)
return true;
else
return false;
}
void NaturalNumber::printDaffodils() //显示所有大于1,且小于数据成员n 的水仙花数;
{
int a,b,c,f;
cout<<"所有大于1,且小于数据成员"<<n<<"的水仙花数为:";
for(int i=2;i<n;++i)
{
if(i<100)
{
a=i/10;
b=i%10;
f=a*a*a+b*b*b;
if(i==f)
cout<<i<<" ";
}
else
{
a=i/100;
b=(i%100)/10;
c=(i%100)%10;
f=c*c*c+b*b*b+a*a*a;
if(i==f)
cout<<i<<" ";
}
}
cout<<endl;
}
- 第四周 任务三
- 第四周任务三
- 第四周 任务三
- 第四周任务三
- 第四周任务三
- 第四周任务三
- 第四周 任务三
- 第四周任务(三)
- 第四周实验报告 任务三
- 第四周实验报告(任务三)
- 第四周任务三(正整数类)
- 第四周实验报告 任务三
- C++程序设计实验报告(三十七)---第四周任务三
- 第四周任务三:设计一个“正整数”类
- 第四周任务三 各种数的判断
- 第四周任务三之定义正整数类
- 第四周任务三:设计一个“正整数”类
- 第四周任务三——正整数类
- 代表窗口的self,opener,parent的区别
- 第四周实验报告(任务二)
- [转载]source insight 相关问题
- Win32程序内部运行基本原理
- (转)让Qt应用程序跑在Android上
- 第四周 任务三
- 双缓冲消息队列-减少锁竞争
- struts2中s:select标签在freemarker中的使用
- 细谈C语言中的strcpy,strncpy,memcpy,memmove,memset函数
- 程序员技术练级攻略
- Linux CURL的安装和使用
- C++第四周【任务2】利用多文件组织项目
- http://jsonlint.com/
- 板子中交叉编译(curl工具的安装编译)