User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,135 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,154 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 2001 | Replies: 2
Reply
Join Date: Mar 2007
Posts: 2
Reputation: daphnie is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
daphnie daphnie is offline Offline
Newbie Poster

Help ASK about C++ stack

  #1  
Mar 1st, 2007
Please help me!!!
How can I do the below ex. , please help me!!!

Description:
In this assignment you will implement the ADT Stack. You
will be using stacks in order to count the pairs of three types
of brackets: {}, [], and (). The input will consist of an input
stream of characters, text and numerics. The input will have
embedded brackets occuring in pairs. For example:
Main(int argc, char** argv) { int A[100]; double B[50]; }
Your program should run with this input stream, for example:
$ ../bin/main < ../dat/input.dat
The output should be:
$ Number of () pairs = 1
$ Number of [] pairs = 2
$ Number of {} pairs = 1



Example of an input stream:
(((123)[1]{1(2345)}))
The following should result in an
error:
((…{...1234 })}}
Any crossing pair match should
also trigger an error.


Thank you all of you!!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2007
Posts: 2
Reputation: daphnie is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
daphnie daphnie is offline Offline
Newbie Poster

Re: ASK about C++ stack

  #2  
Mar 1st, 2007
I have just write out somrrthing like this

int main() {
stack<char> allwords;
char word;

while (cin >> word) {
if (word == '(')
{allwords.push(word);}
else
if (word == ')')
{allwords.pop();}
}

if (allwords.size() == 0)
{cout << "The stack is %d. It is balance!" << allwords.size() << endl;}
else
{cout << "Number of words = " << allwords.size() << endl;}


return 0;
}


So how can I count number of those braces??

Can you help me??
Reply With Quote  
Join Date: Jan 2007
Posts: 166
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: ASK about C++ stack

  #3  
Mar 1st, 2007
Originally Posted by daphnie View Post
I have just write out somrrthing like this
  1. int main() {
  2. stack<char> allwords;
  3. char word;
  4. while (cin >> word) {
  5. if (word == '(')
  6. {allwords.push(word);}
  7. else
  8. if (word == ')')
  9. {allwords.pop();}
  10. }
  11.  
  12. if (allwords.size() == 0)
  13. {cout << "The stack is %d. It is balance!" << allwords.size() << endl;}
  14. else
  15. {cout << "Number of words = " << allwords.size() << endl;}
  16.  
  17. return 0;

So how can I count number of those braces??

Can you help me??


Please use the bultin tags around your code. I changed it above for you but try to remember to use them. Anyway I've done a similar project where I had to read in an HTML document and determine how many tags and their corresponding closing tag, discarding any tags that don't need closing tags as defined in my project. With yours you might use a ratio for each opening and closing tags and determine it that way. In otherwords, if you have 25 opening braces and 25 closing braces their ratio would be 25/25, which is 1 and means that there are 25 pairs of braces. If their different values than I'll leave it up to you to figure out that one

Good luck, LamaBot
Last edited by Lazaro Claiborn : Mar 1st, 2007 at 12:36 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 6:32 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC