Posted on 2011-11-26 13:38
hoshelly 阅读(435)
评论(0) 编辑 收藏 引用 所属分类:
C++
类的实现:
class CNumber
{
private:
int n;
public:
CNumber(int number)
{
n=number;
}
~CNumber(){ }
int isEven()
{
if(n%2==0)
return 1;
else
return 0;
}
int isOdd()
{
if(n%2!=0)
return 1;
else
return 0;
}
int isPrime()
{
int i;
if(n<1)
return 0;
for(i=2;i<=sqrt(n);i++)
if(n%i==0)
return 0;
else
return 1;
return 0;
}
bool isAPrime(int n)
{
for(int i=2;i<sqrt(n);i++)
if(n%i==0)
return false;
return true;
}
int isGoldBach()
{
int i;
int halfnum=n/2;
for(i=2;i<=halfnum;i++)
if(isAPrime(i)&&isAPrime(n-i))
return 1;
return 0;
}
void print()
{
if(isEven()) cout<<"This number is even."<<endl;
if(isOdd()) cout<<"This number is odd."<<endl;
if(isPrime()) cout<<"This number is prime."<<endl;
if(isGoldBach()) cout<<"This number is goldbach."<<endl;
}
};
主函数:
void main()
{
int num;
cout<<"Please enter one number:"<<endl;
cin>>num;
CNumber numb(num);
numb.print();
}