c++第五次实验

  1. /*  
  2. * 文件名称:Ex5-2.cpp  
  3. * 作    者:徐浩宁 
  4. * 完成日期:2017 年 4 月 24 日  
  5. * 版 本 号:v1.0  
  6. * 对任务及求解方法的描述部分: 设计一个“正整数”类,并通过一系列的成员函数对其性质进行做出判断或列出相关联的数值。下面给出类声明,请实现各成员函数。另外,模仿已经给出的main()函数,完成你所设计的各个成员函数的测试
  7. * 输入描述:  
  8. * 问题描述:
  9. * 程序输出:  
  10. * 问题分析:
  11. * 算法设计: 
  12. #include<iostream>  
    using namespace std;  
    class NaturalNumber  
    {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 print Daffodils();  
    };  
      
    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()<<”的因子有:”;  
        printFactor();  
      
       
    }  
  13. </pre><br>  
    <pre></pre>  
    <pre class="plain" name="code">
    {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();
    };</pre><pre class="cpp" name="code">
    <pre class="cpp" name="code">#include<iostream>  
    #include <Cmath>  
    #include "NaturalNumber.h"  
    using namespace std;  
    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();  
        system("PAUSE");  
          
    }</pre><pre class="cpp" name="code">
    <pre class="cpp" name="code">#include<iostream>  
    #include <Cmath>  
    #include "NaturalNumber.h"  
    using namespace std;  
    void NaturalNumber::setValue (int x)  
    {  
        cout << "请输入一个数据成员的值:";  
          
        cout<< x << endl;  
          
        if (x > 0)  
        {  
            cout << "数据为正整数!"<<endl;  
              
            n = x;  
              
              
        }  
        else  
        {  
            cout << "数据不是正整数!请重新输入:"<< endl;  
        }  
    }  
    int NaturalNumber::getValue()  
    {  
        return n;  
    }  
    bool NaturalNumber::isPrime()  
    {  
        bool pri = true;  
        for (int i = 2;i <= sqrt(x);i++)  
        {  
            if( n % i == 0)  
            {  
                pri = false;  
                  
                break;  
            }  
        }  
        return pri;  
    }  
    void NaturalNumber::printFactor()  
    {  
        for(int i = 1;i <= n;i++)  
        {  
            if( n % i == 0)  
            {  
                cout << i << "  ";  
            }  
        }  
        cout << endl;  
    }  
    bool NaturalNumber::isPerfect()  
    {  
        int s = 0;  
          
        for(int i = 1;i < n;i++)  
        {  
            if( n % i == 0)  
            {  
                s = s + i;  
                  
            }  
        }  
        if(n == s)  
        {  
            cout << "n为完全数!"<< endl;  
              
              
        }  
        else  
        {  
            cout << "n不是完全数!"<< endl;  
              
        }  
        return 0;  
          
    }  
    bool NaturalNumber::isReverse(int x)  
    {  
        int t,s = 0;  
          
        while(x > 0)  
        {  
            t = x % 10;  
              
            s = s + t;  
              
            x = x / 10;  
        }  
        if (n == x)  
        {  
            cout << "形式参数x是数据成员n的逆向数"<< endl;  
              
        }  
        else  
        {  
            cout << "形式参数x不是数据n的逆向数"<< endl;  
              
        }  
        return 0;  
          
    }  
    bool NaturalNumber::isDaffodil(int x)  
    {  
        int t,s = 0;  
          
        while(x > 0)  
        {  
            t = x % 10;  
              
            s = s + t * t * t;  
              
            x = x / 10;  
        }  
        if (n == s)  
        {  
            cout << "形式参数x是水仙花数"<< endl;  
              
        }  
        else  
        {  
            cout << "形式参数x不是水仙花数"<< endl;  
              
        }  
        return 0;  
          
    }  
    void NaturalNumber::printDaffodils()  
    {  
        int i,t,s = 0;  
        for(i = 1;i < n;i++)  
        {  
            while(i > 0)  
            {  
                t = i % 10;  
                  
                s = s + t * t *t;  
                  
                i = i / 10;  
            }  
            if(s == i)  
            {  
                cout << "1--n的水仙花数有:"<< i << " ";  
            }  
        }  
    }  
    </pre><pre class="cpp" name="code"><img src="http://hi.csdn.net/attachment/201203/14/0_13317173344u24.gif" alt="">  
    </pre><br>  
       
    <pre></pre>  
         
    </pre>
  14. 二.运算结果

  15. 三.心得体会
  16. 四.知识点总结
  17. 无  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值