Рац. числа - 3 точка
unknown
csharp
3 years ago
2.0 kB
8
Indexable
using System;
namespace ConsoleApp19
{
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 string getDec()
{
return string.Format("{0:0.00}", (double)this.num / this.den);
}
public string print()
{
return string.Format("{0}/{1}", this.num, this.den);
}
public Rational multi(Rational r)
{
return new Rational(this.num * r.num, this.den * r.den);
}
public Rational dev(Rational r)
{
return new Rational(this.num * r.den, this.den * r.num);
}
public Rational add(Rational r)
{
int n = this.num * r.den + r.num * this.den;
int d = this.den *= r.den;
return new Rational(n, d);
}
public Rational minus(Rational r)
{
int p = this.num * r.den - r.num * this.den;
int q = this.den *= r.den;
return new Rational(p, q);
}
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 Program
{
static void Main(string[] args)
{
Rational a = new Rational(4, 10);
Rational b = new Rational(4, 5);
Rational c = a.multi(b);
Console.WriteLine(c.print());
Console.WriteLine(c.getDec());
}
}
}
Editor is loading...