static void Main(string[] args) { values val = new values(); Console.WriteLine("Please enter an infix expression below. I will convert it to a postfix expression using my mechanical mind!"); val.GetPuller = Console.ReadLine(); do { val.GetVal = val.GetPuller[val.GetProgress].ToString(); IsOperator(val.GetVal); val.GetProgress++; } while (val.GetPuller.Length > val.GetProgress); while (val.GetProgress == val.GetPuller.Length && val.GetStack.Count > 0) { Console.Write(val.GetStack.Pop()); } Console.ReadLine(); }
static string IsOperator(string charer) { values val = new values(); Regex r = new Regex("^[a-zA-Z ]+$"); //appends all operators to stack bracket if (charer == "(") { val.GetBrackets++; } //expels all items stored in bracket stack to the console if (charer == ")") { val.GetBrackets--; if (val.GetBrackets > 0) { val.GetBracket = true; } else { val.GetBracket = false; } while (val.GetBrack.Count != 0 && val.GetBracket == false) { Console.Write(val.GetBrack.Pop()); break; } } //will pop the stack if it isn't null, but the addition value will not be printed (Addition operator) else if (charer == "+") { if (val.GetBrackets == 0) { if (val.GetStack.Count > 0) { while (val.GetStack.Count > 0) { Console.Write(val.GetStack.Pop()); val.GetStack.Push(charer); break; } } else { val.GetStack.Push(charer); } } if (val.GetBrackets > 0) { if (val.GetBrack.Count > 0) { Console.Write(val.GetBrack.Pop()); } val.GetBrack.Push(charer); } } //multiplication operator else if (charer == "*") { if (val.GetBrackets == 0) { while (val.GetStack.Count != 0) { if (!val.GetStack.Contains("+")) { Console.Write(val.GetStack.Pop()); break; } } val.GetStack.Push(charer); } if (val.GetBrackets > 0) { if (val.GetBrack.Count > 0) { Console.Write(val.GetBrack.Pop()); } val.GetBrack.Push(charer); } } //division operator else if (charer == "/") { if (val.GetBrackets == 0) { val.GetStack.Push(charer); } if (val.GetBrackets > 0) { val.GetBrack.Push(charer); } } //subtraction operator else if (charer == "-") { if (val.GetBrackets == 0) { if (val.GetStack.Count > 0) { Console.Write(val.GetStack.Pop()); val.GetStack.Push(charer); } else { val.GetStack.Push(charer); } } else if (val.GetBrackets > 0) { val.GetBrack.Push(charer); Console.Write(val.GetBrack.Pop()); } } else if (r.IsMatch(charer)) { Console.Write(charer); } // Console.Write(val.GetStack.Count); // Console.Write(val.GetBracket); return(charer); }