Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Convert expressions that are in infix notation to postfix notation. The expressi

ID: 3783496 • Letter: C

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 *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:

The next step the driver scans it in an stores it in a buffer

in driver:

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

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

Explanation / Answer

in c file:
int actualConversion(Element element,Out out,Stack stack){
/***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;
}
   return 0;
}
int remainingStack(Stack stack,Out out)
{
/***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
}
   return 0;
}

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

}
  
   int i;
  
   i=actualConversion(element,out,stack);
   if (i<>0) return i;
   i=remainingStack(stack,out);
if (i<>0) return i;
  

   freeStack(stack); //at this point we are done using the stack   
return 0; //return success
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote