Exemplo n.º 1
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;
        }