/** @class CStack
Stack class implementation.
@author Peter Hansen (hco03phe@student.hig.se)
@date 2005-11-25
*/
#ifndef CStack_H
#define CStack_H
/**
Stack exceptions
This is the different types of exceptions the stack class will cast when error occurs.
*/
enum StackExceptions {
STACK_EMPTY, //!< Illegal operation on empty stack.
STACK_FULL, //!< Illegal operation on full stack.
STACK_FAILED_TO_CREATE //!< Failed to create stack.
};
#include <iostream>
using namespace std;
template <class T>
class CStack {
int iSize; //!< Max size of stack.
int iLastItem; //!< Last item position inserted in stack.
T *stack; //!< Stack array pointer.
public:
/**
Will initialize a stack array with default size of 10 elements.
@param size - The max size of the array (Default = 10).
*/
CStack(int size = 10)
{
iSize = size;
stack = NULL;
stack = new T[iSize];
if( stack == NULL )
throw STACK_FAILED_TO_CREATE;
iLastItem = -1;
}
/**
Destroys stack array.
@warning
It doesn't delete objects stored in the stack only the stack itself.
So you should delete the object self by POP each element and delete them.
*/
~CStack()
{
if( stack != NULL )
delete[] stack;
}
/**
Push item on top of stack.
@param item - The item to push on stack.
*/
void push( const T &item )
{
if( isFull() )
throw STACK_FULL;
stack[++iLastItem] = item;
}
/**
Pops the top element of the stack.
@return The item it poped from stack.
*/
T pop()
{
if( isEmpty() )
throw STACK_EMPTY;
return stack[iLastItem--];
}
/**
Check if stack is empty.
@return \c True if stack is empty.
*/
bool isEmpty() { return (iLastItem == -1); }
/**
Check if stack is full.
@return \c True if stack is full.
*/
bool isFull() { return ( iLastItem == iSize-1); }
/**
Take a peek at the top element.
@return The top element in stack without removing it.
*/
T peek()
{
if( isEmpty )
throw STACK_EMPTY;
return stack[iLastItem];
}
/**
Clears the stack.
@warning
It doesn't delete objects stored in the stack only the stack itself.
So you should delete the object self by POP each element and delete them.
*/
void clear()
{
iLastItem = -1;
}
};
#endif // CStack_H
--------------------------------- teststack.cpp
#include <iostream>
using namespace std;
#include "CStack.h"
/**
String results.
Return values for string test.
*/
enum eStringResult {
STR_OK, //!< String is balanced.
STR_LESS_END, //!< String missing one \c ).
STR_MORE_END, //!< String missing one \c (.
STR_ERROR //!< Internal error.
};
/**
Function to test if a string has a balanced number of \c ( and \c ).
@param str - input string to test.
@return STR_OK, STR_LESS_END, STR_MORE_END, STR_ERROR.
*/
int testString( char *str )
{
try
{
CStack<char> stack;
int len = (int)strlen(str);
for( int i=0; i<len; i++ )
{
if( str == '(' )
{
stack.push(str);
}
else if( str == ')' )
{
if( stack.isEmpty() )
return STR_MORE_END;
stack.pop();
}
}
if( !stack.isEmpty() )
return STR_LESS_END;
} catch(StackExceptions se) {
switch( se )
{
case STACK_EMPTY:
cout << "Tried to pop on empty stack." << endl;
return STR_ERROR;
case STACK_FULL:
cout << "Tried to push on full stack." << endl;
return STR_ERROR;
case STACK_FAILED_TO_CREATE:
cout << "Failed to create stack." << endl;
return STR_ERROR;
default:
cout << "Unknown error" << endl;
return STR_ERROR;
};
};
return STR_OK;
}
/**
The main function.
*/
int main(void)
{
char str[1024]; // input array of characters to store user inputstring in.
// loop forever, or atleast until something breaks the loop.
while(1) {
// get input string from user
cout << "enter string: ";
cin.getline(str, 1024);
// quit if string is empty. it is empty if first character is \0 which
// is a string termination character.
if( str[0] == '\0' )
break;
int s = testString( str );
switch(s)
{
case STR_OK:
cout << "str is ok" << endl;
break;
case STR_LESS_END:
cout << "missing at least one end ')'" << endl;
break;
case STR_MORE_END:
cout << "missing at least one beginning '('" << endl;
break;
case STR_ERROR:
// Error message already written so just ignore this
break;
default:
cout << "some other error" << endl;
break;
};
};
}
can't run VC++ 2005 express compiled binaries on another computer?
well, I have made a few mini-projects for school at home using the new VC++ 2005 express... but the problem is that it works perfekt at home but not in school, not at my other computer which doesn't have VC++ 2005 Express installed.
the projects are simple console projects so no PSDK is installed yet
Here is the full source code for the Stack implementation of the mini-projecte.
Why can't I run them on another computer? I have nothing else than the above in my code or project. do they require some new .dll file for running?
I get an error like this when I try to start it (freely translated from Swedish):
This program could not start because the configuration of the program is wrong. The problem may be fixed if software is reinstalled.
EDIT: I wrote and compiled it running Win XP 64 but it runs perfectly on the same computer running 32bit Win XP Pro SP2 with VC++ 2005 Express installed, but not on my other computer with Win XP Pro SP2 with out VC++ 2005 express.
EDIT Again: I have recompiled the code in 32bit Win XP just in case that had something to do with it but it didn't change anything still the same error.
How are you linking to the runtime libraries? You may have to redistribute msvc80.dll and/or msvcr80.dll. Best best is to open your executable/dll in dependency walker to see what files it links to externally.
http://www.dependencywalker.com/
http://www.dependencywalker.com/
Have you compiled your project in "Release" mode rather than "Debug"? If you compile in debug mode it will link to the debugging dll's rather than the normal ones and hence won't run on a computer without the debug librarys installed (ie, on a machine without Visual Studio installed).
How could I miss that?
it was linked to msvc80d.dll & msvcr80d.dll files so by tell it to link the libs static it now works ok :)
Thanks for the help.
it was linked to msvc80d.dll & msvcr80d.dll files so by tell it to link the libs static it now works ok :)
Thanks for the help.
A few comments on your code, if I may. First of all, you have an error in your code. Since it sounds like this is homework I'm not going to tell you where, but at least you're now aware of it. Also:
CStack.h:
if( stack == NULL ) throw STACK_FAILED_TO_CREATE; - Will never occur under any standards-conforming compiler. If new[] fails to allocate memory it will throw an exception of type std::bad_alloc (or something derived from std::bad_alloc).
if( stack != NULL ) delete[] stack; - The test is completely redundant. delete[]ing a null pointer is safe and has no effect.
teststack.cpp:
Enigma
CStack.h:
- #include <iostream> - This include is unneccessary.
- using namespace std; - Dangerous in a header file. Now any header included after this one is also using namespace std;, whether it wanted to be or not. Also unneccessary since you're not actually using any of the standard library in this header.
teststack.cpp:
- char str[1024]; - Any particular reason for using a char array instead of a real string?
- cin.getline(str, 1024); - As justification for the above, try entering a string longer than 1024 characters and then try entering another string afterwards.
Enigma
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement