Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            // Inicializacion del constructor
            Calculator calc = new Calculator();                         // cargamos el constructor


            int    operand1, operand2, position;
            char   operation;
            string yesNo;
            bool   keepCalc, keepCalcQuestion;
            int    choice;

            operand1         = 0;
            operand2         = 0;
            position         = 0;
            keepCalc         = false;
            keepCalcQuestion = true;
            string numeroStr;

            choice = 0;


            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write("Welcome to ");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("Mario's ");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write("&");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write(" Dani's ");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Calculator!");
            Console.ResetColor();

            Console.WriteLine();


            while (keepCalc == false)             // Quiero seguir usando la calculadora?
            {
                keepCalcQuestion = true;

                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("Make a choice between these options:");


                Console.WriteLine("\n\n\t\t1. Add operation\n\t\t2. Delete operation\n\t\t3. Evaluate operation\n\t\t4. Print an operation at a given position\n\t\t5. Print all operations\n\t\t6. Print operation with highest result\n\t\t7. Print an specific (+, -, *, /) operation with highest result\n\t\t8. Any negative values?\n\t\t9. EXIT");

                Console.WriteLine();



                Console.Write("Write a number between 1 and 9: ");
                numeroStr = Console.ReadLine();

                if (!checkIsInRange(numeroStr))
                {
                    choice = Convert.ToInt32(numeroStr);
                }

                while (checkIsInRange(numeroStr) || choice < 1 || choice > 9)
                {
                    Console.Write("Write a number between 1 and 9: ");
                    numeroStr = Console.ReadLine();

                    if (!checkIsInRange(numeroStr))
                    {
                        choice = Convert.ToInt32(numeroStr);
                    }
                }

                switch (choice)
                {
                case 1:                        //Hace una operación

                    Console.Clear();

                    Console.Write("Give me the first number: ");
                    numeroStr = Console.ReadLine();

                    if (!checkIsInRange(numeroStr))
                    {
                        operand1 = Convert.ToInt32(numeroStr);
                    }

                    while (checkIsInRange(numeroStr))
                    {
                        Console.Write("Give me the first number: ");
                        numeroStr = Console.ReadLine();

                        if (!checkIsInRange(numeroStr))
                        {
                            operand1 = Convert.ToInt32(numeroStr);
                        }
                    }

                    Console.Write("What operation do you want?('+' , '-' , '*' , '/'): ");
                    operation = Convert.ToChar(Console.ReadLine());
                    Console.Write("Give me another number: ");

                    numeroStr = Console.ReadLine();

                    if (!checkIsInRange(numeroStr))
                    {
                        operand2 = Convert.ToInt32(numeroStr);
                    }

                    while (checkIsInRange(numeroStr))
                    {
                        Console.Write("Give me another number: ");
                        numeroStr = Console.ReadLine();

                        if (!checkIsInRange(numeroStr))
                        {
                            operand2 = Convert.ToInt32(numeroStr);
                        }
                    }


                    if (operation == '/' && operand2 == 0)
                    {
                        Console.WriteLine("Math Error");
                    }

                    calc.addOperation(operation, operand1, operand2);

                    Console.WriteLine();

                    while (keepCalcQuestion)
                    {
                        Console.Write("Keep using calculator?'Y' for YES 'N' for NO): ");
                        yesNo = Console.ReadLine();

                        if (yesNo == "Y" || yesNo == "y")
                        {
                            keepCalc         = false;
                            keepCalcQuestion = false;
                            Console.Clear();
                        }

                        if (yesNo == "N" || yesNo == "n")
                        {
                            Console.Clear();
                            Console.WriteLine("Bye bye!");
                            Console.ReadKey();
                            keepCalc         = true;
                            keepCalcQuestion = false;
                        }
                    }
                    break;

                case 2:                        //Elimina una operación

                    Console.Clear();

                    Console.Write("What operation do you want to delete?(1 to 10): ");
                    position = Convert.ToInt32(Console.ReadLine());
                    calc.deleteOperation(position - 1);



                    // Console.WriteLine("Operation " + position + " deleted.");
                    while (keepCalcQuestion)
                    {
                        Console.Write("Keep using calculator?'Y' for YES 'N' for NO): ");
                        yesNo = Console.ReadLine();

                        if (yesNo == "Y" || yesNo == "y")
                        {
                            keepCalc         = false;
                            keepCalcQuestion = false;
                            Console.Clear();
                        }

                        if (yesNo == "N" || yesNo == "n")
                        {
                            Console.Clear();
                            Console.WriteLine("Bye bye!");
                            Console.ReadKey();
                            keepCalc         = true;
                            keepCalcQuestion = false;
                        }
                    }
                    break;

                case 3:                        //Te da el resultado de una operación

                    Console.Clear();

                    Console.Write("What operation do you want to evaluate?(1 to 10): ");
                    position = Convert.ToInt32(Console.ReadLine());


                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine(calc.evalOperation(position - 1));
                    Console.ForegroundColor = ConsoleColor.Yellow;

                    while (keepCalcQuestion)
                    {
                        Console.Write("Keep using calculator?'Y' for YES 'N' for NO): ");
                        yesNo = Console.ReadLine();

                        if (yesNo == "Y" || yesNo == "y")
                        {
                            keepCalc         = false;
                            keepCalcQuestion = false;
                            Console.Clear();
                        }

                        if (yesNo == "N" || yesNo == "n")
                        {
                            Console.Clear();
                            Console.WriteLine("Bye bye!");
                            Console.ReadKey();
                            keepCalc         = true;
                            keepCalcQuestion = false;
                        }
                    }
                    break;

                case 4:                        //Enseña una operación

                    Console.Clear();

                    Console.Write("What operation do you want to see?(1 to 10): ");
                    position = Convert.ToInt32(Console.ReadLine());

                    calc.printOperation(position - 1);

                    while (keepCalcQuestion)
                    {
                        Console.Write("Keep using calculator?'Y' for YES 'N' for NO): ");
                        yesNo = Console.ReadLine();

                        if (yesNo == "Y" || yesNo == "y")
                        {
                            keepCalc         = false;
                            keepCalcQuestion = false;
                            Console.Clear();
                        }

                        if (yesNo == "N" || yesNo == "n")
                        {
                            Console.Clear();
                            Console.WriteLine("Bye bye!");
                            Console.ReadKey();
                            keepCalc         = true;
                            keepCalcQuestion = false;
                        }
                    }
                    break;

                case 5:                        //Enseña todas las operaciones

                    Console.Clear();

                    Console.ForegroundColor = ConsoleColor.Green;
                    calc.printAllOperations();
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    while (keepCalcQuestion)
                    {
                        Console.Write("Keep using calculator?'Y' for YES 'N' for NO): ");
                        yesNo = Console.ReadLine();

                        if (yesNo == "Y" || yesNo == "y")
                        {
                            keepCalc         = false;
                            keepCalcQuestion = false;
                            Console.Clear();
                        }

                        if (yesNo == "N" || yesNo == "n")
                        {
                            Console.Clear();
                            Console.WriteLine("Bye bye!");
                            Console.ReadKey();
                            keepCalc         = true;
                            keepCalcQuestion = false;
                        }
                    }
                    break;

                case 6:                        //Enseña la operación con el resultado más alto

                    Console.Clear();
                    Console.ForegroundColor = ConsoleColor.Green;
                    calc.printHigherResultOperation();
                    Console.ForegroundColor = ConsoleColor.Yellow;

                    while (keepCalcQuestion)
                    {
                        Console.Write("Keep using calculator?'Y' for YES 'N' for NO): ");
                        yesNo = Console.ReadLine();

                        if (yesNo == "Y" || yesNo == "y")
                        {
                            keepCalc         = false;
                            keepCalcQuestion = false;
                            Console.Clear();
                        }

                        if (yesNo == "N" || yesNo == "n")
                        {
                            Console.Clear();
                            Console.WriteLine("Bye bye!");
                            Console.ReadKey();
                            keepCalc         = true;
                            keepCalcQuestion = false;
                        }
                    }
                    break;

                case 7:                        //Enseña una operación con el resultado más alto dependiendo del signo que le des

                    Console.Clear();


                    Console.Write("What higher result sign do you want to see?('+' , '-' , '*' , '/'): ");
                    operation = Convert.ToChar(Console.ReadLine());



                    Console.ForegroundColor = ConsoleColor.Green;
                    calc.printHigherResultOperation(operation);
                    Console.ForegroundColor = ConsoleColor.Yellow;

                    while (keepCalcQuestion)
                    {
                        Console.Write("Keep using calculator?'Y' for YES 'N' for NO): ");
                        yesNo = Console.ReadLine();

                        if (yesNo == "Y" || yesNo == "y")
                        {
                            keepCalc         = false;
                            keepCalcQuestion = false;
                            Console.Clear();
                        }

                        if (yesNo == "N" || yesNo == "n")
                        {
                            Console.Clear();
                            Console.WriteLine("Bye bye!");
                            Console.ReadKey();
                            keepCalc         = true;
                            keepCalcQuestion = false;
                        }
                    }
                    break;

                case 8:                        //Dice si hay números negativos

                    calc.anyNegativeOperation();

                    if (calc.anyNegativeOperation())
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("Yes, there is at least one negative operation\n");
                        Console.ForegroundColor = ConsoleColor.Yellow;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Nope, there aren't negative numbers\n");
                        Console.ForegroundColor = ConsoleColor.Yellow;
                    }

                    while (keepCalcQuestion)
                    {
                        Console.Write("Keep using calculator?'Y' for YES 'N' for NO): ");
                        yesNo = Console.ReadLine();

                        if (yesNo == "Y" || yesNo == "y")
                        {
                            keepCalc         = false;
                            keepCalcQuestion = false;
                            Console.Clear();
                        }

                        if (yesNo == "N" || yesNo == "n")
                        {
                            Console.Clear();
                            Console.WriteLine("Bye bye!");
                            Console.ReadKey();
                            keepCalc         = true;
                            keepCalcQuestion = false;
                        }
                    }
                    break;

                case 9:                        //Exit
                    keepCalc = true;

                    Console.Clear();
                    Console.WriteLine("Bye bye!");
                    Console.ReadKey();


                    break;
                }
            }
        }