Fire_Tony tenia que escribir un programa que parseara una expresión aritmética infija (3*1/(2-6)), la convirtiera en un arbol binario, para después mostrar la expresión en notación polaca inversa (sufija) y evaluara el resultado.
El pobre no pudo terminar el problema a tiempo, y como me llamo la atención y no tenia mucho que hacer me ofrecí a resolver el problema para que revisara el código y no se viera en ese dilema la próxima ocacion.
El programa por sencillez solo evalúa numeros de 0 al 9 y reconoce los operadores *,+,- y / pero puede ser expandido fácilmente para mas casos.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | class SixPackJoy { public static void Main(string[] args) { Console.WriteLine ("Expression: "); var expression = Console.ReadLine().Replace(" ", ""); var output = new Stack<BinaryNode> (); var stack = new Stack<Operator> (); var index = -1; foreach (var token in expression) { index++; if ("0123456789".Contains(token)) //numeric token { output.Push(new BinaryNode (token)); } else if ("*/+-".Contains(token)) //operator token { var op = new Operator (token); var lastOperator = stack.Count > 0 ? stack.Peek() : null; if (lastOperator != null && !lastOperator.IsParenthesis && op.ComparePrecedence(stack.Peek()) <= 0) { var node = new BinaryNode(stack.Pop().Value); if (output.Count < 2) { Console.WriteLine("Insuficcient operator argumets, process halted"); return; } node.RightChild = output.Pop(); node.LeftChild = output.Pop(); output.Push(node); } stack.Push(op); } else if (token == '(') { stack.Push(new Operator(token)); } else if (token == ')') { while (true) { if (stack.Count == 0) { Console.WriteLine("Parenthesis missmatch at {0}, process halted", index); return; } var op = stack.Pop(); if (op.IsParenthesis) break; if (output.Count < 2) { Console.WriteLine ("Insuficcient operator argumets, process halted"); return; } var node = new BinaryNode (op.Value); node.RightChild = output.Pop (); node.LeftChild = output.Pop (); output.Push (node); } } else { Console.WriteLine("Invalid token '{0}' at {1}, process halted", token, index); return; } } while (stack.Count != 0) { var op = stack.Pop(); if (op.IsParenthesis) { Console.WriteLine ("Parenthesis missmatch, process halted"); return; } if (output.Count < 2) { Console.WriteLine("Insuficcient operator argumets, process halted"); return; } var node = new BinaryNode(op.Value); node.RightChild = output.Pop(); node.LeftChild = output.Pop(); output.Push(node); } if (output.Count != 1) { Console.WriteLine("Invalid expression, process halted"); return; } var ast = output.Pop(); Console.WriteLine("{0} = {1}", ast.GetSufixNotation (), ast.Evaluate()); } } class BinaryNode { public BinaryNode LeftChild; public BinaryNode RightChild; public BinaryNode(char value) { Value = value; } public char Value; public double Evaluate() { if (LeftChild == null) return double.Parse(Value.ToString ()); var operations = new Dictionary<char, Func<double, double, double>> () { {'*', (x, y) => x * y}, {'/', (x, y) => x / y}, {'+', (x, y) => x + y}, {'-', (x, y) => x - y}, }; return operations[Value](LeftChild.Evaluate(), RightChild.Evaluate()); } public string GetSufixNotation() { if (LeftChild == null) return Value.ToString (); return string.Format("{0} {1} {2}", LeftChild.GetSufixNotation(), RightChild.GetSufixNotation(), Value); } } class Operator { public char Value; public Operator(char value) { Value = value; } public bool IsParenthesis { get { return Value == '('; } } public int ComparePrecedence(Operator o) { if ("+-".Contains(Value)) { if ("+-".Contains(o.Value)) return 0; else return -1; } else if ("+-".Contains(o.Value)) return 1; else return 0; } } |