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...
Create in JAVA Suppose you are designing a game called King of the Stacks. The rules...
Create in JAVA Suppose you are designing a game called King of the Stacks. The rules of the game are as follows:  The game is played with two (2) players.  There are three (3) different Stacks in the game.  Each turn, a player pushes a disk on top of exactly one of the three Stacks. Players alternate turns throughout the game. Each disk will include some marker to denote to whom it belongs.  At the end...
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,...
(For Python) Evaluating Postfix Arithmetic Expressions. In this project you are to implement a Postfix Expression...
(For Python) Evaluating Postfix Arithmetic Expressions. In this project you are to implement a Postfix Expression Evaluator as described in section 7-3b of the book. The program should ask the user for a string that contains a Postfix Expression. It should then use the string's split function to create a list with each token in the expression stored as items in the list. Now, using either the stack classes from 7.2 or using the simulated stack functionality available in a...
python problem: ( use a loop to read each character from the string and insert into...
python problem: ( use a loop to read each character from the string and insert into the stack) 1. The function main a5.py continually asks the user for a string, and calls isPalindrome to check whether each string is a palindrome. A palindrome is a word or sequence that reads the same backward as forward, e.g., noon, madam or nurses run. 2. Your function must ignore spaces: when the user enters 'nurses run', the function returns True, to indicate that...
- 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;       ...
Convert this C++ program exactly as you see it into x86 assembly language: // Use the...
Convert this C++ program exactly as you see it into x86 assembly language: // Use the Irvine library for the print function #include <iostream> // The string that needs to be printed char word[] = "Golf\0"; // Pointer to a specific character in the string char * character = word; //NOTE: This main() function is not portable outside of Visual Studio void main() { // Set up a LOOP - See the while loop's conditional expression below int ecx =...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT