这个想法已经有3周,于是今天将它实现了。
Step 1基础:
我们需要定义一个class——fraction,全部属性定义为public
class fraction{
public:
};
现在,让我们添加2个元素,分子和分母——fz和fw
Step 1.1添加分子分母:
class fraction{
public:
int fz,fw;
};
Step 2重载运算符:
首先是约分
static fraction teasy(fraction ans){
int yf=__gcd(ans.fz,ans.fw);
ans.fz/=yf;
ans.fw/=yf;
return ans;
}
接着是lcm(最小公倍数)
static int lcm(int x,int y){
return x*y/__gcd(x,y);
}
其次是加减乘除,很简单
friend fraction operator+(fraction x,fraction y){
fraction ans;
ans.fz=x.fz*y.fw+y.fz*x.fw;
ans.fw=x.fw*y.fw;
return teasy(ans);
}
friend fraction operator-(fraction x,fraction y){
fraction ans;
ans.fz=x.fz*y.fw-y.fz*x.fw;
ans.fw=x.fw*y.fw;
return teasy(ans);
}
friend fraction operator*(fraction x,fraction y){
fraction ans;
ans.fz=x.fz*y.fz;
ans.fw=x.fw*y.fw;
return teasy(ans);
}
friend fraction operator/(fraction x,fraction y){
fraction ans;
swap(y.fz,y.fw);
ans.fz=x.fz*y.fz;
ans.fw=x.fw*y.fw;
return teasy(ans);
}
friend bool operator>(fraction x,fraction y){
int thard=lcm(x.fw,y.fw);
x.fz=x.fz*thard/x.fw;
y.fz=y.fz*thard/y.fw;
if(x.fz>y.fz)return 1;
else return 0;
}
当然,逻辑运算符也不能忘记
friend bool operator>(fraction x,fraction y){
int thard=lcm(x.fw,y.fw);
x.fz=x.fz*thard/x.fw;
y.fz=y.fz*thard/y.fw;
if(x.fz>y.fz)return 1;
else return 0;
}
friend bool operator==(fraction x,fraction y){
int thard=lcm(x.fw,y.fw);
x.fz=x.fz*thard/x.fw;
y.fz=y.fz*thard/y.fw;
if(x.fz==y.fz)return 1;
else return 0;
}
friend bool operator<(fraction x,fraction y){
int thard=lcm(x.fw,y.fw);
x.fz=x.fz*thard/x.fw;
y.fz=y.fz*thard/y.fw;
if(x.fz<y.fz)return 1;
else return 0;
}
friend bool operator<=(fraction x,fraction y){
int thard=lcm(x.fw,y.fw);
x.fz=x.fz*thard/x.fw;
y.fz=y.fz*thard/y.fw;
if(x.fz<=y.fz)return 1;
else return 0;
}
friend bool operator>=(fraction x,fraction y){
int thard=lcm(x.fw,y.fw);
x.fz=x.fz*thard/x.fw;
y.fz=y.fz*thard/y.fw;
if(x.fz>=y.fz)return 1;
else return 0;
}
输出函数
static void print(fraction x){
if(x.fz==0)cout<<"0\n";
else if(x.fw==1)cout<<x.fz<<"\n";
else cout<<x.fz<<"/"<<x.fw<<"\n";
}
n次方函数
static fraction fipow(fraction x,int y){
fraction ans=x;
for(int i=0;i<y-1;i++)ans=ans*x;
return teasy(ans);
}
绝对值
static fraction frabs(fraction x){
if(x.fz*x.fw<1){
x.fz=abs(x.fz);
x.fw=abs(x.fw);
}
return x;
}
完整code:
class fraction{
public:
int fz,fw;
static fraction teasy(fraction ans){
int yf=__gcd(ans.fz,ans.fw);
ans.fz/=yf;
ans.fw/=yf;
return ans;
}
static int lcm(int x,int y){
return x*y/__gcd(x,y);
}
friend fraction operator+(fraction x,fraction y){
fraction ans;
ans.fz=x.fz*y.fw+y.fz*x.fw;
ans.fw=x.fw*y.fw;
return teasy(ans);
}
friend fraction operator-(fraction x,fraction y){
fraction ans;
ans.fz=x.fz*y.fw-y.fz*x.fw;
ans.fw=x.fw*y.fw;
return teasy(ans);
}
friend fraction operator*(fraction x,fraction y){
fraction ans;
ans.fz=x.fz*y.fz;
ans.fw=x.fw*y.fw;
return teasy(ans);
}
friend fraction operator/(fraction x,fraction y){
fraction ans;
swap(y.fz,y.fw);
ans.fz=x.fz*y.fz;
ans.fw=x.fw*y.fw;
return teasy(ans);
}
friend bool operator>(fraction x,fraction y){
int thard=lcm(x.fw,y.fw);
x.fz=x.fz*thard/x.fw;
y.fz=y.fz*thard/y.fw;
if(x.fz>y.fz)return 1;
else return 0;
}
friend bool operator==(fraction x,fraction y){
int thard=lcm(x.fw,y.fw);
x.fz=x.fz*thard/x.fw;
y.fz=y.fz*thard/y.fw;
if(x.fz==y.fz)return 1;
else return 0;
}
friend bool operator<(fraction x,fraction y){
int thard=lcm(x.fw,y.fw);
x.fz=x.fz*thard/x.fw;
y.fz=y.fz*thard/y.fw;
if(x.fz<y.fz)return 1;
else return 0;
}
friend bool operator<=(fraction x,fraction y){
int thard=lcm(x.fw,y.fw);
x.fz=x.fz*thard/x.fw;
y.fz=y.fz*thard/y.fw;
if(x.fz<=y.fz)return 1;
else return 0;
}
friend bool operator>=(fraction x,fraction y){
int thard=lcm(x.fw,y.fw);
x.fz=x.fz*thard/x.fw;
y.fz=y.fz*thard/y.fw;
if(x.fz>=y.fz)return 1;
else return 0;
}
static fraction fipow(fraction x,int y){
fraction ans=x;
for(int i=0;i<y-1;i++)ans=ans*x;
return teasy(ans);
}
static fraction ffpow(fraction x,fraction y){
}
static void print(fraction x){
if(x.fz==0)cout<<"0\n";
else if(x.fw==1)cout<<x.fz<<"\n";
else cout<<x.fz<<"/"<<x.fw<<"\n";
}
static fraction frabs(fraction x){
if(x.fz*x.fw<1){
x.fz=abs(x.fz);
x.fw=abs(x.fw);
}
return x;
}
};
测试:
test1:
test2:
test3:
test4:
总结:
代码难度不大,码量不大,作用不大(3“不大”代码)