using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
public static class Program
public static void Main()
Console.WriteLine("Hello World");
string myString = "5/5 + 5 *-5 - 5*5 + 27";
myString = "1/2 + 3 *-4 - 5*6 + 7";
myString = myString.RemoveWhitespace();
string result = RecursiveEvaluate(myString);
Console.WriteLine("result:" + result);
public static string[] Operators = { "*", "/", "+", "-" };
public static string RecursiveEvaluate(string lineRaw)
for (int i = 0; i < Operators.Length; i++)
elements_LB = lineRaw.Split(op);
while (elements_LB.Length > 1)
Console.WriteLine("PROCESSING lineRaw: " +lineRaw+ " / with op'" + op + "' operator LEVEL " + LEVEL);
int firstIndex = FindFirstIndex(elements_LB);
string leftSide = elements_LB[firstIndex];
string rightSide = elements_LB[firstIndex+1];
Console.WriteLine("leftSide: " +leftSide+ " / rightSide:" + rightSide);
string left = GetLastElementOnLeft(leftSide);
string right = GetFirstElementOnRight(rightSide);
Console.WriteLine("left: " +left+ " / right:" + right);
string result = op.Operator(int.Parse(left), int.Parse(right)).ToString();
regex = new Regex(Regex.Escape(left + op + right));
lineRaw = regex.Replace(lineRaw, result, 1);
elements_LB = lineRaw.Split(op);
private static string GetLastElementOnLeft(string leftSide)
for (int i = leftSide.Length-1; i >= 0 ; i--)
if (Array.IndexOf(Operators, leftSide[i].ToString()) != -1)
string returnString = leftSide.Substring(i+1, counter);
if (leftSide[i].ToString() == "-") returnString+= "-" + returnString;
Console.WriteLine(leftSide + " / leftSide[i]: " + leftSide[i]);
Console.WriteLine("returnString: " + returnString);
private static string GetFirstElementOnRight(string rightSide)
for (int i = 0; i < rightSide.Length; i++)
if (i == 0 && rightSide[i].ToString() == "-") continue;
if (Array.IndexOf(Operators, rightSide[i].ToString()) != -1)
return rightSide.Substring(0, i );
public static int Operator(this string op, int x, int y)
default: throw new Exception("invalid logic");
private static void DebugSoFar(string[] words)
Console.WriteLine("split result length: " + words.Length);
for (int i = 0; i < words.Length; i++)
Console.WriteLine(words[i] + ", ");
private static int FindFirstIndex(string[] words)
for (int i = 0; i < words.Length; i++)
if (words[i] != "") return i;
private static int FindLastIndex(string[] words)
for (int i = 0; i < words.Length; i++)
if (words[i] != "") lastInstance = i;
public static string RemoveWhitespace(this string input)
return new string(input.ToCharArray()
.Where(c => !Char.IsWhiteSpace(c))
public static string KeyValueSeparator = "=";
public static string[] CommentCharacters = { "#", ";", "//" };