//===========================================
// rational  number description
// Dr. Turhan Coban
//===========================================
import javax.swing.JOptionPane; // giriş çıkış


// class rational numbers


public class rational {
    // sınıf degişkenleri
    protected int   numerator;
    protected int   denominator;

    // kurucu metodlar

public rational()
{
numerator=0;
denominator=1;
}

public rational(int nnumerator,int ndenominator)
{
numerator=nnumerator;
denominator=ndenominator;
}

public rational(int numer)
{
numerator=numer;
denominator=1;
}

public rational(rational c )
{
numerator=c.numerator();
denominator=c.denominator();
}

//hesap metodları

public int leastcommondivisor()
{
// iki tam sayinin en buyuk ortak divideenini hesaplar
int n=numerator;
int m=denominator;
if(n==0)
   return m;
if(m==0)
   return n;
while(m != n)
{
  if(n>m)
     n=n-m;
  else
     m=m-n;
}
return n;
}

public void simplify()
{
//bayağı kesiri sadeleştirir
int isaret=1;
if(numerator<0)
{
 isaret=-isaret;
 numerator=-numerator;
}
if(denominator<0)
{
 isaret=-isaret;
 denominator=-denominator;
}
if(denominator==0)
   { JOptionPane.showMessageDialog(null,toString(),
     "Bayağı kesir sıfıra bölme hatası",
     JOptionPane.ERROR_MESSAGE);System.exit(0);
   }
int ebob=leastcommondivisor();
ebob=Math.abs(ebob);
numerator=isaret*numerator/ebob;
denominator=denominator/ebob;
}


// giriş - çıkış metotları

public int numerator()
{
return numerator;
}

public int denominator()
{
return denominator;
}

public void numeratorGir(int r)
{
numerator=r;
}

public void denominatorGir(int i)
{
denominator=i;
}

public void input_rational(rational sag)
{
numerator=sag.numerator();
denominator=sag.denominator();
}

public void input_rational(int nr,int ni)
{
numerator=nr;
denominator=ni;
}

public void input_rational(int nr)
{
numerator=nr;
denominator=1;
}

public void input_rational(double d)
{
// tam sayinin rational esitini yükle
// eh birazcık yaklaşım var tabi
input_rational((int)d*10000,10000);
simplify();
}

public double toDouble()
{
//rational sayinin gercek sayi esidi
return ((double)numerator/(double)denominator);
}

public static rational convert_to_rational(double d)
{
rational b=new rational();
b.input_rational((int)d*10000,10000);
b.simplify();
return b;
}


public void add(rational  sag)
{
numerator = numerator*sag.denominator() + sag.numerator()*denominator;
denominator = denominator*sag.denominator();
simplify();
}

public void add(int sag)  
{
numerator = numerator + sag*denominator;
simplify();
}

public void substract(rational  sag)  
{
numerator = numerator*sag.denominator() - sag.numerator()*denominator;
denominator = denominator*sag.denominator();
simplify();
}

public void substract(int sag)  
{
numerator = numerator - sag*denominator;
simplify();
}

public void multiply(rational sag )  
{
numerator = numerator*sag.numerator();
denominator = denominator*sag.denominator();
simplify();
}

public void multiply(int sag )  
{
numerator = numerator*sag;
simplify();
}

public void divide(rational sag )  
{
numerator = numerator*sag.denominator();
denominator = denominator*sag.numerator();
simplify();
}

public void divide(int sag )  
{
denominator = denominator*sag;
simplify();
}

public static rational add(rational sol, rational sag)  
{
 int r1= sol.numerator() * sag.denominator() + sag.numerator() * sol.denominator();
 int i1= sol.denominator()* sag.denominator();
 rational sonuc;
 sonuc=new rational(r1,i1);
 sonuc.simplify();
 return sonuc;
}

public static rational add(rational sol, int sag)  
{
 int r1=sol.numerator() + sag*sol.denominator();
 int i1=sol.denominator();
 rational sonuc;
 sonuc=new rational(r1,i1);
 sonuc.simplify();
 return sonuc;
}

public static rational add(int sol, rational sag)  
{
 int r1=sag.numerator() + sol*sag.denominator();
 int i1=sag.denominator();
 rational sonuc;
 sonuc=new rational(r1,i1);
 sonuc.simplify();
 return sonuc;
}

//===================================
public static rational substract(rational sol, rational sag)  
{
 // iki rational sayının addmını return deyimiyle rational olarak
 // aktarır
 int r1= sol.numerator() * sag.denominator() - sag.numerator() * sol.denominator();
 int i1= sol.denominator()* sag.denominator();
 rational sonuc;
 sonuc=new rational(r1,i1);
 sonuc.simplify();
 return sonuc;
}

public static rational substract(rational sol, int sag)  
{
 // bir rational ve bir numerator sayının addmını return deyimiyle rational olarak
 // aktarır
 int r1=sol.numerator() - sag*sol.denominator();
 int i1=sol.denominator();
 rational sonuc;
 sonuc=new rational(r1,i1);
 sonuc.simplify();
 return sonuc;
}

public static rational substract(int sol, rational sag)  
{
 int r1=sag.numerator() - sol*sag.denominator();
 int i1=sag.denominator();
 rational sonuc;
 sonuc=new rational(r1,i1);
 sonuc.simplify();
 return sonuc;
}

//===================================

public static rational multiply(rational sol, rational sag)  
{ 
 rational sonuc;
 sonuc=new rational(sol.numerator()*sag.numerator(),sol.denominator()*sag.denominator());
 sonuc.simplify();
 return sonuc;
}

public static rational multiply(rational sol, int sag)  
{ 
 rational sonuc;
 sonuc=new rational(sol.numerator()*sag,sol.denominator());
 sonuc.simplify();
 return sonuc;
}

public static rational multiply(int sol, rational sag)  
{ 
 rational sonuc;
 sonuc=new rational(sag.numerator()*sol,sag.denominator());
 sonuc.simplify();
 return sonuc;
}

public static rational divide(rational sol, rational  sag)  
{ 
 double a=sag.numerator()*sag.numerator()+sag.denominator()*sag.denominator();
 rational sonuc;
 sonuc=new rational((int)(sol.numerator()*sag.denominator()),(int)(sol.denominator()*sag.numerator()));
 sonuc.simplify();
 return sonuc;
}

public static rational divide(rational sol, int  sag)  
{ 
 rational sonuc;
 sonuc=new rational(sol.numerator(),(sol.denominator()*sag));
 sonuc.simplify();
 return sonuc;
}

public boolean smaller_than(rational sol,rational sag)
{
// less then comparison of two rational numbers
return (sol.toDouble() < sag.toDouble());
}

public boolean smaller_than_equals(rational sol,rational sag)
{
// less then and equals comparison of two rational numbers
return (sol.toDouble() <= sag.toDouble());
}

public boolean greater_than(rational sol,rational sag)
{
// greater_than then comparison of two rational numbers
return sol.toDouble() > sag.toDouble();
}

public boolean greater_than_equals(rational sol,rational  sag)
{
// greater_than then and equals comparison of two rational numbers
return sol.toDouble() >= sag.toDouble();
}


public boolean equals(rational sol,rational sag)
{
// equals comparison of two rational numbers
return sol.toDouble() == sag.toDouble();
}

public boolean not_equals(rational sol,rational  sag)
{
// not equals comparison of two rational numbers
return sol.toDouble() != sag.toDouble();
}

public static String toString(rational  value)
{
String b="";
if(Math.abs(value.denominator())!=1)
  {
    b=b+"( "+value.numerator()+" / "+value.denominator()+" )";
  }
else
  {
    b=b+value.numerator()+" ";
  }
return b;
}

public String toString()
{
// yazima hazır rational formda String deşiskenini iletir.
String b="";
if(Math.abs(denominator())!=1)
  {
    b=b+"( "+numerator+" / "+denominator()+" )";
  }
else
  {
    b=b+numerator()+" ";
  }
return b;
}
                };

//rational sınıfının sonu


