第四周实验报告3(判断一个数是什么数)

来源:互联网 发布:手机淘宝怎么销户 编辑:程序博客网 时间:2024/04/19 20:34
//NaturalNumber.h  #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,则称n为完全数bool isReverse(int x);//判断形式参数x是否为数据成员n的逆向数bool isDaffodil(int x);//判断形式参数x是否是水仙花数。水仙花数的各位数字的立方和等于该数void printDaffodils();//显示所有大于1,且小于数据成员n的水仙花数};//NaturalNumber.cpp#include<iostream>  #include"NaturalNumber.h"  using namespace std;void NaturalNumber::setValue (int x)  {     if(x>0 && x%1==0)         {          n=x;         }    }  int NaturalNumber::getValue()  //返回私有数据成员n 的值 {      return n;  }  bool NaturalNumber::isPrime() //判断数据成员n 是否为素数,是返回true,否则返回false  {    if(n<=1)          return false;      for(int i=2;i<n;++i)      {                    if(n%2==0)              return false;      }      return true;  }void NaturalNumber::printFactor() //输出数据成员n 的所有因子,包括1 和n 自身{         for ( int i = 1; i <= n; i++)      {                    if( n % i == 0)          {              cout << i << " ";          }      }  } bool NaturalNumber::isPerfect()  //判断数据成员n 是否为完全数。若一个正整数n 的所有小于n 的因子之和等于n, 则称n 为完全数, 如6=1+2+3 是完全数。{int m=0;      for(int i=1;i<n;++i)      {                            if(n%i==0)              m=m+i;      }      if(m==n)  {   return true;}    return false;  }bool NaturalNumber::isReverse(int x)//判断形式参数x 是否为数据成员n 的逆向数(例321 是123 的逆向数)。  {      int a,s=0;      while(x>0)      {          a=x%10;          s=s*10+a;          x=x/10;      }      if(s==n)          return true;      return false;  }  bool NaturalNumber::isDaffodil(int x) //判断形式参数x 是否是水仙花数。水仙花数的各位数字立方和等于该数,如153=1*1*1+5*5*5+3*3*3  {      int a,s=0;      while(x>0)      {          a=x%10;          s=s+a*a*a;          x=x/10;      }      if(s==n)          return true;      return false;  }  void NaturalNumber::printDaffodils() //显示所有大于1,且小于数据成员n 的水仙花数;  {      int a,b,c,d;      cout<<endl;      cout<<"所有大于1,且小于数据成员"<<n<<"的水仙花数为:";      for(int i=11;i<n;++i)      {          if(i<100)  //两位数时的求水仙花数的方法        {              a=i/10;              b=i%10;              d=a*a*a+b*b*b;              if(i==d)                  cout<<i<<'\t';                        }          else  //三位数时求水仙花数的方法        {              a=i/100;              b=(i%100)/10;              c=(i%100)%10;              d=c*c*c+b*b*b+a*a*a;              if(i==d)                  cout<<i<<'\t';          }      }      cout<<endl;  }  //main.cpp#include<iostream>#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();nn.setValue (1314);      cout << nn.getValue() << (nn.isPerfect()? "是" : "不是") << "完全数" << endl << endl;         nn.setValue (99);      cout << nn.getValue() << (nn.isPerfect()? "是" : "不是") << "完全数" << endl << endl;            nn.setValue (123);      cout <<"321"<< "和" << nn.getValue()  << (nn.isReverse(321)? "是" : "不是") << "逆向数" << endl << endl;        nn.setValue (158);      cout<<"815"<< "和" << nn.getValue() << (nn.isReverse(815)? "是" : "不是") << "逆向数" << endl << endl;        cout <<"153"<< (nn.isDaffodil(153)? "是" : "不是") << "水仙花数" << endl << endl;         cout <<"169"<< (nn.isDaffodil(169)? "是" : "不是") << "水仙花数" << endl << endl;        nn.setValue (1000);      nn.printDaffodils();      cout << endl;  system("PAUSE");  }          


上机感言:

虽然今晚上累点,但是复习了以前做过的程序,还是有些记得不扎实啊

	
				
		
原创粉丝点击