You could use some simple recusive method like this:
public int Calculate(string expression)
{
int result = 0;
string[] expressions = expression.Split('+');
if (expressions.Length > 1)
{
result = 0;
foreach (string expr in expressions)
result += Calculate(expr);
return result;
}
expressions = expression.Split('-');
if (expressions.Length > 1)
{
result = Calculate(expressions[0]);
for (int i = 1; i < expressions.Length; i++)
result -= Calculate(expressions[i]);
return result;
}
expressions = expression.Split('*');
if (expressions.Length > 1)
{
result = 1;
foreach (string expr in expressions)
result *= Calculate(expr);
return result;
}
expressions = expression.Split('/');
if (expressions.Length > 1)
{
result = Calculate(expressions[0]);
for (int i = 1; i < expressions.Length; i++)
result /= Calculate(expressions[i]);
return result;
}
if (!int.TryParse(expression, out result))
throw new ArgumentException("Expression was not nummeric", "expression");
return result;
}
It is very simple and it doesn't support ( and ) and negative number, it only supports +, -, * and /.
You use it like this:
int result = Calculate("20*10+200/100");
Good luck with your quest.