#include<iostream>
using namespace std;
int gcd(int,int);
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),表示一又三分之一;
//不给出参数和非1、2,认为是方式0
};
CFraction::CFraction(int nu,int de)
{
if(de!=0)
{
nume=nu;
deno=de;
}
else
{
cerr<<"初始化中发生错误,程序退出\n";
exit(0);
}
}
void CFraction::Set(int nu,int de)
{
if(deno!=0)
{
nume=nu;
deno=de;
}
}
void CFraction::input()
{
/*int nu,de;
char c='/';
while(1)
{
cout<<"请输入分数n/d:"<<endl;
cin>>nu>>de;
if(c!='/')
cout<<"输入格式不对,请重新输入!"<<endl;
else
if(de==0)
cout<<"分母不能为零"<<endl;
else
break;
}*/
cout<<nume<<"/"<<deno;
//nume=nu;
//deno=de;
}
//化简
void CFraction::Simplify()
{
int n=gcd(nume,deno);
nume=nume/n;
deno=deno/n;
}
int gcd(int m,int n)
{
int r;
if (m<n){r=m;m=n;n=r;}
while(r=m%n) // 求m,n的最大公约数 (原来求公约数可以这样求啊)
{
m=n;
n=r;
}
return n;
}
void CFraction::amplify(int n)
{
cout<<"放大"<<n<<"倍后为:"<<nume*n<<"/"<<deno<<endl;
}
void CFraction::output(int style)
{
int n,nu,de;
if(style==1)
{
n=gcd(nume,deno);
cout<<nume/n<<"/"<<deno/n<<endl;
}
else
if(style==2)
{
n=gcd(deno, nume);
nu=nume/n;
de=deno/n;
cout<<nu/de<<'('<<nu%de<<'/'<<de<<')'<<endl;
}
else
cout<<nume<<"/"<<deno<<endl;
}
int main()
{
CFraction cf;
cf.Set(24,72);
cf.input();
//cf.output(0);
cf.amplify(5);
cout<<"style(0)"<<endl;
cf.output(0);
cout<<"style(1):"<<endl;
cf.output(1);
cout<<"style(2):"<<endl;
cf.output(2);
return 0;
}
#include<iostream>
using namespace std;
int gcd(int,int);
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),表示一又三分之一;
//不给出参数和非1、2,认为是方式0
};
CFraction::CFraction(int nu,int de)
{
if(de!=0)
{
nume=nu;
deno=de;
}
else
{
cerr<<"初始化中发生错误,程序退出\n";
exit(0);
}
}
void CFraction::Set(int nu,int de)
{
if(deno!=0)
{
nume=nu;
deno=de;
}
}
void CFraction::input()
{
int nu,de;
char c='/';
while(1)
{
cout<<"请输入分数n/d:"<<endl;
cin>>nu>>de;
if(c!='/')
cout<<"输入格式不对,请重新输入!"<<endl;
else
if(de==0)
cout<<"分母不能为零"<<endl;
else
break;
}
cout<<nume<<"/"<<deno;
nume=nu;
deno=de;
}
//化简
void CFraction::Simplify()
{
int n=gcd(nume,deno);
nume=nume/n;
deno=deno/n;
}
int gcd(int m,int n)
{
int r;
if (m<n){r=m;m=n;n=r;}
while(r=m%n) // 求m,n的最大公约数 (原来求公约数可以这样求啊)
{
m=n;
n=r;
}
return n;
}
void CFraction::amplify(int n)
{
cout<<"放大"<<n<<"倍后为:"<<nume*n<<"/"<<deno<<endl;
}
void CFraction::output(int style)
{
int n,nu,de;
if(style==1)
{
n=gcd(nume,deno);
cout<<nume/n<<"/"<<deno/n<<endl;
}
else
if(style==2)
{
n=gcd(deno, nume);
nu=nume/n;
de=deno/n;
cout<<nu/de<<'('<<nu%de<<'/'<<de<<')'<<endl;
}
else
cout<<nume<<"/"<<deno<<endl;
}
int main()
{
CFraction cf;
cf.Set(24,72);
cf.input();
//cf.output(0);
cf.amplify(5);
cout<<"style(0)"<<endl;
cf.output(0);
cout<<"style(1):"<<endl;
cf.output(1);
cout<<"style(2):"<<endl;
cf.output(2);
return 0;
}
老师,这个分母为什么是随机数啊?????