using System.Text; using System.Collections; using System.Collections.Generic; using System; static void Main(string[] args) { string inFix= string.Empty; Console.Write("Enter InFix Expression: "); inFix = Console.ReadLine().Replace(" ", string.Empty); Console.WriteLine("\nPostFix: {0}",ConvertToPostFix(inFix)); Console.ReadKey(); } private static string ConvertToPostFix(string inFix) { StringBuilder postFix = new StringBuilder(); char arrival; Stack<char> oprerator = new Stack<char>();//Creates a new Stack foreach (char c in inFix.ToCharArray())//Iterates characters in inFix { if (Char.IsNumber(c)) postFix.Append(c); else if (c == '(') oprerator.Push(c); else if (c == ')')//Removes all previous elements from Stack and puts them in //front of PostFix. { arrival = oprerator.Pop(); while (arrival != '(') { postFix.Append(arrival); arrival = oprerator.Pop(); } } else { if (oprerator.Count != 0 && Predecessor(oprerator.Peek(), c))//If find an operator { arrival = oprerator.Pop(); while (Predecessor(arrival, c)) { postFix.Append(arrival); if (oprerator.Count == 0) break; arrival = oprerator.Pop(); } oprerator.Push(c); } else oprerator.Push(c);//If Stack is empty or the operator has precedence } } while (oprerator.Count > 0) { arrival = oprerator.Pop(); postFix.Append(arrival); } return postFix.ToString(); } private static bool Predecessor(char firstOperator, char secondOperator) { string opString = "(+-*/%"; int firstPoint, secondPoint; int[] precedence = { 0, 12, 12, 13, 13, 13 };// "(" has less prececence firstPoint = opString.IndexOf(firstOperator); secondPoint = opString.IndexOf(secondOperator); return (precedence[firstPoint] >= precedence[secondPoint]) ? true : false; }
prog.cs(5,7): error CS1525: Unexpected symbol `void', expecting `class', `delegate', `enum', `interface', `partial', or `struct' prog.cs(5,24): warning CS0658: `]' is invalid attribute target. All attributes in this attribute section will be ignored prog.cs(66,69): warning CS0658: `false' is invalid attribute target. All attributes in this attribute section will be ignored prog.cs(67,1): error CS8025: Parsing error
public class App { public static void Main(string [] args) { // ... } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)