Question

In: Computer Science

USING C++ Study the scenario and complete the question(s) that follow: Postfix using Stacks The rules...

USING C++

Study the scenario and complete the question(s) that follow:

Postfix using Stacks

The rules to convert an infix expression into an equivalent postfix expression are as follows:

Suppose infx represents the infix expression and pfx represents the postfix expression. The rules to convert infx into pfx are as follows:

1. Initialize pfx to an empty expression and also initialize the stack.

2. Get the next symbol, sym, from infx.

a. If sym is an operand, append sym to pfx.

b. If sym is (, push sym into the stack.

c. If sym is ), pop and append all of the symbols from the stack until the most recent left parentheses. Pop and discard the left parentheses.

d. If sym is an operator:

i. Pop and append all of the operators from the stack to pfx that are above the most recent left parentheses and have precedence greater than or equal to sym.

ii. Push sym onto the stack.

3. After processing infx, some operators might be left in the stack. Pop and append to pfx everything from the stack. In this program, you will consider the following (binary) arithmetic operators: +, -, *, and /. You may assume that the expressions you will process are error free.

Question:

Write a program that converts an infix expression into an equivalent postfix expression. Design a class that stores the infix and postfix strings. The class must include The following operations:

 getInfix: Stores the infix expression.

 showInfix: Outputs the infix expression.

 showPostfix: Outputs the postfix expression.

Some other operations that you might need are:

 convertToPostfix: Converts the infix expression into a postfix expression. The resulting postfix expression is stored in pfx.

 precedence: Determines the precedence between two operators. If the first operator is of higher or equal precedence than the second operator, it returns the value true; otherwise, it returns the value false. Include the constructors and destructors for automatic initialisation and dynamic memory deallocation.

Test your program on the following expressions:

 A + B - C;

 (A + B ) * C;

 (A + B) * (C - D);

 A + ((B + C) * (E - F) - G) / (H - I);

 A + B * (C + D ) - E / F * G + H;

For each expression, your answer must be in the following form:

Infix Expression : A + B - C ;

Postfix Expression: A B + C –

UISING C++

Solutions

Expert Solution

AND

AND

AND

# include <iostream.h>
 # include   <string.h>
 # include   <stdlib.h>
 # include    <conio.h>

 int top=-1;

 char Stack[100]={NULL};

 void push(constchar);
 constchar pop( );

 void infix_to_postfix(constchar *);


 int main( )
    {
       clrscr( );

       char Infix_expression[100]={NULL};

       cout<<"\n\n Enter the Infix Expression : ";
       cin.getline(Infix_expression,80);

       infix_to_postfix(Infix_expression);

       getch( );
       return 0;
    }


 /*************************************************************************///-----------------------------  push(const char)  ----------------------///*************************************************************************/void push(constchar Symbol)
    {
       if(top==99)
      cout<<"Error : Stack is full."<<endl;

       else
      {
         top++;
         Stack[top]=Symbol;
      }
    }

 /*************************************************************************///--------------------------------  pop( )  -----------------------------///*************************************************************************/constchar pop( )
    {
       char Symbol=NULL;

       if(top==-1)
      cout<<"Error : Stack is empty."<<endl;

       else
      {
         Symbol=Stack[top];
         Stack[top]=NULL;
         top--;
      }

       return Symbol;
    }

 /*************************************************************************///---------------------  infix_to_postfix(const char *)  ----------------///*************************************************************************/void infix_to_postfix(constchar *Infix)
    {
       char Infix_expression[100]={NULL};
       char Postfix_expression[100]={NULL};

       strcpy(Infix_expression,"(");
       strcat(Infix_expression,Infix);
       strcat(Infix_expression,")");

       char Symbol[5]={NULL};
       char Temp[5]={NULL};

       for(int count=0;count<strlen(Infix_expression);count++)
      {
         Symbol[0]=Infix_expression[count];

         if(Symbol[0]=='(')
        push(Symbol[0]);

         elseif(Symbol[0]==')')
        {
           Symbol[0]=pop( );

           while(Symbol[0]!='(')
              {
             strcat(Postfix_expression,Symbol);

             Symbol[0]=pop( );
              }
        }

         elseif(Symbol[0]=='^' || Symbol[0]=='*' || Symbol[0]=='/'
                    || Symbol[0]=='+' || Symbol[0]=='-')
        {
           if(Symbol[0]=='*' || Symbol[0]=='/')
              {
             Temp[0]=pop( );

             while(Temp[0]=='^' || Temp[0]=='*' || Temp[0]=='/')
                {
                   strcat(Postfix_expression,Temp);

                   Temp[0]=pop( );
                }

             push(Temp[0]);
              }

           elseif(Symbol[0]=='+' || Symbol[0]=='-')
              {
             Temp[0]=pop( );

             while(Temp[0]!='(')
                {
                   strcat(Postfix_expression,Temp);

                   Temp[0]=pop( );
                }

             push(Temp[0]);
              }

           push(Symbol[0]);
        }

         else
        strcat(Postfix_expression,Symbol);
      }

       cout<<"\n\n Postfix Expression : "<<Postfix_expression<<endl;
    }

