# Rational - Bojidara

unknown
plain_text
a year ago
1.9 kB
1
Indexable
Never
namespace Rational
{
class Rational
{
int num;
int den;
public Rational(Rational r)
{
this.num = r.num;
this.den = r.den;
}
public Rational(int n, int d)
{
this.num = n;
this.den = d;
normalize();
}
public double getDec()
{
return (double)this.num / this.den;
}

public void multi(Rational r)
{
this.num *= r.num;
this.den *= r.den;
normalize();
}
public string print(){
return string.Format("{0}/{1}", this.num, this.den);
}

public void dev(Rational r)
{
this.num *= r.den;
this.den *= r.num;
normalize();
}

public void add(Rational r)
{
this.num = (r.num*this.den)+(r.den*this.num);
this.den *= r.den;
normalize();
}
public void sub(Rational r)
{
this.num = (r.den * this.num) - (r.num * this.den);
this.den *= r.den;
normalize();
}
void normalize()
{
int a = Math.Abs(this.num);
int b = Math.Abs(this.den);
while (a != b)
{
if (a > b) a -= b;
else b -= a;
}
this.num /= a;
this.den /= a;
}
}

class HelloWorld
{
static void Main(string[] args)
{
Rational a = new Rational(4,10);
Rational b = new Rational(4,5);
Rational c = new Rational(a);
Console.WriteLine(c.print());
Console.WriteLine(c.getDec());

}

}
}