1.正整数类
- #include<iostream>
- using namespace std;
- class NaturalNumber
- {
- private:
- int n;
- public:
- void setValue (int x);
- int getValue();
- bool isPrime();
- void printFactor();
- bool isPerfect();
- bool isReverse(int x);
- bool isDaffodil(int x);
- void printDaffodils();
- };
- void NaturalNumber::setValue(int x)
- {
- n=x;
- while(n<0)
- {
- cout<<"输入一个正整数:"<<endl;
- cin>>n;
- }
- }
- int NaturalNumber::getValue()
- {
- return n;
- }
- bool NaturalNumber::isPrime()
- {
- int i,a=0;
- for(i=2;i<=(n+1)/2;i++)
- {
- if(n%i==0)
- a++;
- if(a==0)
- return true;
- else
- return false;
- }
- }
- void NaturalNumber::printFactor()
- {
- int i;
- for(i=1;i<=n;i++)
- {
- if(n%i==0)
- cout<<i<<" ";
- }
- cout<<endl;
- }
- bool NaturalNumber::isPerfect()
- {
- int i,s=0;
- for(i=1;i<n;i++)
- {
- if(n%i==0)
- s+=i;
- }
- if(s==n)
- return true;
- else
- return false;
- }
- bool NaturalNumber::isReverse(int x)
- {
- int a[10],i,s=1,k,c=1;
- for(i=0;n!=0;i++)
- {
- a[i]=n%10;
- n=n/10;
- c=c*10;
- }
- k=i;
- c=c/10;
- for(i=0;i<k;i++)
- {
- s=a[i]*c;
- c=c/10;
- }
- if(s==x)
- return true;
- else
- return false;
- }
- bool NaturalNumber::isDaffodil(int x) //判断形式参数x是否是水仙花数。水仙花数的各位数字立方和等于该数,如153=1*1*1+5*5*5+3*3*3
- {
- int m,a[10],i,j,s=0;
- m=x;
- for(i=1;n!=0;i++)
- {
- a[i]=n%10;
- n=n/10;
- }
- j=i;
- for(i=1;i<=j;i++)
- {
- s=s+a[i]*a[i]*a[i];
- }
- if(s==x)
- return true;
- else
- return false;
- }
- void NaturalNumber::printDaffodils()//显示所有大于1,且小于数据成员n的水仙花数;
- {
- int i;
- for(i=2;i<n;i++)
- {
- int m;
- m=i;
- int a[10],j,k,s=0;
- for(j=1;i!=0;j++)
- {
- a[j]=i%10;
- i=i/10;
- }
- k=j;
- for(j=1;j<=k;j++)
- {
- s=s+a[j]*a[j]*a[j];
- }
- if(s==m)
- cout<<m<<" ";
- }
- cout<<endl;
- }
- 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;
- int z;
- nn.setValue(123);
- z=nn.getValue();
- cout<<(nn.isReverse(321)?"是":"不是")<<"n="<<z<<"的逆向数"<<endl;
- cout<<(nn.isDaffodil(153)?"是":"不是")<<"水仙花树"<<endl;
- nn.setValue(1000);
- cout<<"大于1且小于数据成员"<<"n="<<nn.getValue()<<"的水仙花数:";
- nn.printDaffodils();
- }
2.分数类
#include<iostream>
#include<string>
using namespace std;
class CFraction
{
private:
int nume; // 分子
int deno; // 分母
public:
CFraction(int nu=0,int de=1); //构造函数,初始化用
void set(int nu=0,int de=1); //置值,改变值时用
void input(); //按照"nu/de"的格式,如"5/2"的形式输入
void simplify(); //化简(使分子分母没有公因子)
void amplify(int n); //放大n倍,如2/3放大5倍为10/3
void output(int style=0); //输出:以8/6为例,style为0时,原样输出8/6;
//style为1时,输出化简后形式4/3;
//style为2时,输出1(1/3)形式,表示一又三分之一;
//style为3时,用小数形式输出,如1.3333;
//默认方式0
};
CFraction::CFraction(int nu,int de)
{
nume=nu;
deno=de;
}
void CFraction::set(int nu,int de)
{
nume=nu;
deno=de;
}
void CFraction::input()
{
char a;
cin>>nume>>a>>deno;
}
void CFraction::output(int style)
{
int b;
float c;
if(style==0)
cout<<nume<<"/"<<deno;
else if(style==1)
{
simplify();
cout<<nume<<"/"<<deno;
}
else if(style==2)
{
simplify();
b=nume/deno;
cout<<b<<"("<<nume<<"/"<<deno<<")";
}
else if(style==3)
{
c=nume*1.0/deno;
cout<<c;
}
}
void CFraction::simplify()
{
int i,max;
if(nume>deno)
max=nume;
else max=deno;
for(i=2;i<max;i++)
if(nume%i==0&&deno%i==0)
{
nume/=i;deno/=i;
}
}
void CFraction::amplify(int n)//放大n倍,如2/3放大5倍为10/3
{
nume*=n;
simplify();
}
int main()
{
CFraction c1(0,1);
cout<<"关于c1:"<<endl<<"原样:";
c1.output(0);
cout<<endl<<"改变c1:"<<endl<<"原样:";
c1.set(2,7);
cout<<"输入c1:"<<endl<<"输入分数(m/n):";
c1.input();
cout<<"原样:";
c1.output(0);
CFraction c2(8,6);
cout<<endl<<"关于c2:"<<endl<<"原样:";
c2.output(0);
cout<<endl<<"化简形式:";
c2.output(1);
cout<<endl<<"带分数形式:";
c2.output(2);
cout<<endl<<"近似值:";
c2.output(3);
cout<<endl<<"原样:";
c2.set(8,6);
c2.output(0);
cout<<endl<<"将c2化简:"<<endl<<"原样:";
c2.output(1);
cout<<endl<<"将c2放大倍:"<<endl<<"原样:";
c2.amplify(5);
c2.output(0);
cout<<endl<<"化简形式:";
c2.output(1);
cout<<endl;
return 0;
}