Multiplying/Dividing before Adding/Subtracting I'm working on a calculator for educational purposes and was asked to include a rule that multiplies/divides before it adds/subtracts.

Let's assume I have the following equation:

2 + 2 * 2


Windows built-in calculator would return 8, since it just goes down the line without parantheses.

The correct answer however would be 6.

So my code first splits the entire equation into a List&lt;string&gt;:

string equation = "2 + 2 * 2"
List&lt;string&gt; equationList = equation.Split(' ').ToList();
//Result:
//2
//+
//2
//*
//2


Then it checks if equationList contains an asterisk or slash:

if (equationList.Contains("*")) equationList = PunktVorStrich("*", equationList);
if (equationList.Contains("/")) equationList = PunktVorStrich("/", equationList);


This automatically calls the following function PunktVorStrich:

private List&lt;string&gt; PunktVorStrich(string symbol, List&lt;string&gt; equationList)
{
    while (equationList.Contains(symbol))
    {//loops in case there are multiple instances of the given symbol
        int position = equationList.IndexOf(symbol); //position of the symbol
        int before = int.Parse(equationList.ElementAt(position - 1)); //position of the number before the symbol
        int after = int.Parse(equationList.ElementAt(position + 1)); //position of the number after the symbol
        equationList.RemoveAt(position - 1);
        equationList.RemoveAt(position - 1);
        equationList.RemoveAt(position - 1);//Remove all 3 elements
        //finally calculate and "replace" the equation-part with the result
        if(symbol == "*")
        {
            equationList.Insert(position - 1, (before * after).ToString());
        }else if(symbol == "/")
        {
            equationList.Insert(position - 1, (before / after).ToString());
        }
    }
    return equationList;
    //equationList =
    //2
    //+
    //4
}


And finally the program loops through the remaining equation:

bool number = true; //This ensures that the equation is split between operators and values
int total = 0; //Sum of the current equation
string lastOperator = String.Empty; //Stores the last operator

foreach (string term in equationList)
{
    if (number)
    {
        if (lastOperator == "") total = int.Parse(term);
        if (lastOperator == "+") total = total + int.Parse(term);
        if (lastOperator == "-") total = total - int.Parse(term);
    }
    number= !number;
    lastOperator = term;
}


Finally I can return the equation with

MessageBox.Show(total.ToString());


I'm however not exactly happy with this solution, since I personally think that took a few steps too much to achieve my goal. Is there anything I can do better?
Review


The calculator requires a very specific syntax and depends on whitespace delimiters. As a consumer it is really easy to make a mistake in using the calculator.
I would expect a basic calculator to include parentheses parsing as well as the basic operators +-/*.
You split the input in a series of string tokens List&lt;string&gt; equationList. String is hard to work with once you start extending the calculator. Invest in a custom class Token that has at least a Value (string) and a Type (Operator, Operand, Parenthesis, Whitespace, ..)
Method PunktVorStrich return the same list as provided in the parameters. It could be made void if you allow the provided list to get changed in the method body.
I would not perform the multiplication/division evaluation in the preprocessor. It should be done in a later phase.


Limitations

You have created a preprocessor function that transforms any input string into one that only uses addition and subtraction.

"1 + 1 * 3"


comes out as 

"1 + 3"


Since the expression still remains to be evaluated, you'd have to go over the expression again, this time only dealing with the basic operators. I would not use a function like PunktVorStrich in the preprocessing phase, I would include it in the expression evaluator. This would gain performance, by only iterating the expression once.

By the way, how would you evaluate this expression? There is no consensus in math.

"2 / 3 * 4"


If you want to extend your calculator, you'll notice it would get harder to maintain the evaluator, since you have a fixed format &lt;number&gt;&lt;whitespace&gt;&lt;operator&gt;.. and evaluate left to right. As you noticed, a simple multiplication and division already are special cases to this left to right scanner. The following simple expressions can currently not be parsed.

"1+1"
"(1 + 1) + 1"


Expression Tree Evaluator

What you could do, is tokenize the input, build an expression tree and evaluate the tree. For instance "(1 + 1) + 1" could have a syntax tree:

//             add
//     add          1
//  1       1


You should read more about evaluating expressions using lexers and parsers. Try refactoring the simple evaluator with only +-/* to this structure.