![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#21 | ||
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
I was going to say:
Quote:
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
||
|
|
|
|
|
#22 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#23 |
|
Hobbyist
Join Date: Sep 2005
Posts: 261
Rep Power: 4
![]() |
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. ![]() |
|
|
|
|
|
#24 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#25 | |
|
Hobbyist
Join Date: Sep 2005
Posts: 261
Rep Power: 4
![]() |
Quote:
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? |
|
|
|
|
|
|
#26 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#27 | |
|
Hobbyist
Join Date: Sep 2005
Posts: 261
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#28 | |
|
Hobbyist
Join Date: Sep 2005
Posts: 261
Rep Power: 4
![]() |
Quote:
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. |
|
|
|
|
|
|
#29 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#30 |
|
Programmer
|
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|