Stack conversion

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2006
Posts: 22
Reputation: Kellaw is an unknown quantity at this point 
Solved Threads: 0
Kellaw Kellaw is offline Offline
Newbie Poster

Stack conversion

 
0
  #1
Mar 5th, 2006
How do you implement a binary number to convert into an equivalent decimal number using stack in C++.I am having a headache here.I have been thinking the past 2 days but without much knowledge in C++ i am partially dead. Tried reading books but still can't figure it out and time is running short on me.Please advice and help.Thanks in advance
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 22
Reputation: Kellaw is an unknown quantity at this point 
Solved Threads: 0
Kellaw Kellaw is offline Offline
Newbie Poster

Re: Stack conversion

 
0
  #2
Mar 5th, 2006
I saw a previous thread of code but i think it is not using stacks
#include <string.h> int btoi(char *buf) { // Create local variables int count, tmp; int retValue = 0; // Get length of string count = strlen(buf); // Go through string one byte at a time (backwards) for (i = 0; i <= count; i++) { // If a number '1' was found if (buf[count-i] == '1') { tmp = 1; for (j = 1; j < i; j++) tmp *= 2; retValue += tmp; } } // Return our decimal value return retValue; }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Stack conversion

 
0
  #3
Mar 5th, 2006
There are essentially two steps to the problem, given
void foo ( int num );

1. do something with num % 2
2. call foo ( num / 2 );

Changing the order of those two steps changes what happens - feel free to experiment.

Oh, and you also need something to decide when to stop recursing otherwise you'll just go on until you run out of stack.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Stack conversion

 
0
  #4
Mar 5th, 2006
Originally Posted by Salem
There are essentially two steps to the problem, given
void foo ( int num );

1. do something with num % 2
2. call foo ( num / 2 );

Changing the order of those two steps changes what happens - feel free to experiment.

Oh, and you also need something to decide when to stop recursing otherwise you'll just go on until you run out of stack.
Yes essentially you are using the stack instead of recursion. So if you understand recursion than that shouldn't be a problem.

You would use while
  1. ((! Stack.empty())
to test if your stack is empty.
There is plenty of information on the web documenting the use of the stack from the STL.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 22
Reputation: Kellaw is an unknown quantity at this point 
Solved Threads: 0
Kellaw Kellaw is offline Offline
Newbie Poster

Re: Stack conversion

 
0
  #5
Mar 6th, 2006
Originally Posted by iamthwee
Yes essentially you are using the stack instead of recursion. So if you understand recursion than that shouldn't be a problem.

You would use while
  1. ((! Stack.empty())
to test if your stack is empty.
There is plenty of information on the web documenting the use of the stack from the STL.
you mean my program is already using stack?actually i know nothing about stack and i just started c++ a week ago. i am still learning now.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Stack conversion

 
0
  #6
Mar 6th, 2006
Originally Posted by Kellaw
you mean my program is already using stack?actually i know nothing about stack and i just started c++ a week ago. i am still learning now.
huh?

You started programming a week ago and you're already learning data structures? Hmm, you must have an incompetent teacher like myself. LOL.

Anyhow, let's try and make sense of this.

>you mean my program is already using stack

By this are you asking, 'is there already a facility in c++ such that I don't have to write my own stack, templated procedure?' If that's the case then yes. Using 'stack' from the STL will provide you will all the rudimentary things you need to do. Like push and pop stuff from the stack.

>i am still learning now

me too welcome to the club.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 22
Reputation: Kellaw is an unknown quantity at this point 
Solved Threads: 0
Kellaw Kellaw is offline Offline
Newbie Poster

Re: Stack conversion

 
0
  #7
Mar 6th, 2006
Originally Posted by iamthwee
huh?

You started programming a week ago and you're already learning data structures? Hmm, you must have an incompetent teacher like myself. LOL.

Anyhow, let's try and make sense of this.

>you mean my program is already using stack

By this are you asking, 'is there already a facility in c++ such that I don't have to write my own stack, templated procedure?' If that's the case then yes. Using 'stack' from the STL will provide you will all the rudimentary things you need to do. Like push and pop stuff from the stack.

>i am still learning now

me too welcome to the club.
well i am learning on my own.i need to come up with this project and the F***ing lecturer only know how to ask me to refer to books. he says i should learn how to write up a stack and use the stack as binary to be converted to decimal.he say he learn that in a day when he started learning.it means i basicallt have to do the same lol.is there a specific function to push and pop.i only know the basic function and a little on recursion.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,672
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Stack conversion

 
0
  #8
Mar 6th, 2006
>>is there a specific function to push and pop

No. It depends on the implementation. If you are going to implement your own stack then you get to decide how do set things up. If you are using a third party implementation then you use the functions provided without worrying about the implementation as long as they meet your needs.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 22
Reputation: Kellaw is an unknown quantity at this point 
Solved Threads: 0
Kellaw Kellaw is offline Offline
Newbie Poster

Re: Stack conversion

 
0
  #9
Mar 6th, 2006
Originally Posted by Lerner
>>is there a specific function to push and pop

No. It depends on the implementation. If you are going to implement your own stack then you get to decide how do set things up. If you are using a third party implementation then you use the functions provided without worrying about the implementation as long as they meet your needs.
i am supposed to come up with the stack too.i just called my lecturer.he sounds sarcastic.he says "do i look like an idiot to you?if you are not gona do the stack you expect me to do it?"
just felt like slapping him lol
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,672
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Stack conversion

 
0
  #10
Mar 6th, 2006
The easiest answer is that you will probably find any number of stack implementations by searching the web. However, you will learn more by doing it yourself.

The two versions of implementing a stack that I'm most familiar with use either an array or a list as the underlying container, and build the stack as a wrapper around the underlying container. I am only familiar with implementing stacks using C++, so if you want to do it in C then I'll be of less help.

With an array based stack class you declare an array of a given size, you can use either static or dynamic memory, and then have a second member variable keep track of the last index actually used. Popping then amounts to decrementing the last index and pushing should be straightforward from what I've said already.

With a list, I find it easiest to always push/pop from the head of the list.

This information plus what Salem gave you should put you well on the way to at least roughing out your project. Post code and specific questions if necessary and someone will probably help from there.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC