Question: Convert expressions that are in infix notation to postfix notation. The expressions might contain parentheses and these operators: =, ONLY, NEVER, AND, OR. int convertToPostfix(char
Convert expressions that are in infix notation to postfix notation. The expressions might contain parentheses and these operators: =, ONLY, NEVER, AND, OR.
int convertToPostfix(char *pszInfix, Out out)
It returns 0 if it converted successfully. Otherwise, it returns a value which indicates an error in the infix expression (e.g., missing left paren, missing right paren).
It populates the out array using the addPostfixOut function (provided in the driver).
For modularity, you will need to divide convertToPostfix into multiple functions.
The program reads in an input file:
RECIT = N RECIT = Y PROF = CLARK PROF NEVER CLARK PROF ONLY CLARK PROF = CLARK AND RECIT = N PROF NEVER CLARK AND DEPT = CS RECIT = Y AND ( PROF = CLARK OR PROF = GIBSON ) RECIT = Y AND ( PROF = CLARK AND PROF = GIBSON ) LANG ONLY C ( LANG ONLY C OR LANG ONLY JAVA ) AND PREREQ NEVER CS2123 DEPT = CS AND RECIT = N AND LANG = JAVA DEPT = CS AND ( RECIT = Y OR LANG = PYTHON ) AND PROF = CLARK DEPT = CS AND RECIT = Y OR LANG = PYTHON AND PROF = CLARK ( ( ( LANG NEVER JAVA ) ) ) ( DEPT = XX PROF = SMITH ) ( ( DEPT = XX ) ) ) (
The next step the driver scans it in an stores it in a buffer
in driver:
rc = convertToPostfix(szInputBuffer, postfixOut);
At this point it calls convertToPostFix to convert it to
| RECIT N = |
| RECIT Y = |
| PROF CLARK = |
| PROF CLARK NEVER |
| PROF CLARK ONLY |
| PROF CLARK NEVER DEPT CS = AND |
| RECIT Y = PROF CLARK = PROF GIBSON = OR AND |
in c file:
convertToPostFix(char *pszInfix, Out out) {
Stack stack = newStack(); Token szToken; //token string to store in Element variables while ((pszInfix = getToken(pszInfix, szToken, MAX_TOKEN + 1)) != 0) //while there are tokens in the line of input { /***Element set up***/ Element element; strcpy(element.szToken, szToken); //store the token in the element categorize(&element); //initialize the Element variable's values based on the token string entered } /***Actual conversion***/ switch (element.iCategory) //what kind of element is it? { case CAT_OPERAND: addOut(out, element); break;
case CAT_OPERATOR: while(isEmpty(stack) == FALSE && element.iPrecedence < topElement(stack).iPrecedence) //loop until the stack is empty or there's no more operator in the stack with a lower precedence than the top of the stack { addOut(out, pop(stack)); //POP and OUT the higher precedence operator from the stack } push(stack, element); break;
case CAT_LPAREN: push(stack, element); break;
case CAT_RPAREN: while (isEmpty(stack) == FALSE && topElement(stack).iCategory != CAT_LPAREN) { addOut(out, pop(stack)); //POP and OUT content of the stack until left parenthesis is found } if (isEmpty(stack) == TRUE) //if we didn't find a ( { freeStack(stack); //at this point we are done using the stack return WARN_MISSING_LPAREN; //return error } pop(stack); //POP and get rid of ( break; } } /***Remaining Stack***/ while (isEmpty(stack) == FALSE) //goes through the remaining stack until empty { if (topElement(stack).iCategory == CAT_LPAREN)//if an unmatched ( is found { freeStack(stack); //at this point we are done using the stack return WARN_MISSING_RPAREN; //return error } addOut(out, pop(stack)); //POP and OUT the stack } freeStack(stack); //at this point we are done using the stack return 0; //return success }
The program works fine just want to seperate it into seperate functions
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
