Question

In: Computer Science

Question : Design and implement two classes called InfixToPostfix and PostFixCalculator. The InfixToPrefix class converts an...

Question : Design and implement two classes called InfixToPostfix and PostFixCalculator. The InfixToPrefix class converts an infix expression to a postfix expression. The PostFixCalculator class evaluates a postfix expression. This means that the expressions will have already been converted into correct postfix form. Write a main method that prompts the user to enter an expression in the infix form, converts it into postfix, displays the postfix expression as well as it's evaluation. For simplicity, use only these operators, + , - , * , / and %.

I need help answering the programming question, not sure how this really functions. If you can please explain how it works with comments. Thank you I greatly appreciate the help.

Solutions

Expert Solution

//all important header files required
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#define BLANK ' '
#define TAB '\t'
#define MAX 50
char infix[MAX], postfix[MAX];
long int stack[MAX];
int top;
void infix_to_postfix() 

{

        unsigned int i,p=0;

        char next;

        char symbol;

        for(i=0;i<strlen(infix);i++)

        {

                symbol=infix[i];

                if(!white_space(symbol))

                {

                        switch(symbol)  //if it is a ) we will push it into the stack

                        {

                        case '(':

                                push(symbol);

                                break;

                        case ')':  //this will tell when to pop the started push we did 

                                while((next=pop())!='(')

                                        postfix[p++] = next;

                                break;

                        case '+': //all the arithmetic operations will be transfered to priorty so that it is evaluated accordingly

                        case '-':

                        case '*':

                        case '/':

                        case '%':

                        case '^':

                                while( !isEmpty( ) &&  priority(stack[top])>= priority(symbol) )

                                        postfix[p++]=pop();

                                push(symbol);

                                break;

                        default: 

                             postfix[p++]=symbol;

                        }

                }

        }

        while(!isEmpty( ))

                postfix[p++]=pop();

        postfix[p]='\0'; /*End postfix with'\0' to make it a string*/

}



/*This function returns the priority of the operator*/

int priority(char symbol)

{

        switch(symbol) // simple logic to set priority of operators

        {

        case '(':

                return 0;

        case '+':

        case '-':

                return 1;

        case '*':

        case '/':

        case '%':

                return 2;

        case '^':

                return 3;

        default :

                return 0;

        }

}



void push(long int symbol)  //entering element into stack

{

        if(top>MAX)  //base case

        {

                printf("Stack overflow\n");

                exit(1);

        }

        stack[++top]=symbol;

}



long int pop()  // delete element from stack

{
 
        if( isEmpty() )  //base case 

        {

                printf("Stack underflow\n");

                exit(1);

        }

        return (stack[top--]);

}

int isEmpty()  // is the stack empty 

{

        if(top==-1)

                return 1;

        else

                return 0;

}



int white_space(char symbol)  //check occurance of white_spaces

{

        if( symbol == BLANK || symbol == TAB )

                return 1;

        else

                return 0;

}



long int eval_post()

{

        long int a,b,temp,result;

        unsigned int i;



        for(i=0;i<strlen(postfix);i++)

        {

                if(postfix[i]<='9' && postfix[i]>='0')

                        push(postfix[i]-'0');

                else

                {

                        a=pop();

                        b=pop();

                        switch(postfix[i])

                        {

                        case '+':

                                temp=b+a; 
                                                                        break;

                        case '-':

                                temp=b-a;
                                                                        break;

                        case '*':

                                temp=b*a;
                                                                        break;

                        case '/':

                                temp=b/a;
                                                                        break;

                        case '%':

                                temp=b%a;
                                                                        break;

                        case '^':

                                temp=pow(b,a);
                                                                        break;

                        }

                        push(temp);

                }

        }

        result=pop();

        return result;

}
//main driver functions 
void main()

{

        long int value;

        top=-1;

        printf("Enter infix : ");

        gets(infix);

        infix_to_postfix();

        printf("Postfix : %s\n",postfix);

        value=eval_post();

        printf("Value of expression : %ld\n",value);


}

Related Solutions

JAVA Specify, design, and implement a class called PayCalculator. The class should have at least the...
JAVA Specify, design, and implement a class called PayCalculator. The class should have at least the following instance variables: employee’s name reportID: this should be unique. The first reportID must have a value of 1000 and for each new reportID you should increment by 10. hourly wage Include a suitable collection of constructors, mutator methods, accessor methods, and toString method. Also, add methods to perform the following tasks: Compute yearly salary - both the gross pay and net pay Increase...
Design and implement a class called circle_location to keep track of the position of a single...
Design and implement a class called circle_location to keep track of the position of a single point that travels around a circle. An object of this class records the position of the point as an angle, measured in a clockwise direction from the top of the circle. Include these public member functions: • A default constructor to place the point at the top of the circle. • Another constructor to place the point at a specified position. • A function...
1) Design an implement a program that created and exception class called StringTooLongException, designed to be...
1) Design an implement a program that created and exception class called StringTooLongException, designed to be thrown when a string is discovered that has too many characters in it. Create a driver that reads in strings from the user until the user enters DONE. If a string that has more then 5 characters is entered, throw the exception. Allow the thrown exception to terminate the program 2) Create a class called TestScore that has a constructor that accepts an array...
JAVA - Design and implement a class called Flight that represents an airline flight. It should...
JAVA - Design and implement a class called Flight that represents an airline flight. It should contain instance data that represent the airline name, the flight number, and the flight’s origin and destination cities. Define the Flight constructor to accept and initialize all instance data. Include getter and setter methods for all instance data. Include a toString method that returns a one-line description of the flight. Create a driver class called FlightTest, whose main method instantiates and updates several Flight...
Write in drjava Problem Design and implement these 4 files: A parent class called Plant with...
Write in drjava Problem Design and implement these 4 files: A parent class called Plant with name (eg Rose, Douglas Fir) and lifespan (could be in days, weeks, months or years) attributes Tree inherits from Plant and adds a height attribute Flower inherits from Plant and adds a color attribute A driver file to test the 3 classes above. The classes described in #1, 2 and 3 above should have the usual constructors (default and parameterized), get (accessor) and set...
Please use C++ to complete this question follow the requirement. Question: Implement a class called DoublyLinkedList....
Please use C++ to complete this question follow the requirement. Question: Implement a class called DoublyLinkedList. In the main function, instantiate the DoublyLinkedList class and make sure that there is a user loop and a menu so that the user can access all the list operators. You should implement the following operators, and any others that you may deem best. DestroyList InitializeList GetFirst InsertFirst, InsertLast, Insert DeleteFirst, DeleteLast, Delete IsEmpty Length Print, ReversePrint
Design and implement a C++ class called Module that handles information regarding your assignments for a specific module.
Design and implement a C++ class called Module that handles information regarding your assignments for a specific module. Think of all the things you would want to do with such a class and write corresponding member functions for your Module class. Your class declaration should be well-documented so that users will know how to use it.Write a main program that does the following: Declare an array of all your modules. The elements of the array must be of type Module....
Specifications: This project will have two data classes and a tester class. Design: Create a solution...
Specifications: This project will have two data classes and a tester class. Design: Create a solution for this programming task. You will need to have the following parts: Text file to store data between runs. Two classes that implement the given interfaces. You may add methods beyond those in the interfaces A tester class that tests all of the methods of the data classes. Here are the interfaces for your data classes: package project1; import java.util.ArrayList; public interface Person {...
Specifications: This project will have two data classes and a tester class. Design: Create a solution...
Specifications: This project will have two data classes and a tester class. Design: Create a solution for this programming task. You will need to have the following parts: Text file to store data between runs. Two classes that implement the given interfaces. You may add methods beyond those in the interfaces A tester class that tests all of the methods of the data classes. Here are the interfaces for your data classes: package project1; import java.util.ArrayList; public interface Person {...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix expression,
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT