| | |
Stack conversion
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2006
Posts: 22
Reputation:
Solved Threads: 0
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
•
•
Join Date: Mar 2006
Posts: 22
Reputation:
Solved Threads: 0
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; }
#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; }
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.
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.
•
•
•
•
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.
You would use while
C++ Syntax (Toggle Plain Text)
((! Stack.empty())
There is plenty of information on the web documenting the use of the stack from the STL.
*Voted best profile in the world*
•
•
Join Date: Mar 2006
Posts: 22
Reputation:
Solved Threads: 0
•
•
•
•
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 whileto test if your stack is empty.C++ Syntax (Toggle Plain Text)
((! Stack.empty())
There is plenty of information on the web documenting the use of the stack from the STL.
•
•
•
•
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.
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*
•
•
Join Date: Mar 2006
Posts: 22
Reputation:
Solved Threads: 0
•
•
•
•
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.
•
•
Join Date: Jul 2005
Posts: 1,672
Reputation:
Solved Threads: 261
>>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.
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.
•
•
Join Date: Mar 2006
Posts: 22
Reputation:
Solved Threads: 0
•
•
•
•
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.
just felt like slapping him lol
•
•
Join Date: Jul 2005
Posts: 1,672
Reputation:
Solved Threads: 261
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.
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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Dev C++ Help!
- Next Thread: O-notation
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






