Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Fraction fract1 = new Fraction(200, 600);
            Fraction fract2 = new Fraction()
            {
                Numerator   = 3,
                Denominator = 9
            };


            Console.WriteLine("Даны два дробных числа, {0} и {1}", fract1.GetStringValue(), fract2.GetStringValue());

            Console.WriteLine("При их сложении получим дробное число {0}, которое сокращается до {1} \nи записывается в десятичном формате с точностью до 3-го знака как {2:F3}.\n",
                              (fract1.Plus(fract2, false)).GetStringValue(),
                              (fract1.Plus(fract2, true)).GetStringValue(),
                              (fract1.Plus(fract2, false)).GetDoubleValue());

            Console.WriteLine("Вычислив разность получим дробное число {0}, которое сокращается до {1} \nи записывается в десятичном формате с точностью до 3-го знака как {2:F3}.\n",
                              (fract1.Minus(fract2, false)).GetStringValue(),
                              (fract1.Minus(fract2, true)).GetStringValue(),
                              (fract1.Minus(fract2, false)).GetDoubleValue());

            Console.WriteLine("Произведением данных дробей будет дробное число {0}, которое сокращается до {1} \nи записывается в десятичном формате с точностью до 3-го знака как {2:F3}.\n",
                              (fract1.Multi(fract2, false)).GetStringValue(),
                              (fract1.Multi(fract2, true)).GetStringValue(),
                              (fract1.Multi(fract2, false)).GetDoubleValue());

            Console.WriteLine("Частным же будет дробное число {0}, которое сокращается до {1} \nи записывается в десятичном формате с точностью до 3-го знака как {2:F3}.\n",
                              (fract1.Divide(fract2, false)).GetStringValue(),
                              (fract1.Divide(fract2, true)).GetStringValue(),
                              (fract1.Divide(fract2, false)).GetDoubleValue());

            //Pause
            Console.ReadKey();
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            bool endApp = false;

            Console.WriteLine("Fraction calculator in C#\r");
            Console.WriteLine("-------------------------\n");

            while (!endApp)
            {
                //First fraction enter and check
                Console.WriteLine("Enter first fraction:");

                string n1 = Console.ReadLine();
                int    cleanN1;

                while (!int.TryParse(n1, out cleanN1))
                {
                    Console.Write("This is not valid input. Please enter an integer value: ");
                    n1 = Console.ReadLine();
                }

                string d1 = Console.ReadLine();
                int    cleanD1;

                while (!int.TryParse(d1, out cleanD1) || cleanD1 == 0)
                {
                    Console.Write("This is not valid input. Please enter an integer not zero value: ");
                    d1 = Console.ReadLine();
                }

                //Second fraction enter and check
                Console.WriteLine("Enter second fraction:");

                string n2 = Console.ReadLine();
                int    cleanN2;

                while (!int.TryParse(n2, out cleanN2))
                {
                    Console.Write("This is not valid input. Please enter an integer value: ");
                    n2 = Console.ReadLine();
                }

                string d2 = Console.ReadLine();
                int    cleanD2;

                while (!int.TryParse(d2, out cleanD2) || cleanD2 == 0)
                {
                    Console.Write("This is not valid input. Please enter an integer not zero value: ");
                    d2 = Console.ReadLine();
                }

                //Create objects of Fraction class
                Fraction a = new Fraction(cleanN1, cleanD1);
                Fraction b = new Fraction(cleanN2, cleanD2);

                Console.WriteLine("Choose an operator from the following list:");
                Console.WriteLine("\ta - Add");
                Console.WriteLine("\ts - Subtract");
                Console.WriteLine("\tm - Multiply");
                Console.WriteLine("\td - Divide");
                Console.Write("Your option? ");

                string op     = Console.ReadLine();
                string result = "";

                Fraction c;

                switch (op)
                {
                case "a":
                    c      = Fraction.Add(a, b);
                    result = a.Output() + "+" + b.Output() + "=" + c.Output();
                    break;

                case "s":
                    c      = Fraction.Subtract(a, b);
                    result = a.Output() + "+" + b.Output() + "=" + c.Output();
                    break;

                case "m":
                    c      = Fraction.Multiply(a, b);
                    result = a.Output() + "+" + b.Output() + "=" + c.Output();
                    break;

                case "d":
                    c      = Fraction.Divide(a, b);
                    result = a.Output() + "+" + b.Output() + "=" + c.Output();
                    break;

                default:
                    break;
                }

                if (string.IsNullOrEmpty(result))
                {
                    Console.WriteLine("This operation will result in a mathematical error.\n");
                }
                else
                {
                    Console.WriteLine("Result: " + result);
                }

                Console.WriteLine("------------------------\n");

                Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: ");
                if (Console.ReadLine() == "n")
                {
                    endApp = true;
                }

                Console.WriteLine("\n");
            }
            return;
        }