static int EvaluateExpression(string formula)
{
string[] numbers = formula.Split(new char[] { '+', '-' }).Where(e=>e.Trim() != "").ToArray();
string[] symbols = Regex.Matches(formula, "[+-]").Cast<Match>().Select(e => e.Value).ToArray();
if(numbers.Length != symbols.Length+1)
throw new FormatException("Не верное количество знаков");
int result;
int i = -1;
try
{
result = int.Parse(numbers[0]);
for (i = 0; i < symbols.Length; i++){
switch(symbols[i])
{
case "+":
result += int.Parse(numbers[i+1]);
break;
case "-":
result -= int.Parse(numbers[i+1]);
break;
}
}
}catch(FormatException e){
throw new FormatException(String.Format("Не верное число '{0}'", numbers[i+1]));
}
return result;
}