Simple, Quality, Awesome Software

Making a Calculator

Below is the complete code for the Making a Calculator Try It Out! problem.

The Basic Solution

This solution meets the basic requirements of the problem. Compare also, to the next section, which is a bit more user friendly, but uses some techniques that will be learned in the upcoming chapters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpBook.Examples.Calculator
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get the different pieces we need.
            Console.WriteLine("Enter the first number: ");
            int number1 = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter the second number: ");
            int number2 = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter the operation (+, -, *, /, %, or ^):");
            string operation = Console.ReadLine();

            // This will store the result eventually.
            int result;

            // Do something different depending on the operator that the user
            // typed.
            switch (operation)
            {
                case "+":
                    result = number1 + number2;
                    break;
                case "-":
                    result = number1 - number2;
                    break;
                case "*":
                    result = number1 * number2;
                    break;
                case "/":
                    result = number1 / number2;
                    break;
                case "%":
                    result = number1 % number2;
                    break;
                case "^":
                    result = (int)Math.Pow(number1, number2);
                    break;
                default: // This is a catch-all, if the operator is unknown.
                    Console.WriteLine("Unrecognized operator: " + operation);
                    result = 0;
                    break;
            }

            Console.WriteLine(number1 + " " + operation + " " + number2 +
                        " = " + result);

            Console.ReadKey();
        }
    }
}

The Advanced Solution

This uses some things we will learn over the next few chapters to allow the user to enter their math problem all at once (as in “5 + 5”) and it also allows the calculator to run repeatedly for doing multiple problems in a row.

If you’re going through the book sequentially, this example will include a few concepts that you haven’t seen yet.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpBook.Examples.Calculator
{
    class Program
    {
        static void Main(string[] args)
        {
            // Print instructions.
            Console.WriteLine("Enter equation separated by spaces (ex. 5 + 5).");

            // Just keep looping. Perhaps better than this would be to allow
            // the user to type "exit" and break out of the loop then.
            while (true)
            {
                // Get input from the user, split it up into the three pieces
                // (separated by spaces), to be ready to do the calculation.
                Console.Write(">>>");
                string text = Console.ReadLine();
                string[] parts = text.Split(' ');
                int number1 = Convert.ToInt32(parts[0]);
                string operation = parts[1];
                int number2 = Convert.ToInt32(parts[2]);

                // The result will be stored in this variable.
                int result;

                // Do a different operation depending on the text the user
                // supplied.
                switch (operation)
                {
                    case "+":
                        result = number1 + number2;
                        break;
                    case "-":
                        result = number1 - number2;
                        break;
                    case "*":
                        result = number1 * number2;
                        break;
                    case "/":
                        result = number1 / number2;
                        break;
                    case "%":
                        result = number1 % number2;
                        break;
                    case "^":
                        result = (int)Math.Pow(number1, number2);
                        break;
                    default:
                        Console.WriteLine("Unrecognized operator: " +
                                    operation);
                        result = 0;
                        break;
                }

                // Print out the results.
                Console.WriteLine(number1 + " " + operation + " " + number2
                            + " = " + result);
            }
        }
    }
}