Print this page

Calculator Challenge

I decided to challenge myself to make a parsing calculator. ??Worked out pretty well. ??See comments within the code for how it works.

??

Download from attachments section (A C# Project Source Code - Visual Studio 2015).

Download from attachments section (The binary executable for the program)

??

Here is some of the source code:


// Takes a List of operands & operators and successively pushes them onto a stack. ??Once it finds a closing paren,

// it then goes backwards from there to find it's associated openning paren, popping off the stack until it's found. ??When it does it calculates multiplication & division upon the values

// then it passes the remaining calculations to the plus/minus algorithm, taking care of those. ??Finally passing the resulting value back to the original

// place where the paren was found, placing this resulting calculation in place of the paren. Then it continues to find other parens and calculate those

// in the same way until all calculations have been found correctly. ??If there are calculatoin outside of a paren, those are handled last.

?? ?? ?? ?? public static Double solveParen(List<String> calcList)

?? ?? ?? ?? {

?? ?? ?? ?? ?? ?? Stack<String> parenCalc = new Stack<String>();

?? ?? ?? ?? ?? ?? int j = 0; ?? ?? ????

?? ?? ?? ?? ?? ?? ?? ?? for (int i = 0; i < calcList.Count; ++i)

?? ?? ?? ?? ?? ?? ?? ?? {

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? j = 0;

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? int stackAmount = 0;

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? if (calcList[i] == ")")

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? {

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? j = i - 1;

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? while (calcList[j] != "(")

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? {

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? parenCalc.Push(calcList[j]);

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ++stackAmount;

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? --j;

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? }

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? Double result = solveMultipleDivide(parenCalc, stackAmount);

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? calcList.RemoveRange(j, (i - j + 1));

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? calcList.Insert(j, result.ToString());

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? i = 0;

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? }

?? ?? ?? ?? ?? ?? ?? ?? }

?? ?? ?? ?? ?? ?? ?? ?? if (calcList.Count == 1)

?? ?? ?? ?? ?? ?? ?? ?? {

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? return Convert.ToDouble(calcList[0]);

?? ?? ?? ?? ?? ?? ?? ?? }

?? ?? ?? ?? ?? ?? ?? ?? else

?? ?? ?? ?? ?? ?? ?? ?? {

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? parenCalc.Clear();

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? for (int i = calcList.Count-1; i >= 0; --i)

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? {

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? parenCalc.Push(calcList[i]);

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? }

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? return solveMultipleDivide(parenCalc, calcList.Count);

?? ?? ?? ?? ?? ?? ?? ?? } ?? ?? ?? ?? ????

?? ?? ?? ?? }

// Goes through the provided parentheses Stack, or algorithm snippet, and finds '*' and '/', calculates those calculations,??

// then passes this with what's left of the paren to the 'solvePlusMinus' function

?? ?? ?? ?? public static Double solveMultipleDivide(Stack<String> parenCalc, int stackAmount)

?? ?? ?? ?? {

?? ?? ?? ?? ?? ?? List<String> newResult = new List<String>();

?? ?? ??

?? ?? ?? ?? ?? ?? for (int i = 0; i < stackAmount; i++)

?? ?? ?? ?? ?? ?? { ?? ?? ?? ?? ?? ?? ?? ??

?? ?? ?? ?? ?? ?? ?? ?? newResult.Add(parenCalc.Pop()); ?? ?? ?? ?? ?? ?? ??

?? ?? ?? ?? ?? ?? }

?? ?? ?? ?? ?? ?? for (int i = 0; i < newResult.Count; ++i)

?? ?? ?? ?? ?? ?? {

?? ?? ?? ?? ?? ?? ?? ?? if ((newResult[i] == "*") || (newResult[i] == "/")) {

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? Double aResult = 0;

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? if (newResult[i] == "*")

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? {

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? aResult = Convert.ToDouble(newResult[i-1]) * Convert.ToDouble(newResult[i+1]);

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? }??

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? else if (newResult[i] == "/")??

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? {

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? aResult = Convert.ToDouble(newResult[i-1]) / Convert.ToDouble(newResult[i+1]);

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? }

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? newResult.RemoveRange(i-1, 3);

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? newResult.Insert(i-1, aResult.ToString());

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? i = 0;??

?? ?? ?? ?? ?? ?? ?? ?? }

?? ?? ?? ?? ?? ?? }

?? ?? ?? ?? ?? ?? return solvePlusMinus(newResult);??

?? ?? ?? ?? }

// Handles plus & minus calculations, and returns the resulting scalar back up the line of "order of operation" functions

?? ?? ?? ?? public static Double solvePlusMinus(List<String> nextResult)

?? ?? ?? ?? {

?? ?? ?? ?? ?? ?? for (int i = 0; i < nextResult.Count; ++i)

?? ?? ?? ?? ?? ?? {

?? ?? ?? ?? ?? ?? ?? ?? if ((nextResult[i] == "+") || (nextResult[i] == "-"))

?? ?? ?? ?? ?? ?? ?? ?? {

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? Double aResult = 0;

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? if (nextResult[i] == "+")

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? {

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? aResult = Convert.ToDouble(nextResult[i - 1]) + Convert.ToDouble(nextResult[i + 1]);

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? }

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? else if (nextResult[i] == "-")

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? {

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? aResult = Convert.ToDouble(nextResult[i - 1]) - Convert.ToDouble(nextResult[i + 1]);

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? }

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? nextResult.RemoveRange(i - 1, 3);

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? nextResult.Insert(i - 1, aResult.ToString());

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? i = 0;

?? ?? ?? ?? ?? ?? ?? ?? }

?? ?? ?? ?? ?? ?? }

?? ?? ?? ?? ?? ?? return Convert.ToDouble(nextResult[0]);

?? ?? ?? ?? }