现在的位置: 首页 > 综合 > 正文

第四周实验报告3

2014年01月23日 ⁄ 综合 ⁄ 共 2084字 ⁄ 字号 评论关闭

 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生 
* All rights reserved.
* 文件名称: class time
* 作 者: 于昊
* 完成日期: 2012 年 03 月 12 日
* 版 本 号: 1.0
* 对任务及求解方法的描述部分
* 输入描述:输入时间
* 问题描述:
* 程序输出:
* 问题分析:

* 算法设计:……

#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;  
  
    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(321);  
    cout << "123"<< (nn.isReverse(123)?"是":"不是")<< nn.getValue()<<"的"<<"逆向数" <<endl;  
    cout  <<"165"<< (nn.isReverse(165)?"是":"不是")<<nn.getValue()<<"的"<<"逆向数" <<endl;  
  
  
    nn.setValue(153);  
    cout << "123" << (nn.isDaffodil(123)?"是":"不是")<<"水仙花数" <<endl;  
    cout << "153" << (nn.isDaffodil(153)?"是":"不是")<<"水仙花数" <<endl;  
    system("pause");  
  
   
  
    //随着成员函数的实现,增加代码以完成相关的测试。注意判断类的成员函数需要测试是或否两种情况……      
}  
  
void NaturalNumber::setValue (int x)  
{  
    n = x;  
}  
int NaturalNumber::getValue()  
{  
    return n;  
}  
  
bool NaturalNumber::isPrime()  
{  
    int s, m = 0, i;  
  
    for(i = 2; i < n; i++)  
    {  
        s = n % i;  
        if(s == 0)  
        {  
            m++;  
        }  
    }  
    if(m != 0) return true;  
    else       return false;  
}  
  
void NaturalNumber::printFactor()  
{  
    int i,s;  
  
    for (i = 1; i <= n; i++)  
    {  
        s = n % i;  
        if(s == 0)  
        {  
            cout << i << " ";  
        }  
    }  
}  
  
bool NaturalNumber::isPerfect()  
{  
    int i,s,m = 0;  
  
    for (i = 1; i < n; i++)  
    {  
        s = n % i;  
        if(s == 0)  
        {  
            m = m + i;  
        }  
    }  
    if(n == m) return true;  
    else       return false;  
}  
  
bool NaturalNumber::isReverse(int x)  
{  
    int a,b,c,m;  
  
    if(x / 100 == 0)  
    {  
        b = x / 10;  
        c = x % 10;  
        m = c * 10 + b;  
    }  
    else   
    {  
        a = x / 100;  
        c = x % 10;  
        b = (x / 10) % 10;  
        m = c * 100 + b * 10 + a;  
    }  
    if(m == n)   return true;  
    else         return false;  
}  
  
bool NaturalNumber::isDaffodil(int x)  
{  
    int a,b,c,m;  
  
    if(x / 100 == 0)  
    {  
        b = x / 10;  
        c = x % 10;  
        m = b * b * b + c * c * c;  
    }  
    else   
    {  
        a = x / 100;  
        c = x % 10;  
        b = (x / 10) % 10;  
        m = a * a * a + b * b * b + c * c * c;  
    }  
    if(m == n)  return true;  
    else        return false;  
}  
【上篇】
【下篇】

抱歉!评论已关闭.