#include <iostream>
#include <math.h>
#include "racional.h"

using namespace std;

void Racional::aRacional(double x){
  num = 1;
  den = 1;
}

double Racional::aFlotante(){
  return (double)num/(double)den;
}

Racional Racional::operator^(int n){
  Racional r;
  int i,s=1;
  for(i=1;i<=n;i++) s *= num;
  r.num = s;
  s = 1;
  for(i=1;i<=n;i++) s *= den;
  r.den = s;
  r.simplifica();
  return r;
}

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

Racional Racional::operator-(Racional a) {
  Racional r;
  r.num = num*a.den-den*a.num;
  r.den = den*a.den;
  r.simplifica();
  return r;
}

Racional Racional::operator*(Racional a) {
  Racional r;
  r.num = num*a.num;
  r.den = den*a.den;
  r.simplifica();
  return r;
}

Racional Racional::operator/(Racional a) {
  Racional r;
  r.num = num*a.den;
  r.den = den*a.num;
  r.simplifica();
  return r;
}

bool Racional::operator==(Racional a){
  simplifica();
  a.simplifica();
  return num==a.num && den==a.den;
}

bool Racional::operator!=(Racional a){
  return !(*this==a);
}

bool Racional::operator>(Racional a){
  simplifica();
  a.simplifica();
  return (num*a.den > den*a.num);
}

bool Racional::operator<(Racional a){
  simplifica();
  a.simplifica();
  return (num*a.den < den*a.num);
}

bool Racional::operator>=(Racional a){
  return (*this > a)||(*this == a);
}

bool Racional::operator<=(Racional a){
  return (*this < a)||(*this == a);
}

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;
}

Racional Racional::operator++(){
  Racional r(1);
  *this = *this+1;
  return *this;
}

Racional Racional::operator--(){
  Racional r(1);
  *this = *this-1;
  return *this;
}

Racional Racional::operator++(int){
  Racional r=*this;
  *this = *this+1;
  return r;
}

Racional Racional::operator--(int){
  Racional r=*this;
  *this = *this-1;
  return r;
}

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

