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

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...
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....
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,
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 {...
C++ program homework question 1 1. Create and implement a class called clockType with the following...
C++ program homework question 1 1. Create and implement a class called clockType with the following data and methods (60 Points.): Data: Hours, minutes, seconds Methods: Set and get hours Set and get minutes Set and get seconds printTime(…) to display time in the form of hh:mm:ss default and overloading constructor Overloading Operators: << (extraction) operator to display time in the form of hh:mm:ss >> (insertion) operator to get input for hours, minutes, and seconds operator+=(int x) (increment operator) to...
Design, implement, and fully test a Python3 function that converts a number to words (words_from_number(number: int))....
Design, implement, and fully test a Python3 function that converts a number to words (words_from_number(number: int)). It should expect to be passed a natural number (such as 12345) and return the corresponding words (i.e., “twelve thousand three hundred forty-five”). Since Python integers can grow arbitrarily large, your code should be flexible, handling at least 20 digits (realistically, it’s just as easy up to 30 digits). Spell everything correctly, and use correct punctuation (hyphens for forty-five and thirty-seven, no commas or...
Design, implement, and fully test a Python3 function that converts a number to words (words_from_number(number: int))....
Design, implement, and fully test a Python3 function that converts a number to words (words_from_number(number: int)). It should expect to be passed a natural number (such as 12345) and return the corresponding words (i.e., “twelve thousand three hundred forty-five”). Since Python integers can grow arbitrarily large, your code should be flexible, handling at least 20 digits (realistically, it’s just as easy up to 30 digits). Spell everything correctly, and use correct punctuation (hyphens for forty-five and thirty-seven, no commas or...
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes...
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes Savings and Checking within their respective .java files. (modify display() as needed ) 1. Add New private String name (customer name) for both, add a New int taxID for Savings only. 2. Add equals() METHOD TO CHECK any 2 accounts Demonstrate with AccountDemo.java in which you do the following: 3. Create 1 Savings Account and 3 Checking Accounts, where 2 checkings are the same....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT