/************************************************************************/
/* 求两个分数相加,方法:先求出两个分母的最小公倍数,再求分子和,最后约检结果 */
/************************************************************************/
#include <iostream>
using namespace std;
class Fract{
int num,den;//分子numerator of a fraction 分母denominator
public:
Fract(int n=0,int d=1){
num=n;
den=d;
}
int gcd(int m,int n);//最大公约数 greatest common divisor
Fract result(Fract f);
void show();
};
int main(){
Fract f1(1,5);
f1.show();
Fract f2(7,20),f3;
f2.show();
f3=f1.result(f2);
f3.show();
return 0;
}
int Fract::gcd(int m,int n){
int r;
if(m<n){
r=m;
m=n;
n=r;
}
while(r=m%n){
m=n;
n=r;
}
cout<<n<<endl;
return n;
}
Fract Fract::result(Fract f){
Fract sum;
sum.den=this->den*f.den;
sum.num=this->num*f.den+this->den*f.num;
int x=gcd(sum.num,sum.den);
sum.num=sum.num/x;
sum.den=sum.den/x;
return sum;
}
void Fract::show(){
cout<<num<<"/"<<den<<endl;
}