“第四届中国云计算大会”5月相约北京!
免费下载《SKC易云解决方案》
下载频道3月双倍下载分返还活动震撼出击!
优快云 2011年度十大风云博客专栏评选火爆进行中! 优快云社区3月技术大分享百本IT图书等你拿!
优快云 2011年度十大风云博客专栏评选火爆进行中! 优快云社区3月技术大分享百本IT图书等你拿!
- #include <iostream>
- #include <cmath>
- using namespace std;
- class NuturalNumber
- {
- private:
- int n;
- public:
- void setValue(int x);
- int getValue();
- bool isPrime();
- void printFactor();
- bool isPerfect();
- bool isReverse(int x);
- bool isDaffodil(int x);
- void printDaffodils();
- };
- void main()
- {
- NuturalNumber 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(123);
- cout<<nn.getValue()<<(nn.isReverse(321)?"是":"不是")<<"逆向数" <<endl;
- cout<<"153"<<(nn.isDaffodil(153)?"是":"不是")<<"水仙花数" <<endl;
- nn.setValue(1000);
- nn.getValue();
- nn.printDaffodils();
- }
- void NuturalNumber::setValue(int x)
- {
- if(x>0 && x%1==0)
- {
- n=x;
- }
- }
- int NuturalNumber::getValue()
- {
- return n;
- }
- bool NuturalNumber::isPrime()
- {
- int i;
- for(i=2;i<=sqrt(n);i++)
- {
- if(n%i==0)
- return false;
- }
- return true;
- }
- void NuturalNumber::printFactor()
- {
- int i;
- for (i=1;i<=n;i++)
- {
- if(n%i==0)
- cout<<i<<" ";
- }
- }
- bool NuturalNumber::isPerfect()
- {
- int i,s=0;
- for(i=1;i<n;i++)
- {
- if(n%i==0)
- s=s+i;
- }
- if(s==n)
- return true;
- else
- return false;
- }
- bool NuturalNumber::isReverse(int x)
- {
- 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 NuturalNumber::isDaffodil(int x)
- {
- n=x;
- 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 NuturalNumber::printDaffodils()
- {
- for(int i=2;i<n;i++)
- {
- if(isDaffodil(i))
- cout<<i<<" ";
- }
- cout<<endl;
- return;
- }
问题请教:老师我的最后一个功能为什么不行呀,求解答!谢谢您!
暂无评论