实验报告 4--3

 

第四周任务三

10人阅读 评论(0) 收藏 举报
  1. #include <iostream>           
  2. #include <cmath>          
  3. using namespace std;          
  4. class NuturalNumber        
  5. {          
  6. private:        
  7.  int n;        
  8. public:        
  9.  void setValue(int x);        
  10.  int getValue();        
  11.  bool isPrime();        
  12.  void printFactor();        
  13.  bool isPerfect();        
  14.  bool isReverse(int x);        
  15.  bool isDaffodil(int x);        
  16.  void printDaffodils();        
  17. };        
  18. void main()        
  19. {        
  20.      NuturalNumber nn; //定义类的一个实例(对象)           
  21.     nn.setValue(6);         
  22.     cout<<nn.getValue()<<(nn.isPrime()?"是":"不是")<<"素数"<<endl;         
  23.     nn.setValue (37);          
  24.     cout<<nn.getValue()<<(nn.isPrime()?"是":"不是")<<"素数" <<endl;         
  25.     nn.setValue (84);          
  26.     cout<<nn.getValue()<<"的因子有:";         
  27.     nn.printFactor();  
  28.     cout<<endl;  
  29.     nn.setValue(6);      
  30.     cout<<nn.getValue()<<(nn.isPerfect()?"是":"不是")<<"完全数" <<endl;     
  31.     nn.setValue(123);      
  32.     cout<<nn.getValue()<<(nn.isReverse(321)?"是":"不是")<<"逆向数" <<endl;     
  33.     cout<<"153"<<(nn.isDaffodil(153)?"是":"不是")<<"水仙花数" <<endl;  
  34.     nn.setValue(1000);  
  35.     nn.getValue();  
  36.     nn.printDaffodils();    
  37. }        
  38. void NuturalNumber::setValue(int x)        
  39. {        
  40.  if(x>0 && x%1==0)        
  41.  {        
  42.   n=x;        
  43.  }        
  44. }        
  45. int NuturalNumber::getValue()        
  46. {        
  47.  return n;        
  48. }        
  49. bool NuturalNumber::isPrime()        
  50. {        
  51.     int i;        
  52.     for(i=2;i<=sqrt(n);i++)        
  53.     {        
  54.         if(n%i==0)       
  55.             return false;        
  56.     }        
  57.     return true;        
  58. }        
  59. void NuturalNumber::printFactor()        
  60. {        
  61.     int i;        
  62.     for (i=1;i<=n;i++)        
  63.     {        
  64.         if(n%i==0)        
  65.             cout<<i<<" ";        
  66.     }        
  67. }        
  68. bool  NuturalNumber::isPerfect()        
  69. {        
  70.     int i,s=0;        
  71.     for(i=1;i<n;i++)        
  72.     {        
  73.         if(n%i==0)        
  74.             s=s+i;        
  75.     }        
  76.     if(s==n)        
  77.         return true;        
  78.     else        
  79.         return false;        
  80. }        
  81. bool NuturalNumber::isReverse(int x)       
  82. {       
  83.     int a,s=0;      
  84.     while (x>0)      
  85.     {      
  86.         a=x%10;      
  87.         s=s*10+a;      
  88.         x=x/10;      
  89.     }      
  90.     if(s==n)      
  91.         return true;      
  92.     else      
  93.         return false;      
  94. }       
  95.        
  96. bool NuturalNumber::isDaffodil(int x)       
  97. {     
  98.     n=x;  
  99.     int a,s=0;      
  100.     while (x>0)      
  101.     {      
  102.         a=x%10;      
  103.         s=s+a*a*a;      
  104.         x=x/10;      
  105.     }      
  106.     if(s==n)      
  107.         return true;      
  108.     else      
  109.         return false;      
  110. }      
  111. void NuturalNumber::printDaffodils()      
  112. {      
  113.    for(int i=2;i<n;i++)  
  114.    {  
  115.        if(isDaffodil(i))      
  116.           cout<<i<<" ";  
  117.    }  
  118.         cout<<endl;  
  119.         return;  
  120. }      


 

问题请教:老师我的最后一个功能为什么不行呀,求解答!谢谢您!

 

 

查看评论

  暂无评论

发表评论
  • 用 户 名:
  • zhl7191111
  •   
* 以上用户言论只代表其个人观点,不代表优快云网站的观点或立场
  • 个人资料

  • wangmingxing123
    • 访问:134次
    • 积分:100分
    • 排名:千里之外
    • 原创:9篇
    • 转载:0篇
    • 译文:1篇
    • 评论:0条
  • 文章存档
  • 推荐文章
    公司简介| 招贤纳士| 广告服务| 银行汇款帐号| 联系方式| 版权声明| 法律顾问| 问题报告
    北京创新乐知信息技术有限公司 版权所有, 京 ICP 证 070598 号
    世纪乐知(北京)网络技术有限公司 提供技术支持
    江苏乐知网络技术有限公司 提供商务支持
    Email:webmaster@youkuaiyun.com
    Copyright © 1999-2012, youkuaiyun.com, All Rights Reserved
    GongshangLogo
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值