473,320 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

C++ Primer ex 9.34, using a Stack

All I was able to come up with is this code. I am get confused on how to
solve this problem provided a very small amount of Stack operations.
/* C++ Primer - 4/e
*
* Exercise 9.42
* STATEMENT
* Use a stack to process parenthesized expressions. When you see an
open parenthesis, note that it was seen. When you see a close
parenthesis after an open parenthesis, pop elements down to and
including the open parenthesis off the stack. push a value onto the
stack to indicate that a parenthesized expression was replaced.
*
*/
#include <iostream>
#include <stack>
#include <string>
int main()
{
std::stack<charstrStack;
bool P_ON = false;
char in_char;
while( std::cin >in_char )
{
if( in_char == '(' )
{
P_ON = true;
}

if ( in_char == ')')
{
P_ON = false;
}

if( P_ON == false )
{
strStack.push( in_char );
}
}

/* print the stack with desired elements removed*/
std::cout << "\n------------ STACK after elements removed
---------------\n";
while( strStack.empty() == false )
{
std::cout << strStack.top();
strStack.pop();
}

std::cout << std::endl;
return 0;
}

========= OUTPUT ===============
~/programming/C++ $ g++ -ansi -pedantic -Wall -Wextra ex_09.43.cpp
~/programming/C++ $ ./a.out
comp.(lang).c++

------------ STACK after elements removed ---------------
++c.).pmoc
~/programming/C++ $
Oct 19 '07 #1
1 2548
On 2007-10-19 08:35, cront wrote:
All I was able to come up with is this code. I am get confused on how to
solve this problem provided a very small amount of Stack operations.
/* C++ Primer - 4/e
*
* Exercise 9.42
* STATEMENT
* Use a stack to process parenthesized expressions. When you see an
open parenthesis, note that it was seen. When you see a close
parenthesis after an open parenthesis, pop elements down to and
including the open parenthesis off the stack. push a value onto the
stack to indicate that a parenthesized expression was replaced.
The easiest way to note that an opening parenthesis was seen is to push
is onto the stack. Then when you read in a closing parenthesis you start
poping until you find an opening one.

The below code will replace all parenthesised expressions with 5 (even
nested parentheses (I wrote it really quick, so it might contain bugs).

#include <iostream>
#include <stack>

int main()
{
std::stack<charstack;

char c;
while ( std::cin >c )
{
if ( c != ')' )
stack.push(c);

else
{
char p;
do {
p = stack.top();
stack.pop();
} while ( p != '(' );

stack.push('5');
}
}

while ( !stack.empty() )
{
std::cout << stack.top() << "\n";
stack.pop();
}
}

--
Erik Wikström
Oct 19 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: charlie_M | last post by:
Is there a decent primer on the web or in print that shows the specifics of using javascript in web forms?? I don't need to learn javascript but I need to know some specifics about using it in...
7
by: Lycan. Mao.. | last post by:
Hello, I am a newbie in C++ and I'm in trouble in choosing books, I hope some one who can give me some tips. I'm already know C and a little about Scheme, C#, Python, Lua and so on, and now I want...
2
by: W. Watson | last post by:
Is there a primer out there on these two items? I have the Python tutorial, but would like either a Tkinter tutorial/primer to supplement it, or a primer/tutorial that addresses both. Maybe there's...
8
by: arnuld | last post by:
i have solved it. any suggestions for improving the code: /* C++ Primer - 4/e * chapter 3 * exercise 3.14 * STATEMENT read some text into a vector,storing each word as an elelment in the...
5
by: arnuld | last post by:
it does not run and even does not even give me any clue to the problem in its output :( /* C++ Primer - 4/e * chapter 5 - Expressions * exercise 5.18 * STATEMENT * write a programme...
2
by: arnuld | last post by:
it compiles and runs and at one place it produces strange output becaus eof a semantic-bug. i can't find it: /* C++ Primer - 4/e * * exercise 7.12 * STATEMENT: * write a programme to...
20
by: arnuld | last post by:
I get an error, can't find what is the problem: /* C++ Primer - 4/e * * Chapter 8, exercise 8.3 * STATEMENT * write a function that takes and returns an istream&. the function should read...
2
by: xianwei | last post by:
First, typedef struct pair { Node *parent; Node *child; } Pair; static Pair SeekItem(cosnt Item *pI, const Tree *pTree) { Pair look;
1
by: Kveldulv | last post by:
Hi all, here is the code: http://pastebin.com/m6e74d36b I'm stuck at highfink constructor, last line before #endif. As parameters, I have reference to one parent class and int member of another...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.