예제 #1
0
        static void Main(string[] args)
        {
            bool exitApp = false;

            String prompt = "\nEnter Operations:\n" +
                            "(A)dd\n" +
                            "(S)ubtract\n" +
                            "(M)ultiply\n" +
                            "(D)ivide\n" +
                            "(E)xit\n";

            do
            {
                double number1 = GetDouble("Enter a number: ");
                double number2 = GetDouble("Enter another number: ");
                double result;
                char   c = GetChar(prompt);
                switch (c)
                {
                case 'A':
                    result = CalculatorUtility.Add(number1, number2);
                    PrintResult(number1, number2, " + ", result);
                    break;

                case 'S':
                    result = CalculatorUtility.Minus(number1, number2);
                    PrintResult(number1, number2, " - ", result);
                    break;

                case 'M':
                    result = CalculatorUtility.Multiply(number1, number2);
                    PrintResult(number1, number2, " x ", result);
                    break;

                case 'D':
                    result = CalculatorUtility.Divide(number1, number2);
                    PrintResult(number1, number2, " / ", result);
                    break;

                case 'E':
                    exitApp = true;
                    break;
                }
            } while (!exitApp);
        }
예제 #2
0
        static void Main(string[] args)
        {
            //Create a CalculatorUtility class with
            // Add, Subtract, Multiply and Divide methods
            // these methods take in 2 double parameters
            // and return a double result
            CalculatorUtility cal = new CalculatorUtility();

            bool exitApp = false;

            String prompt = "\nEnter Operations:\n" +
                            "(A)dd\n" +
                            "(S)ubtract\n" +
                            "(M)ultiply\n" +
                            "(D)ivide\n" +
                            "(E)xit\n";

            do
            {
                char c = GetChar(prompt);
                if (c != 'E')
                {
                    double number1 = GetDouble("Enter a number: ");
                    double number2 = GetDouble("Enter another number: ");
                    double result;
                    switch (c)
                    {
                    case 'A':
                        result = cal.Add(number1, number2);
                        PrintResult(number1, number2, "+", result);
                        break;

                    // do the same for -, *, / operations

                    case 'E':
                        exitApp = true;
                        break;
                    }
                }
            } while (!exitApp);
        }