Exemplo n.º 1
0
        public static RatNumber operator /(RatNumber a, RatNumber b)
        {
            RatNumber div = new RatNumber(a.A * b.B, a.B * b.A);

            div.ReduceFraction();
            return(div);
        }
Exemplo n.º 2
0
        public static RatNumber operator *(RatNumber a, RatNumber b)
        {
            RatNumber mul = new RatNumber(a.A * b.A, a.B * b.B);

            mul.ReduceFraction();
            return(mul);
        }
Exemplo n.º 3
0
 public bool Compare(RatNumber other)
 {
     if (other == null)
     {
         return(false);
     }
     if (this.A == other.A && this.B == other.B)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Exemplo n.º 4
0
 public bool Equals(RatNumber other)
 {
     if (other == null)
     {
         return(false);
     }
     if (((double)this.A) / ((double)this.B) == (((double)other.A) / ((double)other.B)))
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Exemplo n.º 5
0
        public RatNumber StrToObj(string str)
        {
            string strn = "";
            string strm = "";

            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] == '/')
                {
                    for (i++; i < str.Length; i++)
                    {
                        strm += str[i];
                    }
                }
                else
                {
                    strn += str[i];
                }
            }
            RatNumber ratnum1 = new RatNumber(Convert.ToInt32(strn), Convert.ToInt32(strm));

            return(ratnum1);
        }
Exemplo n.º 6
0
        static void Main()
        {
            try
            {
                RatNumber      num1 = new RatNumber(0, 1);
                RatNumber      num2 = new RatNumber(0, 1);
                string         str  = "";
                string         str2 = "";
                ConsoleKeyInfo ch;
                ConsoleKeyInfo ch1;

                Console.WriteLine("Vvedite chislitel for vashego Rational Number:");
                num1.A = int.Parse(Console.ReadLine());
                Console.WriteLine("Vvedite znamenatel for vashego Rational Number:");
                num1.B = int.Parse(Console.ReadLine());
                Console.Clear();
                Console.WriteLine("Choose string format:");
                Console.WriteLine("1");
                Console.WriteLine("2");
                ch = Console.ReadKey();
                if (ch.KeyChar == '1')
                {
                    str = num1.ToString();
                }
                if (ch.KeyChar == '2')
                {
                    str = num1.StrFormat();
                }
                while (true)
                {
                    Console.Clear();
                    Console.WriteLine("Your rational number: ");
                    Console.WriteLine(str);
                    Console.WriteLine("");
                    Console.WriteLine("1 - Add the rational number");
                    Console.WriteLine("2 - Subtract the rational number");
                    Console.WriteLine("3 - Multiplied by the rational number");
                    Console.WriteLine("4 - Divided by the rational number");
                    Console.WriteLine("5 - To equal with rational number");
                    Console.WriteLine("6 - Change string format");
                    Console.WriteLine("7 - To compare two rational numbers");
                    Console.WriteLine("0 - Exit");
                    ch1 = Console.ReadKey();
                    if (ch1.KeyChar == '1')
                    {
                        Console.Clear();
                        Console.WriteLine("Vvedite A");
                        num2.A = int.Parse(Console.ReadLine());
                        Console.Clear();
                        Console.WriteLine("Vvedite B");
                        num2.B = int.Parse(Console.ReadLine());
                        num1   = num1 + num2;
                        if (ch.KeyChar == '1')
                        {
                            str = num1.ToString();
                        }
                        if (ch.KeyChar == '2')
                        {
                            str = num1.StrFormat();
                        }
                        Console.Clear();
                        Console.WriteLine("Nazhmite na lybuyu knopku for continue");
                    }

                    if (ch1.KeyChar == '2')
                    {
                        Console.Clear();
                        Console.WriteLine("Vvedite A");
                        num2.A = int.Parse(Console.ReadLine());
                        Console.Clear();
                        Console.WriteLine("Vvedite B");
                        num2.B = int.Parse(Console.ReadLine());
                        num1   = num1 - num2;
                        if (ch.KeyChar == '1')
                        {
                            str = num1.ToString();
                        }
                        if (ch.KeyChar == '2')
                        {
                            str = num1.StrFormat();
                        }
                        Console.Clear();
                        Console.WriteLine("Nazhmite na lybuyu knopku for continue");
                    }

                    if (ch1.KeyChar == '3')
                    {
                        Console.Clear();
                        Console.WriteLine("Vvedite A");
                        num2.A = int.Parse(Console.ReadLine());
                        Console.Clear();
                        Console.WriteLine("Vvedite B");
                        num2.B = int.Parse(Console.ReadLine());
                        num1   = num1 * num2;
                        if (ch.KeyChar == '1')
                        {
                            str = num1.ToString();
                        }
                        if (ch.KeyChar == '2')
                        {
                            str = num1.StrFormat();
                        }
                        Console.Clear();
                        Console.WriteLine("Nazhmite na lybuyu knopku for continue");
                    }

                    if (ch1.KeyChar == '4')
                    {
                        Console.Clear();
                        Console.WriteLine("Vvedite A");
                        num2.A = int.Parse(Console.ReadLine());
                        Console.Clear();
                        Console.WriteLine("Vvedite B");
                        num2.B = int.Parse(Console.ReadLine());
                        num1   = num1 / num2;
                        if (ch.KeyChar == '1')
                        {
                            str = num1.ToString();
                        }
                        if (ch.KeyChar == '2')
                        {
                            str = num1.StrFormat();
                        }
                        Console.Clear();
                        Console.WriteLine("Nazhmite na lybuyu knopku for continue");
                    }

                    if (ch1.KeyChar == '5')
                    {
                        Console.Clear();
                        Console.WriteLine("Enter rational number: ");
                        str2 = Console.ReadLine();
                        num2 = num2.StrToObj(str2);
                        Console.Clear();
                        if (num1.Equals(num2))
                        {
                            Console.WriteLine("Yes");
                        }
                        else
                        {
                            Console.WriteLine("No");
                        }
                    }
                    if (ch1.KeyChar == '6')
                    {
                        Console.Clear();
                        Console.WriteLine("Choose string format:");
                        Console.WriteLine("1");
                        Console.WriteLine("2");
                        ch = Console.ReadKey();
                        if (ch.KeyChar == '1')
                        {
                            str = num1.ToString();
                        }
                        if (ch.KeyChar == '2')
                        {
                            str = num1.StrFormat();
                        }
                        Console.Clear();
                        Console.WriteLine("Nazhmite na lybuyu knopku for continue");
                    }
                    if (ch1.KeyChar == '7')
                    {
                        Console.Clear();
                        Console.WriteLine("VVedite A");
                        num2.A = int.Parse(Console.ReadLine());
                        Console.Clear();
                        Console.WriteLine("Vvedite B");
                        num2.B = int.Parse(Console.ReadLine());
                        if (num1 > num2)
                        {
                            Console.WriteLine("{0} > {1}", num1, num2);
                        }
                        else
                        {
                            if (num2 > num1)
                            {
                                Console.WriteLine("{0} < {1}", num1, num2);
                            }
                        }
                    }

                    if (ch1.KeyChar == '0')
                    {
                        break;
                    }
                    Console.ReadKey();
                }
            }
            catch (Exception exc)
            {
                Console.Clear();
                Console.WriteLine(exc.Message);
            }
        }