Question

Use the following algorithm: (1) Create three stacks of characters: stk1, stk2, stk3. (2) Read the...

Use the following algorithm:

(1) Create three stacks of characters: stk1, stk2, stk3.

(2) Read the input infix expression one character at a time and push every character read ( other than ')' ) onto stk1. Do not push the character read if it is ')'.

(3) As long as stk1 is not empty, do the following:

           Fetch the top character ( call it c ) of stk1 and pop stk1.

           if c is an alphabetic character (a-z), push it onto stk3. (You can use the function islower(c) to test if character is one of a-z.)

           if c is an operator, push it onto stk2.

           if c is '(' then fetch the top character of stk2, push it onto stk3, and pop stk2.

(4) Print stk3 top to bottom by repeatedly fetching the top of stk3, printing it, and popping stk3, until stk3 is empty.

The program uses the class template stack from the standard C++ library.

Please complete the following program. ( You do not have to write a whole program, just need to fill in the blanks thanks!)

----------------------------------

#include <iostream>

#include <stack>

#include<cctype>

using namespace std;

int main ()

{

stack<char> stk1, stk2, stk3; //The three stacks used to implemnt the algorithm

char c;

cout <<"Enter infix expression. To finnish hit Enter and then enter CTL-D";

while (cin >> c)

{

//FOR YOU TO DO: PUSH CHARECTER READ ( EXCULDING ')') ON TO FIRST STACK

}

while (! stk1.empty())

{

char c;

//For you to do: fetch top of first stack into variable c, and pop the first stack

//For you to do:check if c is lowercase alphabetic (a-z) or one of +,-,*,/, or open parethesis (, and perform appropiate //action according to algorithm:

}

// For you to do print the first stack from the bottom.

return 0;

}

Homework Answers

Answer #1

Hi, Please find my implementation.

Please let me know in cae of any issue.

#include <iostream>
#include <stack>
#include <cctype>
using namespace std;
int main ()
{
   stack<char> stk1, stk2, stk3; //The three stacks used to implemnt the algorithm
   char c;
   cout <<"Enter infix expression. To finnish hit Enter and then enter # at end to stop"<<endl;
   while (cin >> c)
   {
       // stopping criteria
       if(c == '#')
           break;

   //FOR YOU TO DO: PUSH CHARECTER READ ( EXCULDING ')') ON TO FIRST STACK
       if( c != ')')
           stk1.push(c);
   }
   while (!stk1.empty())
   {
   //For you to do: fetch top of first stack into variable c, and pop the first stack
      c = stk1.top();
      stk1.pop();
     
   //For you to do:check if c is lowercase alphabetic (a-z) or one of +,-,*,/, or open parethesis (, and perform appropiate
   //action according to algorithm:
      if(islower(c))
          stk3.push(c);
      else if(c =='+' || c=='-' || c=='*' || c=='/')
          stk2.push(c);
      else if(c == '('){
          c = stk2.top();
          stk2.pop();
          stk3.push(c);
      }
     
   }
   // For you to do print the first stack from the bottom.
   while (! stk3.empty())
   {
       c = stk3.top();
       stk3.pop();
       cout<<c;
   }

   cout<<endl;
     
   return 0;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
in Java In this exercise, you'll write a Java version of the infix-to-postfix conversion algorithm. These...
in Java In this exercise, you'll write a Java version of the infix-to-postfix conversion algorithm. These same mechanisms can be used as a part of writing a simple compiler. Write class InfixToPostfixConverter co convert an ordinary infix arithmetic expression (assume a valid expression is entered) with single-digit integers (to make things easier) such as (6 + 2) • 5 - 8 / 4 to a postfix expression. The postfix version (no parentheses are needed) of this infix expression is 6...
Complete the following C program to solve the parenthesis matching problem using a stack. We studied...
Complete the following C program to solve the parenthesis matching problem using a stack. We studied the problem and the algorithm in class. Given a sequence of chars (symbols), check if each “(”, “{”, or “[” is paired with a matching “)”, “}”, or “[”. For example, correct: ( )(( )){([( )])}     correct: (( )(( ))){([( )])}   incorrect: )(( )){([( )])}     incorrect: ({[ ])}     incorrect: (   #include <stdio.h> #include <stdlib.h> #define size 100 void push(char *s, int* top, char element);...
Something is either messed up in my operator overload <<, covertopostfix function, or my main output....
Something is either messed up in my operator overload <<, covertopostfix function, or my main output. Cannot figure it out. please help. Please comment your changes too. Program below is supposed to be outputting like this: InFix is:   A+B-C Post fix is:   A B + C - InFix is:   A+C Post fix is:   A C + InFix is:   x*(y+z)-(w+t) Post fix is:   x y z + * w t + - InFix is:   A+B*(C+D)-E/F+G+H Post fix is:   A B C...
I'm having a warning in my visual studio 2019, Can anyone please solve this warning. Severity  ...
I'm having a warning in my visual studio 2019, Can anyone please solve this warning. Severity   Code   Description   Project   File   Line   Suppression State Warning   C6385   Reading invalid data from 'DynamicStack': the readable size is '(unsigned int)*28+4' bytes, but '56' bytes may be read.   Here is the C++ code were I'm having the warning. // Sstack.cpp #include "SStack.h" // Constructor SStack::SStack(int cap) : Capacity(cap), used(0) {    DynamicStack = new string[Capacity]; } // Copy Constructor SStack::SStack(const SStack& s) : Capacity(s.Capacity), used(s.used)...
Java Please [(1)] A palindrome is a string that reads the same forwards as backwards. Using...
Java Please [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q be a non-empty queue,...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h"...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h" template StackLinked::StackLinked (int maxNumber) { } template StackLinked::StackLinked(const StackLinked& other) { } template StackLinked& StackLinked::operator=(const StackLinked& other) { } template StackLinked::~StackLinked() {    clear(); } template void StackLinked::push(const DataType& newDataItem) throw (logic_error) {    } template DataType StackLinked::pop() throw (logic_error) { } template void StackLinked::clear() {    StackNode* t;    while ( top != NULL)    {        t = top;       ...
I've posted this question like 3 times now and I can't seem to find someone that...
I've posted this question like 3 times now and I can't seem to find someone that is able to answer it. Please can someone help me code this? Thank you!! Programming Project #4 – Programmer Jones and the Temple of Gloom Part 1 The stack data structure plays a pivotal role in the design of computer games. Any algorithm that requires the user to retrace their steps is a perfect candidate for using a stack. In this simple game you...
c++ C++ CLASSES and objects DO ADD COMMENTS DISPLAY OUTPUT First make three files: episode.cpp, episode.h...
c++ C++ CLASSES and objects DO ADD COMMENTS DISPLAY OUTPUT First make three files: episode.cpp, episode.h andRun.cpp to separate class header and implementation. In this program, we are going to create a small scale Telivision Management System. A) Create a class Episode with following variables: char* episode_name, char* episode_venue, char episode_date[22] and char episode_time[18]. Input format for episode_date: dd-mm-yyyy Input format for episode_time: hh:mm am/pm B) Implement default constructor and overloaded constructor. Print “Default Constructor Called” and “Overloaded Constructor Called”...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT