Пример #1
0
        static void Main(string[] args)
        {
            Console.Write("Write two rational numbers: \n");
            RationalNumber a = new RationalNumber();
            RationalNumber b = new RationalNumber();

            Console.WriteLine(a.ToString() + "  " + b.ToString());

            Console.WriteLine($"{a} >= {b} = " + (a >= b));
            Console.WriteLine($"{a.ToStringfloat()} >= {b.ToStringfloat()} = " + (a >= b));
            Console.WriteLine($"{a} < {b} = " + (a < b));
            Console.WriteLine($"{a.ToStringfloat()} < {b.ToStringfloat()} = " + (a < b));
            Console.WriteLine($"{a} <= {b} = " + (a <= b));
            Console.WriteLine($"{a.ToStringfloat()} <= {b.ToStringfloat()} = " + (a <= b));
            Console.WriteLine($"{a} != {b} = " + (a != b));
            Console.WriteLine($"{a.ToStringfloat()} != {b.ToStringfloat()} = " + (a != b));
            Console.WriteLine($"{a} - {b} = " + (a - b));
            Console.WriteLine($"{a.ToStringfloat()} - {b.ToStringfloat()} = " + (a - b).ToStringfloat());
            Console.WriteLine($"{a} + {b} = " + (a + b));
            Console.WriteLine($"{a.ToStringfloat()} + {b.ToStringfloat()} = " + (a + b).ToStringfloat());
            Console.WriteLine($"{a} * {b} = " + (a * b));
            Console.WriteLine($"{a.ToStringfloat()} * {b.ToStringfloat()} = " + (a * b).ToStringfloat());
            Console.WriteLine($"{a} / {b} = " + (a / b));
            Console.WriteLine($"{a.ToStringfloat()} / {b.ToStringfloat()} = " + (a / b).ToStringfloat());


            Console.WriteLine("Рациональное число в строковом виде: " + a.ToString());
            Console.WriteLine("Рациональное число как float: " + a.ToStringfloat());
            Console.WriteLine("Рациональное число как double: " + a.ToStringDouble() + "\n");

            Console.ReadLine();
        }
Пример #2
0
        static void Main(string[] args)
        {
            RationalNumber number1 = new RationalNumber(2, 5);
            RationalNumber number2 = new RationalNumber(5, 2);

            Console.WriteLine("Number1 : {0}", number1.ToString(RationalNumber.RepresentationVariant.Fraction));
            Console.WriteLine("Number2 : {0} \n", number2.ToString(RationalNumber.RepresentationVariant.Fraction));


            //Arithmetical operations
            Console.WriteLine("Number1 + Number2 = {0}", (number1 + number2).ToString(RationalNumber.RepresentationVariant.Fraction));
            Console.WriteLine("Number1 - Number2 = {0}", (number1 - number2).ToString(RationalNumber.RepresentationVariant.Fraction));
            Console.WriteLine("Number1 * Number2 = {0}", (number1 * number2).ToString(RationalNumber.RepresentationVariant.Fraction));
            Console.WriteLine("Number1 / Number2 = {0}\n", (number1 / number2).ToString(RationalNumber.RepresentationVariant.Fraction));


            //Comparison operations
            Console.WriteLine("Number1 >  Number2 = {0}", number1 > number2);
            Console.WriteLine("Number1 <= Number2 = {0}", number1 <= number2);
            Console.WriteLine("Number1 == Number2 = {0}", number1 == number2);
            Console.WriteLine("Number1 != Number2 = {0} \n", number1 != number2);


            //Getting numbers from strings
            string case1 = "15/3";
            string case2 = "-7/2";
            string case3 = "1.3615";
            string case4 = "-5.";

            RationalNumber number3 = RationalNumber.StringToNumber(case1);
            RationalNumber number4 = RationalNumber.StringToNumber(case2);
            RationalNumber number5 = RationalNumber.StringToNumber(case3);
            RationalNumber number6 = RationalNumber.StringToNumber(case4);

            Console.WriteLine(@"Number 3 string variant : ""{0}"" and Number 3 deciaml fraction variant : {1}",
                              case1, number3.ToString(RationalNumber.RepresentationVariant.DecimalFraction));
            Console.WriteLine(@"Number 4 string variant : ""{0}"" and Number 4 deciaml fraction variant : {1}",
                              case2, number4.ToString(RationalNumber.RepresentationVariant.DecimalFraction));
            Console.WriteLine(@"Number 5 string variant : ""{0}"" and Number 5 deciaml fraction variant : {1}",
                              case3, number5.ToString(RationalNumber.RepresentationVariant.DecimalFraction));
            Console.WriteLine(@"Number 6 string variant : ""{0}"" and Number 6 deciaml fraction variant : {1}",
                              case4, number6.ToString(RationalNumber.RepresentationVariant.DecimalFraction));


            //Explicit/implicit operators
            RationalNumber number7 = 3.1;
            RationalNumber number8 = 7.99;
            int            a       = (int)number8;
            double         b       = (double)number7;

            Console.WriteLine($"Number7 : {number7.ToString(RationalNumber.RepresentationVariant.Fraction)} \n" +
                              $"Number8 : {number8.ToString(RationalNumber.RepresentationVariant.Fraction)} \n" +
                              $"a = {a}, b = {b} \n");
            Console.WriteLine("double b = {1} from Number7 : {0} ", number7.ToString(RationalNumber.RepresentationVariant.Fraction), b);
            Console.WriteLine("int a = {1} from Number8 : {0}\n", number8.ToString(RationalNumber.RepresentationVariant.Fraction), a);
        }
Пример #3
0
        static void Main(string[] args)
        {
            RationalNumber number1          = new RationalNumber(3, 5);                // example for constructor
            RationalNumber numbGetByMethod1 = RationalNumber.GetObjectByString("2/5"); // examples for method that takes string and returns
            RationalNumber numbGetByMethod2 = RationalNumber.GetObjectByString("2.5"); // object (uses regular expressions)
            RationalNumber numbGetByMethod3 = RationalNumber.GetObjectByString("4/10");


            Console.WriteLine(numbGetByMethod1 > numbGetByMethod2); // examples for overloading operators
            Console.WriteLine(numbGetByMethod1 < numbGetByMethod2);
            Console.WriteLine(numbGetByMethod1 + numbGetByMethod2);


            Console.WriteLine(numbGetByMethod1.ToString("A")); // example for overloading ToString method.
            Console.WriteLine(numbGetByMethod2.ToString("B")); // it also takes parameter(A\B\C) to
            Console.WriteLine(numbGetByMethod1.ToString("C")); // display string in different formats


            if (numbGetByMethod1.Equals(numbGetByMethod3))      // example for overloading the interface of comparison
            {
                Console.WriteLine("numbers are equal");
            }


            int x = (int)numbGetByMethod2;                     // examples for explicit\implicit casting to other types

            Console.WriteLine(x);

            double y = (double)numbGetByMethod1;

            Console.WriteLine(y);

            RationalNumber number2 = 24;
            RationalNumber number5 = 2.4;

            Console.WriteLine(number5.ToString("A"));
        }
Пример #4
0
        static void Main(string[] args)
        {
            RationalNumber n1 = new RationalNumber(5, 7);
            RationalNumber n2 = new RationalNumber(5, 2);
            RationalNumber n  = n1 + n2;

            Console.WriteLine($"{n1} + {n2} = " + n);
            n = n1 - n2;
            Console.WriteLine($"{n1} - {n2} = " + n.ToString("."));
            n = n1 * n2;
            Console.WriteLine($"{n1} * {n2} = " + n);
            n = n1 / n2;
            Console.WriteLine($"{n1} / {n2} = " + n);
            Console.WriteLine($"{n1} > {n2} - " + (n1 > n2));
            Console.WriteLine($"{n1} < {n2} - " + (n1 < n2));
            Console.WriteLine($"{n1} == {n2} - " + (n1 == n2));
            Console.WriteLine($"{n1} != {n2} - " + (n1 != n2));
            Console.WriteLine("parse -1.5 " + RationalNumber.Parse("-1.5"));
            Console.WriteLine("parse -1/3 " + RationalNumber.Parse("-1/3"));
            n.Reduce();
            Console.WriteLine("reduce 10/35 " + n);
            Console.WriteLine($"(int){n} " + (int)n);
            Console.WriteLine($"(double){n} " + (double)n);
        }
Пример #5
0
        static void Main(string[] args)
        {
            RationalNumber a = new RationalNumber(15, 30);
            RationalNumber b = new RationalNumber(10, 8);
            RationalNumber c = new RationalNumber(8, 4);
            RationalNumber d = new RationalNumber(34, 17);
            RationalNumber[] rationalNumbers = new RationalNumber[] { a, b, c, d };

            foreach (RationalNumber i in rationalNumbers)
            {
                RationalNumber.reduceFraction(i);
            }

            Array.Sort(rationalNumbers);
            foreach (RationalNumber i in rationalNumbers)
            {
                RationalNumber.getType(i, "in decimal");
            }

            Console.WriteLine();

            Console.WriteLine(a > b);
            Console.WriteLine(d < c);
            Console.WriteLine(d == c);
            Console.WriteLine(b.Equals(d));
            Console.WriteLine(d.Equals(c));
            Console.WriteLine();

            RationalNumber rationalNumber = new RationalNumber();
            rationalNumber = a + b;
            RationalNumber.reduceFraction(rationalNumber);
            RationalNumber.getType(rationalNumber, "with fractions");
            Console.WriteLine();
            rationalNumber = a - b;
            RationalNumber.reduceFraction(rationalNumber);
            RationalNumber.getType(rationalNumber, "with fractions");
            Console.WriteLine();
            rationalNumber = a * b;
            RationalNumber.reduceFraction(rationalNumber);
            RationalNumber.getType(rationalNumber, "with fractions");
            Console.WriteLine();
            rationalNumber = a / b;
            RationalNumber.reduceFraction(rationalNumber);
            RationalNumber.getType(rationalNumber, "with fractions");
            Console.WriteLine();
            a++;
            RationalNumber.reduceFraction(rationalNumber);
            RationalNumber.getType(a, "with fractions");
            Console.WriteLine();
            a--;
            RationalNumber.reduceFraction(rationalNumber);
            RationalNumber.getType(a, "with fractions");
            Console.WriteLine();
            rationalNumber = a + 10;
            RationalNumber.reduceFraction(rationalNumber);
            RationalNumber.getType(rationalNumber, "with fractions");
            Console.WriteLine();

            int buf1;
            double buf2;
            buf1 = a;
            Console.WriteLine(buf1);
            buf2 = (double)a;
            Console.WriteLine(buf2 + "\n");

            Console.WriteLine(a.ToString("in decimal"));
            Console.WriteLine(a.ToString("with fractions"));
            Console.WriteLine();

            RationalNumber num = new RationalNumber();
            string str = "99/17";
            num = (RationalNumber)str;
            RationalNumber.getType(num, "with fractions");
            Console.WriteLine();

            string inputStr = Console.ReadLine();
            string[] rationalNumbers1 = inputStr.Split(' ');
            List<RationalNumber> listNumbers = new List<RationalNumber>();
            RationalNumber.processStr(rationalNumbers1, listNumbers);
            foreach (RationalNumber i in listNumbers)
            {
                Console.WriteLine(i.ToString("in decimal"));
            }
        }
Пример #6
0
        static void Main(string[] args)
        {
            RationalNumber number1, number2;
            int            numerator1, denominator1;

            Console.WriteLine("Здравствуйте!");
            Console.WriteLine("Введите числитель первой рациональной дроби:");
            numerator1 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Введите знаменатель первой рациональной дроби:");
            denominator1 = Convert.ToInt32(Console.ReadLine());
            number1      = new RationalNumber(numerator1, denominator1);
            Console.WriteLine("Введите второе число в формате a/b");
            string form = Console.ReadLine();

            number2 = new RationalNumber(form);
            while (true)
            {
                Console.WriteLine();
                Console.WriteLine("Выберите действие:");
                Console.WriteLine("1.Сложение");
                Console.WriteLine("2.Вычетание");
                Console.WriteLine("3.Умнажение");
                Console.WriteLine("4.Деление");
                Console.WriteLine("5.Вывести большее число");
                Console.WriteLine("6.Вывести меньшее число");
                Console.WriteLine("7.Проверить числа на равенство");
                Console.WriteLine("8.Преобразовать в int");
                Console.WriteLine("9.Преобразовать в double");
                Console.WriteLine("10.Завешить работу");
                int otvet = Convert.ToInt32(Console.ReadLine());
                Console.Clear();
                switch (otvet)
                {
                case 1: Console.WriteLine($"{(number1 + number2).ToString()}"); break;

                case 2: Console.WriteLine($"{(number1 - number2).ToString()}"); break;

                case 3: Console.WriteLine($"{(number1 * number2).ToString()}"); break;

                case 4: Console.WriteLine($"{(number1 / number2).ToString()}"); break;

                case 5:
                    if (number1 > number2)
                    {
                        Console.WriteLine($"{number1.ToString()}");
                    }
                    else
                    {
                        Console.WriteLine($"{number2.ToString()}");
                    }
                    break;

                case 6:
                    if (number1 < number2)
                    {
                        Console.WriteLine($"{number1.ToString()}");
                    }
                    else
                    {
                        Console.WriteLine($"{number2.ToString()}");
                    }
                    break;

                case 7:
                    if (number1 == number2)
                    {
                        Console.WriteLine("Числа равны");
                    }
                    else
                    {
                        Console.WriteLine("Числа не равны");
                    }
                    break;

                case 8: Console.WriteLine($"{(int)number1}    {(int)number2}"); break;

                case 9: Console.WriteLine($"{(double)number1}    {(double)number2}"); break;

                case 10: return;

                default:
                    break;
                }
            }
        }