class rational
{
private int nr, nu;
public rational() { nr = 0; nu = 1; }
public rational(int a, int b)
{ nr = a; nu = b; simplificare(); }
public void afisare()
{
if (nu == 1) Console.Write(nr);
else Console.Write(nr + "/" + nu);
}
public override string ToString()
{
if (nu == 1) return nr.ToString();
else return nr + "/" + nu;
}
public static rational operator +(rational a, rational b)
{return new rational(a.nr*b.nu+a.nu*b.nr,a.nu*b.nu);}
public static rational operator -(rational a, rational b)
{ return new rational(a.nr * b.nu - a.nu * b.nr, a.nu * b.nu); }
public static rational operator *(rational a, rational b)
{ return new rational(a.nr * b.nr , a.nu * b.nu); }
public static rational operator /(rational a, rational b)
{ return new rational(a.nr * b.nu , a.nu * b.nr); }
public static bool operator <(rational a, rational b)
{ return a.nr * b.nu < a.nu * b.nr; }
public static bool operator >(rational a, rational b)
{ return a.nr * b.nu > a.nu * b.nr; }
public static bool operator <=(rational a, rational b)
{ return !(a > b); }
public static bool operator >=(rational a, rational b)
{ return !(a < b); }
public static bool operator ==(rational a, rational b)
{ return a.nr * b.nu == a.nu * b.nr; }
public static bool operator !=(rational a, rational b)
{ return !(a == b); }
public static int comparare(rational a, rational b)
{
if (a < b) return -1;
else if (a == b) return 0;
else return 1;
}
public override bool Equals(object obj)
{
return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
private int cmmdc(int a, int b)
{
while (b != 0)
{
int r = a % b; a = b; b = r;
}
return a;
}
private void simplificare()
{
int c = cmmdc(Math.Abs(nr), Math.Abs(nu));
nr = nr / c; nu = nu / c;
if (nr * nu < 0)
{
nr = -Math.Abs(nr); nu = Math.Abs(nu);
}
}
}
|