/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:设计一个“正整数”类,并通过一系列的成员函数对其性质进行做出判断或列出相关联的数值。
* 作 者: 雷恒鑫
* 完成日期: 2012年03 月12日
* 版 本 号:V1.2
* 对任务及求解方法的描述部分
* 输入描述: ...
* 问题描述: ...
* 程序输出: ...
*程序头部的注释结束*/
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 (6);
cout<<nn.getValue()<<(nn.isPerfect()?"是":"不是")<<"完数" <<endl;
nn.setValue (9);
cout<<nn.getValue()<<(nn.isPerfect()?"是":"不是")<<"完数" <<endl;
nn.setValue (321);
cout<<"123"<<(nn.isReverse(123)?"是":"不是")<<nn.getValue()<<"逆向数" <<endl;
nn.setValue (321);
cout<<"223"<<(nn.isReverse(223)?"是":"不是")<<nn.getValue()<<"的逆向数" <<endl;
nn.setValue (153);
cout<<"153"<<(nn.isDaffodil(153)?"是":"不是")<<"水仙花数" <<endl;
nn.setValue (456);
cout<<"456"<<(nn.isDaffodil(456)?"是":"不是")<<"水仙花数" <<endl;
nn.setValue (999);
nn.printDaffodils();
system("PAUSE");
}
NaturalNumber.cpp
#include<iostream>
#include"NaturalNumber.h"
void NaturalNumber::setValue (int x)//置数据成员n 的值,要求判断是否是正整数
{
n=x;
if(n>0)
{
cout<<n<<"是正整数。";
}
else
{
cout<<n<<"不是正整数。";
}
return;
}
int NaturalNumber::getValue() //返回私有数据成员n 的值
{
return n;
}
bool NaturalNumber::isPrime() //判断数据成员n 是否为素数,是返回true,否则返回false
{
int t=0;
if(n<=1)
return false;
for(int i=2;i<n;++i)
{
t=n%2;
if(t==0)
return false;
}
return true;
}
void NaturalNumber::printFactor() //输出数据成员n 的所有因子,包括1 和n 自身
{
int t;
for(int i=1;i<n;++i)
{
t=n%i;
if(t==0)
cout<<i<<" ";
}
cout<<endl;
return;
}
bool NaturalNumber::isPerfect() //判断数据成员n 是否为完全数。若一个正整数n 的所有小于n 的因子之和等于n, 则称n 为完全数, 如6=1+2+3 是完全数。
{
int t,m=0;
for(int i=1;i<n;++i)
{
t=n%i;
if(t==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,h;
cout<<endl;
cout<<"所有大于1,且小于数据成员"<<n<<"的水仙花数为:";
for(int i=11;i<n;++i)
{
if(i<100)
{
a=i/10;
b=i%10;
h=a*a*a+b*b*b;
if(i==h)
cout<<i<<'\t';
}
else
{
a=i/100;
b=(i%100)/10;
c=(i%100)%10;
h=c*c*c+b*b*b+a*a*a;
if(i==h)
cout<<i<<'\t';
}
}
cout<<endl;
}
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,否则返回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 的水仙花数;
};
运行结果:
经验积累:
1.我学会了如何用循环求逆向数和水仙花数。
2.对VS2008我更熟悉了。