Quantcast

Maximum PC

It is currently Wed Aug 27, 2014 3:26 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 46 posts ]  Go to page Previous  1, 2, 3  Next
Author Message
 Post subject:
PostPosted: Tue Jul 29, 2008 12:30 pm 
Team Member Top 500
Team Member Top 500
User avatar

Joined: Sun Apr 02, 2006 7:32 pm
Posts: 1323
So would this be proper pop and push functions for the class?

Code:
class stack
{
public:
   
   void push(char i)
   {
      char * stackChar = new char[100];
      int stackSize = 0;
      stackChar[stackSize++] = i;
   }   
   int pop()
   {
      char * stackChar = new char[100];
      int stackSize = 0;
      return stackChar[--stackSize];
   }
};


How would I check if the stack was empty and how would I impliment it into my hexadecimal function:



Code:
void hexadecimal(int number)
{
   stack hex;
   int remainder;
   while (number > 0)
   {
      remainder = number % 16;
      number = number/16;
      if (remainder > 9)
         hex.push('A'  + remainder - 10) ;
      else hex.push(remainder);
   }
};


Top
  Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 12:41 pm 
Monkey Federation (Top 10)*
Monkey Federation (Top 10)*
User avatar

Joined: Sun May 22, 2005 8:28 am
Posts: 3673
Location: The Blue Nowhere
The stack class is already implemented as part of the STL.

Here is a quick and dirty on stacks http://www.cppreference.com/cppstack/index.html


Top
  Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 2:14 pm 
Million Club - 5 Plus*
Million Club - 5 Plus*
User avatar

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4745
Location: In the monkey's litterbox
hbgrg wrote:
So would this be proper pop and push functions for the class?


It's really close. Store the array and the number of elements in the stack in private class level variables.

That will also get you isEmpty()

@Lodis4: The exercise here is to not use the STL for the stacks. Anyways, doing it by hand is always good to know (at least the first time, then the STL should be used)


Top
  Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 2:25 pm 
Team Member Top 500
Team Member Top 500
User avatar

Joined: Sun Apr 02, 2006 7:32 pm
Posts: 1323
Ok how do I know how many times to pop?

EDIT:

Code:
#include <iostream>
using namespace std;
class stack
{
public:
   void push(char i)
   {
      int stackSize = 0;
      stackChar[stackSize++] = i;
   }   
   int pop()
   {
      int stackSize = 0;
      return stackChar[--stackSize];
   }
   bool isEmpty()
   {
      if (stackSize = 0)
         bool empty = true;
   }
   void showSize ()
   {
      cout << stackSize;
   }
private:
   char stackChar[100];
   int stackSize;
};

void binary(int number)
{
   int remainder;

   if(number <= 1)
   {
      cout << number;
      return;
   }

   remainder = number%2;
   binary(number >> 1);   
   cout << remainder;
}

void hexadecimal(int number)
{
   stack hex;
   int remainder;
   while (number > 0)
   {
      remainder = number % 16;
      number = number/16;
      if (remainder > 9)
         hex.push('A'  + remainder - 10) ;
      else hex.push(remainder);
   }
};
void octal(int number)
{
   int remainder;
   int i;
   int ten = 10;
   remainder = number % 8;
   number = number/8;
   i = remainder;
   while (number >= 1)
   {
      remainder = number%8;
      number = number/8;
      i += remainder * ten;
      ten = ten * 10;
   }
   cout << i << "\n";
};
int main ()
{
   int number;
   cout << "Enter a number in decimal: ";
   cin >> number;
   int spec;
   cout << "Specify whether you want to convert to Binary, Octal, or Hexadecimal (1-3): ";
   cin >> spec;
   if (spec == 1)
   {
      binary (number);
      cout << "\n";
   }
   if (spec == 2)
      octal (number);
   if (spec == 3)
      hexadecimal (number);
   return 0;
}


There is my entire code did I make the stack class right? How do I implement the popping?


Top
  Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 4:38 pm 
Million Club - 5 Plus*
Million Club - 5 Plus*
User avatar

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4745
Location: In the monkey's litterbox
You only ever pop once per function call, just like you implemented it. Add a check to see if its empty first and it should work like a charm.

This is wrong (for a very obvious reason and one not as obvious reason ;) )
Code:
bool isEmpty()
   {
      if (stackSize = 0)
         bool empty = true;
   }


Top
  Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 5:34 pm 
Team Member Top 50
Team Member Top 50

Joined: Sat Jun 25, 2005 11:04 am
Posts: 1026
hbgrg wrote:
Code:
#include <iostream>
using namespace std;
void push(char i)
{
   int stackSize = 0;
   stackChar[stackSize++] = i;
}   
int pop()
{
   int stackSize = 0;
   return stackChar[--stackSize];
}


These two are wrong also (for the same exact reason).


Top
  Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 5:43 pm 
Team Member Top 500
Team Member Top 500
User avatar

Joined: Sun Apr 02, 2006 7:32 pm
Posts: 1323
I hate to be retarded (I can't help it in c++) but what would that reason be? It all looks right to me exept for maybe the bool cuz I didn't really know what to do on that one...


Top
  Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 6:09 pm 
Million Club - 5 Plus*
Million Club - 5 Plus*
User avatar

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4745
Location: In the monkey's litterbox
stackSize is being redefined as a locally scoped variable in those.

Assignment in the if statement (and locally scoped variable with no return value)


Top
  Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 6:44 pm 
Team Member Top 500
Team Member Top 500
User avatar

Joined: Sun Apr 02, 2006 7:32 pm
Posts: 1323
I understand what you mean because if theyre local they're deleted and can't be used in other places right? But how do I set stackSize to 0 without redefining it as a local variable?


Top
  Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 8:15 pm 
Million Club - 5 Plus*
Million Club - 5 Plus*
User avatar

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4745
Location: In the monkey's litterbox
stackSize is a class variable, initialize it in the constructor of the class and assume it's ready when you go into the function.

(you don't want to say you have an empty stack and then remove one from it anyways)


Top
  Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 9:18 pm 
Team Member Top 500
Team Member Top 500
User avatar

Joined: Sun Apr 02, 2006 7:32 pm
Posts: 1323
What am I doing wrong :cry: :cry: :cry:

Code:
#include <iostream>
using namespace std;
class stack
{
public:
   stack ();
   ~stack() {}
   void push(char i)
   {
      stackChar[stackSize++] = i;
   }   
   int pop()
   {
      return stackChar[--stackSize];
   }
   bool isEmpty()
   {
      if (stackSize = 0)
         empty = true;
      else
         empty = false;
      return empty;
   }
private:
   char stackChar[100];
   int stackSize;
   bool empty;
};
stack::stack()
{
   stackSize = 0;
}


I get no result when I push and then pop :cry:


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jul 30, 2008 10:25 am 
Team Member Top 50
Team Member Top 50

Joined: Sat Jun 25, 2005 11:04 am
Posts: 1026
hbgrg wrote:
Code:
        int pop()
   {
      return stackChar[--stackSize];
   }
   bool isEmpty()
   {
      if (stackSize = 0)
         empty = true;
      else
         empty = false;
      return empty;
   }

In your pop() method, return type does not match the type of the array.

In you isEmpty() method, you still have an assignment in your if statement.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jul 30, 2008 11:18 am 
Team Member Top 500
Team Member Top 500
User avatar

Joined: Sun Apr 02, 2006 7:32 pm
Posts: 1323
How would I check if the stack is empty without using an if statement? Should I use some other kind of statement like for or while or something? and the push and pop still dont work :[

Stack Class:
Code:
class stack
{
public:
   stack ();
   ~stack() {}
   void push(char i)
   {
      stackChar[stackSize++] = i;
   }   
   char pop()
   {
      return stackChar[--stackSize];
   }
   bool isEmpty()
   {
      if (stackSize != 0)
         empty = false;
      else
         empty = true;
      return empty;
   }
private:
   char stackChar[100];
   int stackSize;
   bool empty;
};


Hexadecimal Converter Function:
Code:
void hexadecimal(int number)
{
   stack hex;
   int remainder;
   while (number > 0)
   {
      remainder = number % 16;
      number = number/16;
      if (remainder > 9)
         hex.push('A'  + remainder - 10) ;
      else hex.push(remainder);
   }
   hex.pop();
};


Help? I need to turn this in tonight :[[[[


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jul 30, 2008 12:05 pm 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
Location: Granite Heaven
hbgrg wrote:
How would I check if the stack is empty without using an if statement?


He didn't say that you can't do it with an if-statement (you need one, actually). He said that you don't need an assignment statement. You don't need the 'empty' variable at all.

What is wrong with the rest of your code? I'm not compiling it ... you need to figure most of this out yourself, so if you can't decipher the compilation errors, post those and we can help you figure out what the problem is.

Learning to debug is almost as important as learning not to make bugs in the first place. :)


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jul 30, 2008 12:45 pm 
Team Member Top 500
Team Member Top 500
User avatar

Joined: Sun Apr 02, 2006 7:32 pm
Posts: 1323
I'd be fine with debugging hahaha... But theres no errors o__O it just doesn't display anything when I enter an integer and try to convert it to hexadecimal :(


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jul 30, 2008 1:35 pm 
Team Member Top 500
Team Member Top 500
User avatar

Joined: Sun Apr 02, 2006 7:32 pm
Posts: 1323
Im getting kinda desperate now as this assignment is due in like a few hours o__O

Code:
class stack
{
public:
   stack () {stackSize = 0;};
   ~stack() {}
   void push(char i)
   {
      stackChar[stackSize++] = i;
   }   
   void pop()
   {
      while (stackSize > 0)
         cout << stackChar[--stackSize];
   }
   bool isEmpty ()
   {
      if (stackSize = 0)
         return true;
      else
         return false;
   }
}


so I changed it up a bit... Now when I entered the number 3345 it gave me "D :) :)" whats up with that? PLEASE HELP?!?!


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jul 30, 2008 2:48 pm 
Northwood
Northwood
User avatar

Joined: Mon Sep 17, 2007 1:27 pm
Posts: 3311
Location: Toronto, ON
here is a math forum explaining how to solve math problems with c++

http://mathforum.org/library/drmath/view/54347.html


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jul 30, 2008 3:07 pm 
Team Member Top 500
Team Member Top 500
User avatar

Joined: Sun Apr 02, 2006 7:32 pm
Posts: 1323
Thats very helpful but I am doing this for a class and have specific requirments for what I have to do... My class starts in an hour o__O and todays the last day to turn in assignments... can someone help me? I think im getting the letters part fine but when it comes to numbers it usually couts smiley faces... Why is it doing that?


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jul 30, 2008 3:58 pm 
Million Club - 5 Plus*
Million Club - 5 Plus*
User avatar

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4745
Location: In the monkey's litterbox
hbgrg wrote:
Thats very helpful but I am doing this for a class and have specific requirments for what I have to do... My class starts in an hour o__O and todays the last day to turn in assignments... can someone help me? I think im getting the letters part fine but when it comes to numbers it usually couts smiley faces... Why is it doing that?


Even if its too late, post the entire source code so we can run it and see what's going on


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jul 30, 2008 4:45 pm 
Team Member Top 500
Team Member Top 500
User avatar

Joined: Sun Apr 02, 2006 7:32 pm
Posts: 1323
Here it is:

Code:
#include <iostream>
using namespace std;
class stack
{
public:
   stack () {stackSize = 0;};
   ~stack() {}
   void push(char i)
   {
      stackChar[stackSize++] = i;
   }   
   void pop()
   {
      while (stackSize > 0)
         cout << stackChar[--stackSize];
   }
   bool isEmpty ()
   {
      if (stackSize = 0)
         return true;
      else
         return false;
   }
private:
   char stackChar[100];
   int stackSize;
};
void binary(int number)
{
   int remainder;

   if(number <= 1)
   {
      cout << number;
      return;
   }

   remainder = number%2;
   binary(number >> 1);   
   cout << remainder;
}

void hexadecimal(int number)
{
   stack hex;
   int remainder;
   while (number > 0)
   {
      remainder = number % 16;
      number = number/16;
      if (remainder > 9)
         hex.push('A'  + remainder - 10);
      else
         hex.push(remainder);
   }
      hex.pop();
};
void octal(int number)
{
   int remainder;
   int i;
   int ten = 10;
   remainder = number % 8;
   number = number/8;
   i = remainder;
   while (number >= 1)
   {
      remainder = number%8;
      number = number/8;
      i += remainder * ten;
      ten = ten * 10;
   }
   cout << i << "\n";
};
int main ()
{
   int number;
   cout << "Enter a number in decimal: ";
   cin >> number;
   int spec;
   cout << "Specify whether you want to convert to Binary, Octal, or Hexadecimal (1-3): ";
   cin >> spec;
   if (spec == 1)
   {
      binary (number);
      cout << "\n";
   }
   if (spec == 2)
      octal (number);
   if (spec == 3)
      hexadecimal (number);
   return 0;
}



Thanks for any help,

Greg


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 46 posts ]  Go to page Previous  1, 2, 3  Next

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group