Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 16th, 2005, 7:00 PM   #21
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
I was going to say:
Quote:
Originally Posted by Iwasgoingtosay
Quote:
Originally Posted by Kilo
Do you possibly have this pre-written that you could send me? Please DaWei
Why not write this yourself? You'd want to:
-get input
-check to see how many items there are
-first check to see if they are int
-if not, then it's a word
-check to see if you have them all
-if not repeat
-lastly print
But then my i-net connection broke, but DaWei has given a nice example in the meantime. Note that this problem would be harder in C, learn to use the STL in C++.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Dec 16th, 2005, 7:07 PM   #22
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Actually, I used doubles in order to encompass more than integers. The program can be written in a number of ways. I used C++ techniques because this is the C++ forum. There are other methods available in C. s/f/scanf, fgets, strtok, any number of approaches would work just as well and be approximately equivalent in complexity. Robust interfaces with the user require complexity and effort one doesn't like to expend, of course, but one doesn't control one's users. An easy and forgiving interface for the user that won't crash the system is worth some effort. All the "Press 1 for blah, 2 for blegh, and 3 for effit" type of interfaces are pretty poor, when you get right down to it.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Dec 16th, 2005, 8:20 PM   #23
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 261
Rep Power: 4 Cache is on a distinguished road
I made a stack. But I've just noticed I didn't stay within the rules posted above. I'd like to post it here anyway, and maybe make it fit the rules some other day. It's taken me all day just to make this lol.

Try not to be sick lol. Here it is:

#include <iostream>
#include <string>
#include <exception>

using namespace std;

class MyStack 
{
	  public:
			 MyStack () :  Top (0), stack_array (new string[3]) {}
			 bool Empty (void);
			 void Push (string next);
			 string Pop (void);
	  private:
			  int Top;
			  string *stack_array;
			  string *temp;
			  string Ex_Top_string;
};

// Return stack state
bool MyStack::Empty (void)
{
	if (Top == 0) { return true; } else { return false; }
}

// Push items onto the stack
void MyStack::Push (string next)
{
	   
	   if (! Empty())
	   {
		  ++Top;   // Increase stack index by: 1
		  
		  try	// Try create temporary array -- 'temp'
		  {
				 temp = new string [Top];
		  }
		  catch (exception& e)   // Catch Exception, warn n' exit!
		  {
			 cout << "Exception: " << e.what() << endl;
				cout << "EXIT 1";
			 exit (1);								 
		  }
		  
		  // Copy the stack into 'temp' -- the stack array can then be resized
		  for (int i = 0; i < Top; i++) { temp[i] = stack_array[i]; } 
		  
		  try   //Try create an array large anough for the stack + 1
		  {
				stack_array = new string[Top+1];
		  }
		  catch (exception& e)   // Catch Exception, warn n' exit!
		  {
			 cout << "Exception: " << e.what() << endl;
				cout << "EXIT 1";
				exit (1);
		  }
		  
		  // Put the stack back into the main array -- 'stack_array'
		  for (int j = 0; j < Top; j++)
		  {
			  stack_array[j] = temp[j];
		  }
		  delete[] temp;   // Delete temporary array
		  
		  stack_array[Top-1] = next;   //Push the next item on the stack
	   } 
	   else
	   {   // First item on the stack must come here!
		   stack_array[Top] = next;   //Push first item onto the stack
		   ++Top;   // Increase stack index by: 1
	   }
}


string MyStack::Pop (void)
{
	   if (! Empty())
	   {
		  Ex_Top_string = stack_array[Top-1];   // Store the top stack element
		  
		  --Top;   // Decrease stack array index
		  try	// Try create temporary array -- 'temp'
		  {
				 temp = new string [Top];
		  }
		  catch (exception& e)   // Catch Exception, warn n' exit!
		  {
			 cout << "Exception: " << e.what() << endl;
				cout << "EXIT 1";
			 exit (1);								 
		  }
		  
		  // Copy the stack into 'temp' -- the stack array can then be resized
		  for (int i = 0; i < Top; i++) { temp[i] = stack_array[i]; } 
		  
		  // Put the stack back into the main array -- 'stack_array'
		  for (int j = 0; j < Top; j++)
		  {
			  stack_array[j] = temp[j];
		  }
		  delete[] temp;   // Delete temporary array
		  
		  cout << Ex_Top_string << endl;   // Print the value popped
	   }
}

int main(int argc, char *argv[])
{
	
	MyStack stack;
	string test = "This";
	string test2 = "is";
	string test3 = "my";
	string test4 = "first";
	string test5 = "stack";
	
	stack.Push(test);
	stack.Push(test2);
	stack.Push(test3);
	stack.Push(test4);
	stack.Push(test5);
	
	while (! stack.Empty())
	{
		  stack.Pop();
	}
	
	cin.sync();
	cin.get();
	return EXIT_SUCCESS;
}

There have been a lot of firsts for me while writing this. Even if it's not very good, I got a lot out of it.
Cache is offline   Reply With Quote
Old Dec 16th, 2005, 8:27 PM   #24
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
The way you learn is by doing it. Good shot. You might want to output the contents as you pop them off, just as an example of the lifo nature. I would also suggest that exceptions are an overkill under the circumstances. Just check for failure and take appropriate actions, since the fault and the dealing with it is localized. Bruce Eckel's book (free on-line), Thinking in C++ (Vol II, I think) has some good stuff on exceptions.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Dec 16th, 2005, 8:47 PM   #25
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 261
Rep Power: 4 Cache is on a distinguished road
Quote:
Originally Posted by DaWei
The way you learn is by doing it. Good shot. You might want to output the contents as you pop them off, just as an example of the lifo nature. I would also suggest that exceptions are an overkill under the circumstances. Just check for failure and take appropriate actions, since the fault and the dealing with it is localized. Bruce Eckel's book (free on-line), Thinking in C++ (Vol II, I think) has some good stuff on exceptions.
Thanks for the constructive comments, DaWei.

I just noticed, in my rush to see this completed, I forgot to resize the stack array in the member: Pop. Since the Top index is changed anyway, does that really matter?
Cache is offline   Reply With Quote
Old Dec 16th, 2005, 8:58 PM   #26
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
The only thing you need to worry about, really, is that you return everything you borrow from the free store when you're through with it.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Dec 16th, 2005, 9:07 PM   #27
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 261
Rep Power: 4 Cache is on a distinguished road
Quote:
Originally Posted by DaWei
The only thing you need to worry about, really, is that you return everything you borrow from the free store when you're through with it.
You mean deleting temp arrays, and such?
Cache is offline   Reply With Quote
Old Dec 16th, 2005, 9:40 PM   #28
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 261
Rep Power: 4 Cache is on a distinguished road
Quote:
Originally Posted by DaWei
The only thing you need to worry about, really, is that you return everything you borrow from the free store when you're through with it.
In trying to figure out what you mean, I'm starting to think your under the impression that I didn't write my code myself. Is this correct?

EDIT: I should have done some research before making assumptions. I see now that you mean 'heap memory' or 'dynamic memory'.

Last edited by Cache; Dec 16th, 2005 at 9:53 PM.
Cache is offline   Reply With Quote
Old Dec 17th, 2005, 7:02 AM   #29
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Lol . Sorry you got THAT impression!.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Dec 17th, 2005, 9:29 AM   #30
drifter
Programmer
 
drifter's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia
Posts: 39
Rep Power: 0 drifter is an unknown quantity at this point
Send a message via ICQ to drifter Send a message via MSN to drifter
Actually, I think the best thing to start with, would be either a Temperature Converter (F to C and back again) or a Wind Chill Factor Calculator.
Those are the two things they had us start with in school. (Besides the overly pointless[besides teaching you how to output to the screen], but always done cuz its like an unspoken rule *Hello World*)

Then we created a Tic Tac Toe game. annoying, but good way to learn arrays and multiple function use. *shrugs* you could use classes for that app too, but I did not at the time, since we hadn't gotten to that point by then.

There is a text book I really do recommend, since it teaches you everything you need for basics in C++ as well as gives you exercises based on what you've learned for those chapters.
Absolute C++ (Second Edition) by Walter Savitch
If you can't find that title, here is the ISBN Number 0-321-33023-4
__________________
Only two things are infinite, the universe and human stupidity, and im not sure about the former.
drifter is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:03 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC