1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
void somafrac(int na, int da, int nb, int db, int &nr, int &dr){
//Calculo do mmc
int a=da, b=db, r, mdc, mmc;
while (b!=0){
r = a%b;
a = b;
b = r;
}
mdc=a;
mmc=(da*db)/mdc;
//Valores não simplificados de nr e dr
dr=mmc;
nr=((mmc/da)*na+(mmc/db)*nb);
//Simplificação de nr e dr
a=nr;
b=dr;
int mdcr;
while (b!=0){
r = a%b;
a = b;
b = r;
}
mdcr=a;
//dr e nr simplificados
dr=dr/mdcr;
nr=nr/mdcr;
}
int main (){
int a, b, c, d, nr, dr;
cin >> a >> b >> c >> d;
somafrac(a, b, c, d, nr, dr);
cout << a << "/" << b << "+" << c << "/" << d << "=" << nr << "/" << dr << endl;
return 0;
}
|