944,051 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 8531
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 5th, 2006
0

Stack conversion

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kellaw is offline Offline
22 posts
since Mar 2006
Mar 5th, 2006
0

Re: Stack conversion

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; }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kellaw is offline Offline
22 posts
since Mar 2006
Mar 5th, 2006
0

Re: Stack conversion

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Mar 5th, 2006
0

Re: Stack conversion

Quote 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
C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 6th, 2006
0

Re: Stack conversion

Quote 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
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kellaw is offline Offline
22 posts
since Mar 2006
Mar 6th, 2006
0

Re: Stack conversion

Quote 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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 6th, 2006
0

Re: Stack conversion

Quote 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kellaw is offline Offline
22 posts
since Mar 2006
Mar 6th, 2006
0

Re: Stack conversion

>>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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Mar 6th, 2006
0

Re: Stack conversion

Quote 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kellaw is offline Offline
22 posts
since Mar 2006
Mar 6th, 2006
0

Re: Stack conversion

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Dev C++ Help!
Next Thread in C++ Forum Timeline: O-notation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC