private void Window_Loaded(object sender, RoutedEventArgs e) { try { String path = "Input.txt"; List <String> lines = LineByLineReader.ReadInput(path); foreach (String line in lines) { this.expressions.Add(new ExpressionSimplifier.Expression( line, ExpressionParser.BuildTree(line))); } lbExpressions.ItemsSource = this.expressions; FillTransformationsComboBox(); } catch (Exception ex) { // Workaround // 64bit machines are unable to properly throw the errors during a Window_Loaded event. // http://stackoverflow.com/questions/4807122/wpf-showdialog-swallowing-exceptions-during-window-load BackgroundWorker loaderExceptionWorker = new BackgroundWorker(); loaderExceptionWorker.DoWork += ((exceptionWorkerSender, runWorkerCompletedEventArgs) => { runWorkerCompletedEventArgs.Result = runWorkerCompletedEventArgs.Argument; }); loaderExceptionWorker.RunWorkerCompleted += ((exceptionWorkerSender, runWorkerCompletedEventArgs) => { throw (Exception)runWorkerCompletedEventArgs.Result; }); loaderExceptionWorker.RunWorkerAsync(ex); } }
static void Main(String[] args) { String path = "Input.txt"; List <String> lines = LineByLineReader.ReadInput(path); List <ExpressionNode> trees = new List <ExpressionNode>(); foreach (String line in lines) { trees.Add(ExpressionParser.BuildTree(line)); } foreach (var item in trees) { Console.WriteLine("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); if (item == null) { Console.WriteLine("Invalid parentheses\n"); continue; } Dimension dim; try { dim = item.GetDimension(); Console.WriteLine("dim: [" + dim.M + "," + dim.N + "]\n"); Console.WriteLine("cost: " + item.Cost() + "\n"); } catch (ApplicationException appEx) { Console.WriteLine(appEx.Message + "\n"); } finally { Console.WriteLine(item.ToString()); } } Console.ReadKey(); }
static void Main() { binarySearchTree binaryTree = new binarySearchTree(5); binaryTree.Add(4); binaryTree.Add(24); binaryTree.Add(72); binaryTree.Add(12); binaryTree.Add(-4); binaryTree.Add(8); binaryTree.Add(5); binaryTree.Add(7); binaryTree.Add(10); binaryTree.Add(24); System.Console.WriteLine("In Order Traversal"); Console.WriteLine(binaryTree.traverseInOrder()); Debug.Assert(binaryTree.traverseInOrder() == "-4 4 5 5 7 8 10 12 24 24 72 "); System.Console.WriteLine(); System.Console.WriteLine("Pre Order Traversal"); Console.WriteLine(binaryTree.traversePreOrder()); Debug.Assert(binaryTree.traversePreOrder() == "5 4 -4 5 24 12 8 7 10 24 72 "); System.Console.WriteLine(); System.Console.WriteLine("Post Order Traversal"); Console.WriteLine(binaryTree.traversePostOrder()); Debug.Assert(binaryTree.traversePostOrder() == "-4 5 4 7 10 8 24 12 72 24 5 "); System.Console.WriteLine(); System.Console.WriteLine("binaryTree contains value 7: "); Debug.Assert(binaryTree.Contains(7) == true); System.Console.WriteLine(); System.Console.WriteLine("binaryTree contains value 30: "); Debug.Assert(binaryTree.Contains(30) == false); System.Console.WriteLine(); ExpressionParser expressionParser = new ExpressionParser(); ExpressionTree expressionTree = new ExpressionTree(expressionParser.BuildTree("5 + 2 * 8 - 6 / 4")); System.Console.WriteLine("Expression Parser In Order Traversal"); Console.WriteLine(expressionTree.traverseInOrder()); Debug.Assert(expressionTree.traverseInOrder() == "5 + 2 * 8 - 6 / 4 "); System.Console.WriteLine(); System.Console.WriteLine("Expression Parser Pre Order Traversal"); Console.WriteLine(expressionTree.traversePreOrder()); Debug.Assert(expressionTree.traversePreOrder() == "- + 5 * 2 8 / 6 4 "); System.Console.WriteLine(); System.Console.WriteLine("Expression Parser Post Order Traversal"); Console.WriteLine(expressionTree.traversePostOrder()); Debug.Assert(expressionTree.traversePostOrder() == "5 2 8 * + 6 4 / - "); System.Console.WriteLine(); Console.WriteLine("Expression Tree Evaluated"); Console.WriteLine(expressionTree.EvaluateRecursive()); Debug.Assert(expressionTree.EvaluateRecursive() == (5 + 2 * 8 - 6 / 4)); Console.WriteLine(); /// binaryTree /// 5 /// / \ /// / \ /// 4 24 /// / \ / \ /// -4 5 12 72 /// / \ /// 8 20 /// / \ /// 7 10 }