Question
There are some pseudo code I need to change these pseudo code into C++ codes I have all the header files , just change the
There are some pseudo code I need to change these pseudo code into C++ codes I have all the header files , just change the pseudo code into c++ codes as per instruction given // ShuntBETTemplateSol.cpp : Defines the entry point for the console application. // ShuntingYardPostFixBETSol.cpp : Defines the entry point for the console application. // Construction of an expression tree uses a stack to process the tree nodes // // To construct an expression tree: // // Loop through the postfix expression for every token // If the token is an operand // Put the token into a node and push the operand token node onto the stack // Else // token is an operator // Place/Pop 2 token nodes from stack, make these tokens the children for the current token node // // When loop is done processing tokens, // The stack node will be the root // Code that you need to develop is marked in areas with xxx #include "stdafx.h" #include #include #include #include #include #include #include using namespace std; string inputFileNameStr = "expressions.txt"; // Defaults in solution folder class OperatorMapClass { private: typedef map OperatorPrecedenceMapType; OperatorPrecedenceMapType operatorMapObj; public: OperatorMapClass() { operatorMapObj.insert(OperatorPrecedenceMapType::value_type('+', 1)); operatorMapObj.insert(OperatorPrecedenceMapType::value_type('-', 2)); operatorMapObj.insert(OperatorPrecedenceMapType::value_type('*', 3)); operatorMapObj.insert(OperatorPrecedenceMapType::value_type('/', 4)); }//OperatorMapClass () bool isStackOperatorHigherPrecedence(char operatorChar, char operatorStackChar) { return((operatorMapObj.count(operatorStackChar)) && (operatorMapObj.at(operatorStackChar) >= operatorMapObj.at(operatorChar))); }//isStackOperatorHigherPrecedence() bool isOperator(char token) { if (operatorMapObj.count(token)) return (true); else return(false); }//isOperator() };//OperatorMapClass OperatorMapClass operatorMapObj; class ShuntingYardClass { public: string createPostFixFrom(string infixString) { string outputString; stack operatorStackObj; for (char token : infixString) { switch (token) { case '/': case '*': case '+': case '-': while ( (!operatorStackObj.empty()) && (operatorStackObj.top() != '(') && (operatorMapObj.isStackOperatorHigherPrecedence(token, operatorStackObj.top())) ) { outputString.push_back(operatorStackObj.top()); operatorStackObj.pop(); }//while operatorStackObj.push(token); break; case '(': // left parenthesis operatorStackObj.push(token); break; case ')': // right parenthesis while ((operatorStackObj.top()) != '(') { outputString.push_back(operatorStackObj.top()); operatorStackObj.pop(); } operatorStackObj.pop(); break; default: //operand outputString.push_back(token); break; }//switch }//for while (!operatorStackObj.empty()) { // if any remainng operators on the stack, pop all push back to the output outputString.push_back(operatorStackObj.top()); operatorStackObj.pop(); } return(outputString); }//postfix() };//ShuntingYardClass class TreeNodeClass { public: TreeNodeClass *left; char value; TreeNodeClass *right; };//TreeNodeClass TreeNodeClass* BuildNewNodeObjPtrMethod(char value) { TreeNodeClass *newNodePtr = new TreeNodeClass; // xxx set the new node pointer value to the char parameter // xxx set newNodePtr left and right branch pointers to null return newNodePtr; }; TreeNodeClass * ConstructBET(string postFixStr) { stack parseStack; TreeNodeClass *newNodePtr; OperatorMapClass OperatorMapObj; // Process each character of the post-fix expression for (char token : postFixStr) { // xxx Form a new node pointer by submitting a token to BuildNewNodeObjPtrMethod and assigning return to new node pointer if (OperatorMapObj.isOperator(token)) { // xxx // xxx Operator, Put/Remove top 2 from parse stack nodes into a new node pointer sub tree as right, left children // xxx Put this new node pointer sub tree node to the parseStack } else { // operand, put new node pointer onto parse stack parseStack.push(newNodePtr); } } // Place remaininng sub tree root node on stack into tree newNodePtr = parseStack.top(); parseStack.pop(); return newNodePtr; } string buildString; void preorder(TreeNodeClass *treeNode) { // xxx insert code here // if not null transverse: // buildString output using current tree node value // do recursive left // do recursive right } bool areParensRequired(TreeNodeClass *treeNode, char value) { OperatorMapClass operatorMapObj; if (operatorMapObj.isOperator(value) && operatorMapObj.isOperator(treeNode->value) && operatorMapObj.isStackOperatorHigherPrecedence(treeNode->value, value)) { buildString += '('; return true; } return false; } void inorder(TreeNodeClass *treeNode) { bool parensRequired = false; // if not null then transverse if (treeNode) { // xxx // check if parentheses are required, save state // recurse transverse left // if parentheses are required append closed ) to the build string buildString += treeNode->value; parensRequired = areParensRequired(treeNode->right, treeNode->value); // xxx //recurse transverse right // if parentheses are required append closed ) to the build string }//if } void postorder(TreeNodeClass *treeNode) { // xxx insert code here // if not null transverse: // do recursive left // do recursive right // buildString output using current tree node value } int main() { ifstream inputFileStreamObj; inputFileStreamObj.open(inputFileNameStr, ios::in); if (inputFileStreamObj.fail()) { cout << "File could not be opened !" << endl; return (EXIT_FAILURE); }//if string infixExpressionStr, postfixExpressionStr; const bool EOF_ENCOUNTERED = false, MORE_DATA_TO_READ = true; ShuntingYardClass shuntingYardObj; system("cls"); while (getline(inputFileStreamObj, infixExpressionStr) ? MORE_DATA_TO_READ : EOF_ENCOUNTERED) { cout << "InFix Expression : " << infixExpressionStr << endl; postfixExpressionStr = shuntingYardObj.createPostFixFrom(infixExpressionStr); cout << "PostFix Expression : " << postfixExpressionStr << endl << endl; TreeNodeClass * expressionTreeRootPtr = ConstructBET(postfixExpressionStr); //xxx initialize build string, start a preorder transversal cout << "Tree pre-order expression is " << endl << buildString << endl << endl; //xxx initialize build string, start an inorder transversal cout << "Tree in-order expression is " << endl << buildString << endl << endl; ////xxx initialize build string, start an inorder transversal cout << "Tree post-order expression is " << endl << buildString << endl << endl; cout << endl << endl; };//while inputFileStreamObj.close(); system("pause"); return EXIT_SUCCESS; }//main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started