Advertisement
chipsi2

מספרים אי רציונליים

Feb 21st, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.48 KB | None | 0 0
  1.  class Rational
  2.     {
  3.         private int x;
  4.         private int y;
  5.         public Rational(int x, int y)
  6.         {
  7.             this.x = x;
  8.             this.y = y;
  9.         }
  10.         public int GetNumerator()
  11.         {
  12.             return this.x;
  13.         }
  14.         public int GetDenom()
  15.         {
  16.             return this.y;
  17.         }
  18.         public bool IsEqual(Rational num)
  19.         {
  20.             if (this.x * num.y == this.y * num.x)
  21.                 return true;
  22.             else return false;
  23.         }
  24.         public Rational Multiply(Rational num)
  25.         {
  26.             Rational R = new Rational(this.x * num.x, this.y * num.y);
  27.             return R;
  28.         }
  29.         public Rational Divide(Rational num)
  30.         {
  31.             Rational D = new Rational(this.x* num.y, this.y * num.x);
  32.             return D;
  33.         }
  34.         public override string ToString()
  35.         {
  36.             return string.Format(x + "/" + y);
  37.         }
  38.     }
  39.  
  40.     class Program
  41.     {
  42.         public static Rational Plus (Rational num,Rational num2)
  43.         {
  44.             int a = num.GetNumerator();
  45.             int b = num.GetDenom(), c = num2.GetNumerator(), d = num2.GetDenom();
  46.             Rational r1 = new Rational(a * d + c * b, a * d + d * a);
  47.             return r1;
  48.            
  49.  
  50.         }
  51.         static void Main(string[] args)
  52.         {
  53.             Rational r2 = new Rational(1, 6);
  54.             Rational r1 = new Rational(2, 4);
  55.             Console.WriteLine(Plus(r1, r2));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement