Question

In: Computer Science

In this homework, we will write the code that will take an expression (input) from the...

In this homework, we will write the code that will take an expression (input) from the user in infix notation and convert that to corresponding postfix notation and evaluate its value.

Task 0: Use the starter code to start. All the needed functions and their functionalities are given in the starter code.

Task 1: Complete the startercodeinfixtopostfix.c file

Task 2: Complete the startercodeevaluatepostfix.c file

Sample Input/Output:

(Infix to postfix)

Input: (A + B)*C-D*E

Output: A B + C * D E * -

Input: ( ( A + B ) * D ) ↑ ( E - F)

Output: A B + D * E F-^

Input: ((A – (B + C)) * D) ↑ (E + F)

Output: A B C + - D * E F + ↑

Input: a + b * c + (d * e + f) * g

Output: a b c * + d e * f + g * +

Input: (4+8)*(6-5)/((3-2)*(2+2))

Output: 4 8 + 6 5 - * 3 2 – 2 2 + * /

Input: 3+4*5/6

Output: 3 4 5 * 6 /+

Input: 3 + (4 * 5 – (6 / 7 ↑ 1 ) * 9 ) * 1

Output: 345*671^/9*-1*+

(Evaluate postfix expression)

Input: 4 8 + 6 5 - * 3 2 – 2 2 + * /

Evaluation result: 3

Input: 3 4 5 * 6 /+

Evaluation result: 6

Input: 345*671^/9*-1*+

Evaluation result: 23

StarterCodeEvaluatePostfix.c:

#define SIZE 50            /* Size of Stack */
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

int stack[SIZE];
int top=-1;       /* Global declarations */
 
/* Function for PUSH operation */
void push(int item)
{

} 

/* Function for POP operation */
int pop()
{
 
}

/* Function for precedence */
int pr(char elem)
{                 

}

//Evaluate postfix expression

void evaluatepostfix (char pofx[])
{

    
}

/* main function begins */
int main()
{
        char postfix[SIZE];        
        printf("ASSUMPTION: The postfix expression contains single letter variables and single digit constants only.\n");
        printf("\nEnter postfix expression : ");
    scanf("%s",postfix);
        evaluatepostfix(postfix);
        return 0;
}

StarterCodeInfixToPostfix.c

#define SIZE 50            /* Size of Stack */
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

char stack[SIZE];
int top=-1;       /* Global declarations */
 
/* Function for PUSH operation */

void push(char item)
{
    
} 

/* Function for POP operation */
char pop()
{

}

 /* Function for precedence */
int pr(char elem)
{                
    
}

 /* Function for infix to postfix conversion */
void InfixToPostfix(char infx[], char pofx[])
{                        
 
}

/* main function begins */
int main()
{
        char infix[SIZE], postfix[SIZE];         /* declare infix string and postfix string */
        printf("ASSUMPTION: The infix expression contains single letter variables and single digit constants only.\n");
        printf("\nEnter Infix expression : ");
    scanf("%s",infix);
        InfixToPostfix(infix,postfix);                   /* call to convert */
        printf("\n\nGiven Infix Expn: %s  Postfix Expn: %s\n",infix,postfix);
        return 0;
}

Solutions

Expert Solution

startercodeinfixtopostfix.c

#define SIZE 50            /* Size of Stack */
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

char stack[SIZE];
int top=-1;       /* Global declarations */

/* Function for PUSH operation */

void push(char item)
{
    stack[++top]=item;
}

/* Function for POP operation */
char pop()
{
   if(top==-1)
       return -1;
   else
       return stack[top--];
}

/* Function for precedence */
int pr(char elem)
{              
     if(elem == '(')
        return 0;
  
    if(elem == '+' || elem == '-')
        return 1;
      
    if(elem == '*' || elem == '/')
        return 2;
      /* we can add priority for even more operators */
}
/* Function for infix to postfix conversion */
void InfixToPostfix(char infx[], char pofx[])
{          
   char x, *s = infx;
  
   int index = 0;
              
    while(*s!='\0')
   {
       if(isalnum(*s))
           pofx[index++]=*s;
  
       else if(*s=='(')
           push(*s);
          
       else if(*s==')')
       {
           while((x=pop())!='(')
               pofx[index++]=x;
       }
       else
       {
           while(pr(stack[top]) >= pr(*s))
               pofx[index++]=pop();
                    push(*s);
       }
       s++;
   }
   while(top != -1)
          pofx[index++]=pop();
   
    pofx[index] = '\0';
}
int main()
{
  
        char infix[SIZE], postfix[SIZE];         /* declare infix string and postfix string */

        printf("\nEnter Infix expression : ");
       scanf("%s",infix);
      
        InfixToPostfix(infix,postfix);
                           /* call to convert */
        printf("\n\nGiven Infix Expn: %s Postfix Expn: %s\n",infix,postfix);
        return 0;
}

startercodeevaluatepostfix.c

#define SIZE 50            /* Size of Stack */
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

int stack[SIZE];
int top=-1;       /* Global declarations */

/* Function for PUSH operation */
void push(int item)
{
   stack[++top] = item;
}

/* Function for POP operation */
int pop()
{
      return stack[top--];
}

/* Function for precedence */
int pr(char elem)
{                           
     if(elem == '(')
        return 0;
  
    if(elem == '+' || elem == '-')
        return 1;
      
    if(elem == '*' || elem == '/')
        return 2;
      /* we can add priority for even more operators */
}

//Evaluate postfix expression

void evaluatepostfix (char pofx[])
{
   int n1,n2,res;
        
         char *e = pofx;
        
        while(*e != '\0')
        {
                if(isdigit(*e))
                        push(*e - 48);
                else
                {
                        n2 = pop();
                        n1 = pop();
                        switch(*e)
                        {
                                case '+':
                                {
                                        res = n2 + n1;
                                 break;
                                }
                                case '-':
                                {
                                        res = n1 - n2;
                                        break;
                                }
                                case '*':
                                {
                                        res = n2 * n1;
                                        break;
                                }
                                case '/':
                                {
                                        res = n1 / n2;
                                        break;
                                }
                        }
                        push(res);
                }
                e++;
        }
       printf("\nThe result of expression :: %d \n",pop());
}

/* main function begins */
int main()
{
        char postfix[SIZE];      
    
        printf("\nEnter postfix expression : ");
    scanf("%s",postfix);
  
        evaluatepostfix(postfix);
        return 0;
}


Related Solutions

Write the code for postfix expression in C++ using a linked stack that can take numbers...
Write the code for postfix expression in C++ using a linked stack that can take numbers bigger than 9 (any size the user gives) and pushes the final result onto the top of the stack
Write code in C please. #1 Write a function multiples() which will take an integer input...
Write code in C please. #1 Write a function multiples() which will take an integer input and it will print out all the multiples of this number starting from 2 but not including itself. For example, multiples(10) will print 2, 5 and multiples(100) will print 2, 4, 5, 10, 20, 25, 50 #2 Write and test a Fibonacci() function that uses a loop instead of recursion to calculate Fibonacci numbers.
Write a program that takes an infix expression as input and produces a postfix expression. The...
Write a program that takes an infix expression as input and produces a postfix expression. The infix and postfix expressions are in the form of vectors of strings. We will test with a main method that takes a file name as input from the command line. We will test your implementation using printPostFix() method.   There are sample input and output files in this folder. You may edit the header file. Example 1 infix expression = apple + banana * cat...
Write code to read a list of song names and durations from input. Input first receives...
Write code to read a list of song names and durations from input. Input first receives a song name, then the duration of that song. Input example: Time 424 Money 383 quit. #include <iostream> #include <string> #include <vector> using namespace std; class Song { public: void SetNameAndDuration(string songName, int songDuration) { name = songName; duration = songDuration; } void PrintSong() const { cout << name << " - " << duration << endl; } string GetName() const { return name;...
Write an asssembly languaage (805l) code for EDSIM5I that takes the input from the # keypad...
Write an asssembly languaage (805l) code for EDSIM5I that takes the input from the # keypad to and output to the liquid crystal display.
Write an algorithm (flowchart OR Pseudocode) for the following problem Take input character from the user...
Write an algorithm (flowchart OR Pseudocode) for the following problem Take input character from the user unless he enters '$'. Thereafter display how may vowels were entered by the user. Also display the number of each vowel ('a', 'e', 'i', 'o' and 'u') separately. For example if the user enters B a b e c o o d i u g o a l $ Then we have the output below: #A=2 #E=1 #I=1 #O=3 #U=2
Write a code in C that will take four words from the user and show those...
Write a code in C that will take four words from the user and show those in ascending order.
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and stores the even integers of num1 in another arraylist named evennum.
in c++ QUESTION 4: Write the code to do the following: -read input from the keyboard...
in c++ QUESTION 4: Write the code to do the following: -read input from the keyboard by displaying the message on the screen to ask and read the following information: * customer ID (string) * customer name (string)                                                        * balance (float) -open output file customer .txt to write -Write to the output file customer.txt the following information:                 Customer ID – customer name – balance For example: 1561175753 - James Smith – 1255.25 -close file QUESTION 5: -create one notepad...
C++ code please: Write a program that first gets a list of integers from input. The...
C++ code please: Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, which indicates how much to multiply the array by. Finally, print out the entire array with each element multiplied by the last input. Assume that the list will always contain less than 20 integers. Ex: If the input is 4 4 8 -4 12...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT