/*Clase Racional: Implementación*/
#include <iostream>
#include "racional.h"

using namespace std;

Racional Racional::suma(Racional a,Racional b){
  Racional t;
  t.num = a.num*b.den+a.den*b.num;
  t.den = a.den*b.den;
  t.simplifica();
  return t;
}

Racional Racional::suma(Racional a){
  Racional t;
  t.num = a.num*den+a.den*num;
  t.den = a.den*den;
  t.simplifica();
  return t;
}

Racional Racional::resta(Racional a,Racional b){
  Racional t;
  t.num = a.num*b.den-a.den*b.num;
  t.den = a.den*b.den;
  t.simplifica();
  return t;
}

Racional Racional::miltiplica(Racional a,Racional b){
  Racional t;
  t.num = a.num*b.num;
  t.den = a.den*b.den;
  t.simplifica();
  return t;
}

Racional Racional::divide(Racional a,Racional b){
  Racional t;
  t.num = a.num*b.den;
  t.den = a.den*b.num;
  t.simplifica();
  return t;
}

void Racional::escribe(){
  cout << num << "/" << den << "\n";
}

void Racional::simplifica(){
  int a,b,res;
  if(num>den){
    a = num;
    b = den;
  }  else{
    a = den;
    b = num;
  }
  while(b != 0){
    res = a%b;
    a = b;
    b = res;
  }
  num = num/a;
  den = den/a;
}
