第四周 项目4---正整数
来源:互联网 发布:大胃王密子君催吐 知乎 编辑:程序博客网 时间:2024/05/16 03:48
【项目4 - 正整数类】设计一个“正整数”类,并通过一系列的成员函数对其性质进行做出判断或列出相关联的数值。下面给出类声明,请实现各成员函数。另外,模仿已经给出的main()函数,完成你所设计的各个成员函数的测试。如果时间有限,不必实现所有成员函数。[cpp] view plaincopyprint?#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()<<”的因子有:”; printFactor(); //随着成员函数的实现,增加代码以完成相关的测试。注意判断类的成员函数需要测试是或否两种情况…… } //请在下面定义类中的各个成员函数 * * 程序的版权和版本声明部分 * Copyright (c)2013, 烟台大学计算机学院学生 * All rightsreserved. * 文件名称: object.cpp * 作 者: 袁静 * 完成日期:2013年3月23日 * 版本号: v1.0 * 输入描述:无 * 问题描述: 程序输出: #include<iostream>using namespace std;class NaturalNumber{private:int n; public:void setValue (int x);//置数据成员n的值,要求判断是否是正整数int getValue(); //返回私有数据成员n的值bool isPrime(); //判断数据成员n是否为素数,是返回true,否则返回falsevoid 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*3void 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()<<”的因子有:”;printFactor();//随着成员函数的实现,增加代码以完成相关的测试。注意判断类的成员函数需要测试是或否两种情况……}//请在下面定义类中的各个成员函数#include <iostream>#include <cmath>using namespace std;class NaturalNumber{private: int n;public: void set_value(int x); int get_value(); //返回私有成员的值 bool is_prime(); void printFactor(); bool is_perfect(); bool is_Daffodil(int x); void printDaffodils();};void NaturalNumber::printDaffodils() //显示所有大于1,且小于数据成员n的水仙花数;{ int z; for(z=2;z<n;z++) if(is_Daffodil(z)) cout<<z<<" "; cout<<endl;}bool NaturalNumber::is_Daffodil(int x ){ bool daffodil=true; int gw,sw,bw; bw=n/100; sw=(n%100)/10; gw=n%10; if(n!=sw*sw*sw+bw*bw*bw+gw*gw*gw) daffodil=false; return daffodil; }bool NaturalNumber::is_perfect(){ bool prime=true; int k,sum=1; for(k=2;k<n;++k){ if(n%k==0) prime=false; break; } prime=true; return prime;}void NaturalNumber::set_value(int x){ n=x;}int NaturalNumber::get_value(){ return n;}bool NaturalNumber::is_prime(){ bool prime=true; int i,m; m=sqrt(n); for(i=2;i<=m;++i) if(n%i==0){ prime=false; break; } return prime;}void NaturalNumber::printFactor(){ int j ,x ; for(j=1;j<=n;++j) if(n%j==0) cout<<j<<" "; cout<<endl;}int main(){ int x; NaturalNumber num; num.set_value(7); cout<<num.get_value()<<" :"<<(num.is_prime()?"是":"不是")<<"素数"<<endl; num.set_value(9); cout<<num.get_value()<<" :"<<(num.is_prime()?"是":"不是")<<"素数"<<endl; cout<<" "<<endl; cout<<num.get_value()<<"的因子有:"; cout<<" "<<endl; num.printFactor(); cout<<num.get_value()<<(num.is_perfect()?"是":"不是")<<"完全数"<<endl; cout<<" "<<endl; num.set_value(111); cout<<" "<<endl; cout<<num.get_value()<<(num.is_Daffodil( x)?"是":"不是")<<"水仙花数"<<endl; num.set_value(153); cout<<" "<<endl; cout<<num.get_value()<<(num.is_Daffodil( x)?"是":"不是")<<"水仙花数"<<endl; cout<<" "<<endl; num.set_value(333); cout<<"所有大于1,而且小于"<<num.get_value()<<"的水仙花数为:"; num.printDaffodils(); return 0;} 亲们,为什么此程序不能输出,“大于一,而且小于n”的所有水仙花数 。num.printDaffodils()是否有问题