#include<cstdio>
#include<iostream>
using namespace std;
struct Myfloat {
int son,mother;
Myfloat(int x=0,int y=0) {
son=y;
mother=x;
}
void pt() {
printf("%d/%d",son,mother);
}
};
int gcd(int x, int y) {
int z = y;
while(x%y!=0) {
z = x%y;
x = y;
y = z;
}
return z;
}
int lcm(int x, int y){
return x*y/gcd(x,y);
}
Myfloat operator + (Myfloat F1,Myfloat F2) {
int bigmother=lcm(F1.mother,F2.mother);
int bigson=F1.son*bigmother/F1.mother+F2.son*bigmother/F2.mother;
int biggcd=gcd(bigson,bigmother);
return Myfloat(bigmother/biggcd,bigson/biggcd);
}
int main() {
//try:
Myfloat a(5,4); // 4/5
Myfloat b(6,7); // 7/6
Myfloat c=a+b;
c.pt();// 59/30
return 0;
}
利用最大公约数和最小公倍数进行计算