int main(int argc, char* argv[]) { /** Hint 1: handle arguments * * We only treat the first argument */ /* cancel if no expression is given */ if (argc <= 1) { #ifdef VERBOSE printf("No expression given. Exiting ...\n"); #endif return 1; } /* get the expression */ char* p_expression = argv[1]; if (Helpers::StringLen(p_expression) == 0) { #ifdef VERBOSE printf("No expression given. Exiting ...\n"); #endif return 1; } /** Tree handling */ ExpTree tree; tree.InsertExp(p_expression); /** Tree evaluation */ /* allocate string, remember the terminator */ char* p_result = new char[Helpers::StringLen(p_expression) + 1]; tree.EvaluateTree(); tree.GetInfixExp(p_result); printf("Infix Expression: %s\n", p_result); delete[] p_result; return 0; }
int main(void) { string infix; // Input string int solution; // Solution to the expression tree bool control; // Loop flag to allow multiple iterations of input char loop; // User input for control loop ExpTree tree = ExpTree(NULL); // Declaring an instance of ExpTree cout << "Welcome to the Improved Infix Equation Solver" << endl; control = true; // Continue to loop the program until the user presses the 'n' key while (control) { infix = getInfix(); tree.parseExpression(infix, NULL); // Parse the infix expression and assemble it on the tree solution = tree.evaluate(tree.getRoot()); // Evaluate the results of the infix expression // Testing for proper arrangement of tree nodes and traversal cout << endl << "Pre Order: "; tree.preOrder(tree.getRoot()); cout << endl << "In Order: "; tree.inOrder(tree.getRoot()); cout << endl << "Post Order: "; tree.postOrder(tree.getRoot()); cout << endl; cout << endl << "The solution to the expression is: " << solution << endl; cout << "Do you want to enter another expression (y/n)? "; cin >> loop; if (loop == 'n') control = false; } system("PAUSE"); return 0; }
int main(int argc, const char * argv[]) { ExpTree a; a.build(); cout << "Postorder: "; a.print_postorder(a.root); cout << endl << "Inorder: "; a.print_inorder(a.root); cout << endl << "Preorder: "; a.print_preorder(a.root); cout << endl; a.evaluate(a.root); return 0; }