Monday, July 25, 2016

Simple Calculator without Order of Operations in C#



I have recently started a new job, and with it I decided to do mentoring for a junior developer program. Because of this, I will be working on puzzles / mini-apps with them and I will be posting my solutions to them here for anyone else to use :). It is easiest for me to guide when I solve the problem beforehand. Below is a C# calculator without order of operations (to keep it simple this is a good starting place). Next will be one with order of operations using the tree method.

This is the Program.cs file below.

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            Calculator _calculator = new Calculator();
            _calculator.InitialPrompt();
        }
    }
}


This is some class file, for ex. Calculator.cs below:

using System;

namespace ConsoleApplication1
{
    public class Calculator
    {
        public void InitialPrompt()
        {
            Console.WriteLine("1st number");
            decimal firstNum = int.Parse(Console.ReadLine());

            Console.WriteLine("operation");

            SharedPrompt(firstNum);
        }

        public void ContinuedPrompt(decimal previousSolution)
        {
            decimal firstNum = previousSolution;

            Console.WriteLine("Your previous solution was: " + previousSolution);
            Console.WriteLine("What operation would you like to do to this previous solution?");

            SharedPrompt(firstNum);
        }

        public void SharedPrompt(decimal firstNum)
        {
            string oper = Console.ReadLine();

            Console.WriteLine("2nd number");
            decimal secondNum = int.Parse(Console.ReadLine());

            decimal solution = Calculate(firstNum, oper, secondNum);

            Console.WriteLine("Your answer is: " + solution);
            Console.ReadLine();

            Console.WriteLine("Would you like to continue, do a new problem, or quit? C/N/Q?");
            string nextStep = Console.ReadLine();

            // Continue with same problem
            if (nextStep == "C")
            {
                ContinuedPrompt(solution);
            }

            // New problem
            else if (nextStep == "N")
            {
                InitialPrompt();
            }
        }

        public decimal Calculate(decimal firstNum, string oper, decimal secondNum)
        {
            decimal solution = 0;

            switch (oper)
            {
                case "+":
                    solution = firstNum + secondNum;
                    break;
                case "-":
                    solution = firstNum - secondNum;
                    break;
                case "*":
                    solution = firstNum * secondNum;
                    break;
                case "/":
                    solution = firstNum / secondNum;
                    break;
            }

            return solution;
        }
    }
}