Monday, August 1, 2016

Simple Calculator with Order of Operations in C#



Here is the Calculator with order of operations for multiplication, division, addition, and subtraction. No parenthesis, exponents, or other things included. This is using the tree method. We check for the operation that would be done last from right to left, and this is the first node of the tree. Keep in mind: "multiplication and division are of equal precedence, as are addition and subtraction." Here is an example of how the tree works:
Source: http://math.hws.edu/eck/cs225/s03/binary_trees/expressionTree.gif

This is the Program.cs file below.

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


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

using System;
using System.Text.RegularExpressions;

namespace SimpleCalculator
{
    public class Calc
    {
        public double Solve(string equation)
        {
            // Remove all spaces
            equation = Regex.Replace(equation, @"\s+", "");

            Operation operation = new Operation();
            operation.Parse(equation);

            double result = operation.Solve();

            return result;
        }
    }

    public class Operation
    {
        public Operation LeftNumber { get; set; }
        public string Operator { get; set; }
        public Operation RightNumber { get; set; }

        private Regex additionSubtraction = new Regex("[+-]", RegexOptions.RightToLeft);
        private Regex multiplicationDivision = new Regex("[*/]", RegexOptions.RightToLeft);

        public void Parse(string equation)
        {
            var operatorLocation = additionSubtraction.Match(equation);
            if (!operatorLocation.Success)
            {
                operatorLocation = multiplicationDivision.Match(equation);
            }

            if (operatorLocation.Success)
            {
                Operator = operatorLocation.Value;

                LeftNumber = new Operation();
                LeftNumber.Parse(equation.Substring(0, operatorLocation.Index));
                
                RightNumber = new Operation();
                RightNumber.Parse(equation.Substring(operatorLocation.Index + 1));
            }
            else
            {
                Operator = "v";
                result = double.Parse(equation);
            }
        }

        private double result;

        public double Solve()
        {
            switch (Operator)
            {
                case "v":
                    break;
                case "+":
                    result = LeftNumber.Solve() + RightNumber.Solve();
                    break;
                case "-":
                    result = LeftNumber.Solve() - RightNumber.Solve();
                    break;
                case "*":
                    result = LeftNumber.Solve() * RightNumber.Solve();
                    break;
                case "/":
                    result = LeftNumber.Solve() / RightNumber.Solve();
                    break;
                default:
                    throw new Exception("Call Parse first.");
            }

            return result;
        }
    }
}