Related Solutions

E-commerce Question Study the scenario and complete the question(s) that follow: Tshepiso Florists is a small...
E-commerce Question Study the scenario and complete the question(s) that follow: Tshepiso Florists is a small flower business located in the Glen Austin neighbourhood. There are two main types of clients that patronise Tshepiso Florists – gardening enthusiasts and people who need to send flowers to their loved ones on special occasions. Tshepiso is starting a gardening class online and she would like to send a notification via email to a particular group of clients. Create a database of at...
E-commerce Question Study the scenario and complete the question(s) that follow: Assume you and your university...
E-commerce Question Study the scenario and complete the question(s) that follow: Assume you and your university friends have a new start-up company called MealsOnWheels. The goal of the start-up is to provide the services of a chef on demand. The business is located in Vorna Valley, Midrand. In addition to mobile chef services, you also provide health and wellness services. After careful deliberation, you have decided that your start-up needs a website to assist in increasing its client base. As...
Skills needed to complete this assignment: linked lists, stacks. Postfix notation, is a mathematical notation in...
Skills needed to complete this assignment: linked lists, stacks. Postfix notation, is a mathematical notation in which operators follow their operands; for instance, to add 3 and 4, one would write 3 4 + rather than 3 + 4 (infix notation). If there are multiple operations, operators are given immediately after their second operands; so, the expression written 3 − 4 + 5 in conventional notation would be written 3 4 − 5 + in postfix notation: 4 is first...
languague c++ Write a program to demonstrate the use of STACKS. The scenario is as follows:...
languague c++ Write a program to demonstrate the use of STACKS. The scenario is as follows: There is a MAZE. There is a mouse inside the maze. The mouse must find the exit from the maze. When the user clicks the letter C or c a CAT is added to the maze. The cat will run through the maze and if it finds the mouse it should eat it and the game is over! User can add as many cats...
Write a C++ class that implement two stacks using a single C++ array. That is, it...
Write a C++ class that implement two stacks using a single C++ array. That is, it should have functions pop_first(), pop_second(), push_first(…), push_second(…), size_first(), size_second(), …. When out of space, double the size of the array (similarly to what vector is doing). Notes: Complete all the functions in exercise_2.cpp, then submit this cpp file. pop_first() and pop_second() should throw std::out_of_range exception when stack is empty. CODE: #include <cstdio> #include <stdexcept> template <class T> class TwoStacks { public:   // Constructor, initialize...
Problem 2: Evaluate the following postfix expression, using the rules given in Section I of Lab...
Problem 2: Evaluate the following postfix expression, using the rules given in Section I of Lab 10: 1 5 4 – 3 + * 3.   Computer Science
(C++ Programming) Practice with Stacks and Queues (PLEASE FOLLOW ALL DIRECTIONS) ·In “stackfib.cpp”, write a non-recursive...
(C++ Programming) Practice with Stacks and Queues (PLEASE FOLLOW ALL DIRECTIONS) ·In “stackfib.cpp”, write a non-recursive function fib() which: ·Takes a single int parameter n ·Returns the nth Fibonacci number. We will start with fib(0) == fib(1) == 1, then fib(n) = fib(n-1) + fib(n-2) ·To compute this without using recursion, we use a stack to store the numbers we need to compute (1)   Initialize your result to be 0 (2)   Push the parameter n onto the stack (3)   While the stack is...
Consider the scenario below, then follow the instructions underneath it to complete the discussion. If appropriate,...
Consider the scenario below, then follow the instructions underneath it to complete the discussion. If appropriate, support your position with credible resources/examples/evidence and provide APA references. Mr. B Mr. B, a 70-year-old male client, presented to his primary care physician with complaints of blurred vision and headaches over the last two months. On several visits, Mr. B's blood pressure was found to be elevated, so the physician started him on hydrochlorothiazide 25 mg by mouth daily. One month later, Mr....
Consider the scenario below, then follow the instructions underneath it to complete the discussion. If appropriate,...
Consider the scenario below, then follow the instructions underneath it to complete the discussion. If appropriate, support your position with credible resources/examples/evidence and provide APA references. Mr. B Mr. B, a 70-year-old male client, presented to his primary care physician with complaints of blurred vision and headaches over the last two months. On several visits, Mr. B's blood pressure was found to be elevated, so the physician started him on hydrochlorothiazide 25 mg by mouth daily. One month later, Mr....
Consider the scenario below, then follow the instructions underneath it to complete the discussion. If appropriate,...
Consider the scenario below, then follow the instructions underneath it to complete the discussion. If appropriate, support your position with credible resources/examples/evidence and provide APA references. Mr. D Mr. D is a 90-year-old man who was admitted to the hospital with complaints of nausea, vomiting, left arm pain, and chest pain. An electrocardiogram (ECG) is performed, and he is diagnosed as having a myocardial infarction. Mr. D has a long history of comorbidities including hypertension, diabetes, and congestive heart failure...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT