fork download
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Collections;
  6.  
  7. public class InFixtoPostFix
  8. {
  9. public static void Main()
  10. {
  11. // your code goes here
  12.  
  13. string[] inputString = {"5","+","3","*","6","-","4","+","8"};
  14. string[] outputString;
  15. outputString=InFixtoPostFix.infixToPostFix(inputString);
  16. foreach(string token in outputString)
  17. {
  18. Console.WriteLine(token);
  19. }
  20. }
  21.  
  22. public enum Precedence
  23. {
  24. Prod = 3,Sub = 2, Add = 1
  25. }
  26.  
  27.  
  28. /// <summary>
  29. /// converts an infix expression to postfix expression
  30. /// *,+,- in the order of precedence
  31. /// </summary>
  32. /// <param name="inputExpression"></param>
  33. /// <returns></returns>
  34. public static string[] infixToPostFix(string[] inputExpression)
  35. {
  36. Stack<string> opStack;
  37. string[] output;
  38. int i, j;
  39.  
  40. if (inputExpression == null) return null;
  41. opStack = new Stack<string>();
  42. output = new string[inputExpression.Length];
  43.  
  44. i=0;
  45. j = 0;
  46.  
  47. while (i < inputExpression.Length)
  48. {
  49. // check if token is number
  50. if (IsNumber(inputExpression[i]))
  51. {
  52. output[j++] = inputExpression[i++];
  53. }
  54. // check if token is operator
  55. else if(IsOperator(inputExpression[i]))
  56. {
  57. if(opStack.Count() != 0)
  58. {
  59. while (getPrecedence(opStack.Peek().ToString()) >= getPrecedence(inputExpression[i]))
  60. {
  61. output[j++] = opStack.Pop().ToString();
  62. if (opStack.Count() == 0) break;
  63. }
  64.  
  65. opStack.Push(inputExpression[i++]);
  66. }
  67. else
  68. opStack.Push(inputExpression[i++]);
  69. }
  70. // non-operator return null string
  71. else
  72. {
  73. return null;
  74. }
  75. }
  76. while (opStack.Count() != 0)
  77. {
  78. output[j++] = opStack.Pop().ToString();
  79. }
  80.  
  81. return output;
  82. }
  83.  
  84. public static int getPrecedence(string inputString)
  85. {
  86. switch (inputString)
  87. {
  88. case "*":
  89. return (int)Precedence.Prod;
  90. case "-":
  91. return (int)Precedence.Sub;
  92. case "+":
  93. return (int)Precedence.Add;
  94. }
  95.  
  96. return -1;
  97. }
  98.  
  99. public static bool IsNumber(string inputString)
  100. {
  101. try
  102. {
  103. Convert.ToInt32(inputString);
  104. return true;
  105. }
  106. catch (System.FormatException ex)
  107. {
  108. Console.WriteLine("Not a Number");
  109. return false;
  110. }
  111. }
  112.  
  113. public static bool IsOperator(string inputstring)
  114. {
  115. switch(inputstring)
  116. {
  117. case "*": return true;
  118. case "+": return true;
  119. case "-": return true;
  120. }
  121. return false;
  122. }
  123. }
Success #stdin #stdout 0.03s 33992KB
stdin
Standard input is empty
stdout
Not a Number
Not a Number
Not a Number
Not a Number
5
3
6
*
4
-
+
8
+