Exemplo n.º 1
0
 public static void Deceive(Fraction first, Fraction second)//метод зводить дроби до спільного знаменника
 {
     int znamennuk = first.number_2 * second.number_2;//результат добуток знаменників
     first.number_1 = second.number_2 * first.number_1;//при зміненні знаменника змінюється і чисельник відповідним чином 
     second.number_1 = first.number_2 * second.number_1;//домножимо чисельник на знаменник іншого дробу 
     first.number_2 = first.number_2 * second.number_2;
     second.number_2 = first.number_2;
 }
Exemplo n.º 2
0
 //перегрузки операторів
 public static Fraction operator +(Fraction first, Fraction second)
 {
     if (first.number_2 != second.number_2)
     {
         Deceive(first, second);
     }
     Fraction a = new Fraction(first.number_1 + second.number_1, first.number_2);
     Reduce(a);
     return a;
 }
Exemplo n.º 3
0
 public static Fraction operator -(Fraction first, Fraction second)
 {
     if (first.number_2 != second.number_2)//коли знаменник рівні, то в залежності від знака виконаємо дію
     //якщо знаменники нерівні
     {
         Deceive(first, second);
     }
     Fraction a = new Fraction(first.number_1 - second.number_1, first.number_2);
     Reduce(a);
     return a;
 }
Exemplo n.º 4
0
 //методи
 public static void Reduce(Fraction c)//пошука НСД і скорочення
 {
     //для знаходження НСД використаємо алгоритм Евкліда
     int copyChiselnuk3;
     int copyZnamennuk3;
     copyChiselnuk3 = c.number_1; //зробимо копії змінних
     copyZnamennuk3 = c.number_2;
     while (copyChiselnuk3 != copyZnamennuk3)//поки чисельник не дорівнює знаменнику
     {
         if (copyChiselnuk3 > copyZnamennuk3)//порівняємо яке значення більше і від більшого віднімемо менше
         {
             copyChiselnuk3 = copyChiselnuk3 - copyZnamennuk3;
         }
         else
         {
             copyZnamennuk3 = copyZnamennuk3 - copyChiselnuk3;
         }
     }
     //поділимо чисельник і знаменник на НСД, тим самим скоротивши дріб
     c.number_1 = c.number_1 / copyChiselnuk3;
     c.number_2 = c.number_2 / copyChiselnuk3;
 }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            try
            {
                Numbers[] n = new Numbers[6] { new Complex(), new Complex(2, 1), new Complex(2, 3),new Fraction(),  new Fraction(1), new Fraction(2,5) };
                Console.WriteLine("a=" + n[1].ToString());
                Console.WriteLine("b=" + n[2].ToString());
                n[0] = (Complex)n[1] + (Complex)n[2];
                Console.WriteLine("a+b=" + n[0].ToString());
                n[0] = (Complex)n[1] / (Complex)n[2];
                Console.WriteLine("a/b=" + n[0].ToString());
                n[0] = (Complex)n[1] - (Complex)n[2];
                Console.WriteLine("a-b=" + n[0].ToString());
                n[0] = (Complex)n[1] * (Complex)n[2];
                Console.WriteLine("a*b=" + n[0].ToString());
                Console.WriteLine("test=" + n[1].ToString());
                Console.WriteLine("спряжене= " + ((Complex)n[1]).conjugate());
                double n1 = 2.0;
                Console.WriteLine(((Complex)n[1]).Root(n1));
                Console.WriteLine(((Complex)n[1]).Pow(n1));

                Console.WriteLine("+++++++++++++++++++++++");


                Fraction a1 = new Fraction(1);
                Console.WriteLine("1+2/5=");
                Fraction b1 = new Fraction(2, 5);
                n[3] = (Fraction)n[4] + (Fraction)n[5];
                Console.WriteLine(n[3].ToString());
                Console.WriteLine("1-2/5=");
                n[3] = (Fraction)n[4] - (Fraction)n[5];
                Console.WriteLine(n[3].ToString());
                Console.WriteLine("1*2/5=");
                n[3] = (Fraction)n[4] * (Fraction)n[5];
                Console.WriteLine(n[3].ToString());
                Console.WriteLine("1 : 2/5=");
                n[3] = (Fraction)n[4] / (Fraction)n[5];
                Console.WriteLine(n[3].ToString());
                Console.WriteLine();
                Console.WriteLine("1<2/5");
                Console.WriteLine((Fraction)n[4] < (Fraction)n[5]);
                Console.WriteLine();
                Console.WriteLine("1>2/5");
                Console.WriteLine((Fraction)n[4] > (Fraction)n[5]);
                Console.WriteLine();
                Console.WriteLine("1<=2/5");
                Console.WriteLine((Fraction)n[4] <= (Fraction)n[5]);
                Console.WriteLine();
                Console.WriteLine("1>=2/5");
                Console.WriteLine((Fraction)n[4] >= (Fraction)n[5]);
                Console.WriteLine();
                Console.WriteLine("1==2/5");
                Console.WriteLine((Fraction)n[4] == (Fraction)n[5]);
                Console.WriteLine();
                Console.WriteLine("1!=2/5");
                Console.WriteLine((Fraction)n[4] != (Fraction)n[5]);
                Fraction mm = new Fraction(2, 2);
                Complex mn = new Complex(2, 2);
                if (mn.Equals(mm))
                    Console.WriteLine("+++");
                else
                    Console.WriteLine("---");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

        }
Exemplo n.º 6
0
        public static Fraction operator /(Fraction first, Fraction second)
        {
            if (second.number_1 == 0)
            {
                throw new DivideByZeroException();
            }
            int chiselnuk = first.number_1 * second.number_2;//чисельник першого дробу множимо на знаменник другого
            int znamennuk = first.number_2 * second.number_1;//знаменник першого дробу множимо на чисельник другогo
            Fraction c = new Fraction(chiselnuk, znamennuk);
            Fraction.Reduce(c);
            return c;

        }
Exemplo n.º 7
0
 public static Fraction operator *(Fraction first, Fraction second)
 {
     Fraction a = new Fraction(first.number_1 * second.number_1, first.number_2 * second.number_2);
     Reduce(a);
     return a;
 }
Exemplo n.º 8
0
 }//перевантажуємо метод для перетворення об'єкта в рядок
 private int Compare(Fraction other) // метод є базовим для перевантаження порівнянь
 {
     double first = this.number_1 / this.number_2;
     double second = other.number_1 / other.number_2;
     if (first > second) return 1;
     if (first < second) return -1;
     return 0;
 }
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            try
            {
                Numbers[] n = new Numbers[6] {
                    new Complex(), new Complex(2, 1), new Complex(2, 3), new Fraction(), new Fraction(1), new Fraction(2, 5)
                };
                Console.WriteLine("a=" + n[1].ToString());
                Console.WriteLine("b=" + n[2].ToString());
                n[0] = (Complex)n[1] + (Complex)n[2];
                Console.WriteLine("a+b=" + n[0].ToString());
                n[0] = (Complex)n[1] / (Complex)n[2];
                Console.WriteLine("a/b=" + n[0].ToString());
                n[0] = (Complex)n[1] - (Complex)n[2];
                Console.WriteLine("a-b=" + n[0].ToString());
                n[0] = (Complex)n[1] * (Complex)n[2];
                Console.WriteLine("a*b=" + n[0].ToString());
                Console.WriteLine("test=" + n[1].ToString());
                Console.WriteLine("спряжене= " + ((Complex)n[1]).conjugate());
                double n1 = 2.0;
                Console.WriteLine(((Complex)n[1]).Root(n1));
                Console.WriteLine(((Complex)n[1]).Pow(n1));

                Console.WriteLine("+++++++++++++++++++++++");


                Fraction a1 = new Fraction(1);
                Console.WriteLine("1+2/5=");
                Fraction b1 = new Fraction(2, 5);
                n[3] = (Fraction)n[4] + (Fraction)n[5];
                Console.WriteLine(n[3].ToString());
                Console.WriteLine("1-2/5=");
                n[3] = (Fraction)n[4] - (Fraction)n[5];
                Console.WriteLine(n[3].ToString());
                Console.WriteLine("1*2/5=");
                n[3] = (Fraction)n[4] * (Fraction)n[5];
                Console.WriteLine(n[3].ToString());
                Console.WriteLine("1 : 2/5=");
                n[3] = (Fraction)n[4] / (Fraction)n[5];
                Console.WriteLine(n[3].ToString());
                Console.WriteLine();
                Console.WriteLine("1<2/5");
                Console.WriteLine((Fraction)n[4] < (Fraction)n[5]);
                Console.WriteLine();
                Console.WriteLine("1>2/5");
                Console.WriteLine((Fraction)n[4] > (Fraction)n[5]);
                Console.WriteLine();
                Console.WriteLine("1<=2/5");
                Console.WriteLine((Fraction)n[4] <= (Fraction)n[5]);
                Console.WriteLine();
                Console.WriteLine("1>=2/5");
                Console.WriteLine((Fraction)n[4] >= (Fraction)n[5]);
                Console.WriteLine();
                Console.WriteLine("1==2/5");
                Console.WriteLine((Fraction)n[4] == (Fraction)n[5]);
                Console.WriteLine();
                Console.WriteLine("1!=2/5");
                Console.WriteLine((Fraction)n[4] != (Fraction)n[5]);
                Fraction mm = new Fraction(2, 2);
                Complex  mn = new Complex(2, 2);
                if (mn.Equals(mm))
                {
                    Console.WriteLine("+++");
                }
                else
                {
                    Console.WriteLine("---");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }