Question
Download the starter code and implement the infixToPostfix function in infixToPostfix.cpp to convert an infix arithmetic expression to postfix. This function takes 3 parameters, the
Download the starter code and implement theinfixToPostfixfunction ininfixToPostfix.cppto convert an infix arithmetic expression to postfix. This function takes 3 parameters, the first being an array of strings where each string is each part of the expression:
Infix expression: 2 + 3 ->string infix[]: { \"2\", \"+\", \"3\"}
The second parameter is the length of this array, for the array above this will be3.
The third parameter is where the postfix expression will be written in the same format, the function will assume that this array is at least the same size as the array for the first parameter.
The infix expression 2 + 3 will generate the postfix expression 2 3 + represented instring postfix[]: { \"2\", \"3\", \"+\"}
The value returned by the function will be the length of the postfix expression if no error occured, for 2 + 3 -> 2 3 + the return value is3.
An error occurs when the function encounters a mismatched parenthesis, in these cases0should be returned. Examples of where this would happen would be ( 2 * (3 + 5) and ( 2 + 3 ) * 5).
Other invalid expressions do not need to be accounted for (and a nonzero value may be returned) such as multiple consecutive operator symbols (2 + * 3), expressions missing/without operator symbols (2 3), or expressions with unspecified operator symbols (2 ^ 3).
Operator symbols: ( : left parenthesis, all operations between this and \")\" take place first ) : right parenthesis, all op.s back to previous \"(\" take place first * : multiplication, higher precedence - takes place before \"+\" and \"-\" / : division, higher precedence - takes place before \"+\" and \"-\" % : remainder, higher precedence - takes place before \"+\" and \"-\" + : addition, lower precedence - takes place after \"*\" , \"/\" , \"%\" - : subtraction, lower precedence - takes place after \"*\" , \"/\" , \"%\" The function is not specified to work with any other operator symbols. Any string in infix may be assumed to be an integer operand if none of the above symbols.
A main function is provided to test theinfixToPostfixfunction providing the expression2 + 3 * 4 + ( 5 - 6 + 7 ) * 8which should convert to2 3 4 * + 5 6 - 7 + 8 * +. You may use the STL stack class in the function, the header file is already #include'd. SubmitinfixToPostfix.cppwith the implementedinfixToPostfixfunction.
Starter Code:
#include #include
using namespace std;
//Converts an infix arithmetic expression into postfix //The function takes 3 parameters //First parameter: array of strings for infix expression // each string is either an integer number or operator symbol //Second parameter: number of strings in infix expression //Third parameter: array of strings for postfix expression // generated by function, same format as first parameter // assumes that postfix is at least the size of postfix //Return value: int, number of strings in postfix expression // Returns 0 if an error is encountered when converting expression // An error occurs with a mismatched parenthesis, e.g. ( ( ) or ( ) ) etc. //Operator symbols: // ( : left parenthesis, all operations between this and \")\" take place first // ) : right parenthesis, all op.s back to previous \"(\" take place first // * : multiplication, higher precedence - takes place before \"+\" and \"-\" // / : division, higher precedence - takes place before \"+\" and \"-\" // % : remainder, higher precedence - takes place before \"+\" and \"-\" // + : addition, lower precedence - takes place after \"*\" , \"/\" , \"%\" // - : subtraction, lower precedence - takes place after \"*\" , \"/\" , \"%\" //The function is not specified to work with any other operator symbols //Any string in infix may be assumed to be an integer operand if none // of the above symbols int infixToPostfix(string infix[], int length, string postfix[]) { }
//Main function to test infixToPostfix() //Should convert 2 + 3 * 4 + ( 5 - 6 + 7 ) * 8 // to 2 3 4 * + 5 6 - 7 + 8 * + int main() { string infixExp[] = {\"2\", \"+\", \"3\", \"*\", \"4\", \"+\", \"(\", \"5\", \"-\", \"6\", \"+\", \"7\", \")\", \"*\", \"8\"}; string postfixExp[15]; int postfixLength;
cout for (int i=0; i15;> { cout } cout cout
postfixLength = infixToPostfix(infixExp, 15, postfixExp);
cout for (int i=0; i;> { cout } cout cout return 0; }
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