/*--------------------------------------------------------------------------+
| Name: Jerome Richards |
| Course: CNS-2400 F01 |
| |
| Program: Integer Stack |
| File: driver.cpp |
+--------------------------------------------------------------------------*/
#include
#include
#include
#include
#include "intStack.h"
using namespace std;
enum CMD { PUSH, POP, UNDEFINED };
struct cmdData
{
CMD itsCommand;
char itsTarget;
int itsData;
};
void dispPrologue();
bool openFile( ifstream& inFile );
bool readData( ifstream& inFile, string& textLine );
void processCmd( Integer_stack& myStack, string textLine );
bool parseCmd( string textLine, cmdData& theCmd );
void dumpStack( Integer_stack& myStack );
/*--------------------------------------------------------------------------+
| main() |
| |
| Establishes the program's environment by displaying the Prologue, and |
| the data file to be processed. |
| |
| The program will terminate if a filename is not entered. |
+--------------------------------------------------------------------------*/
int main( int argc, char* argv[] )
{
ifstream inFile;
Integer_stack myStack;
string textLine;
dispPrologue();
while( openFile( inFile ) )
{
cout << "Operation Stack Value Results" << endl;
cout << "--------- ----- --------- -------" << endl;
while( ! inFile.eof() )
{
if( readData( inFile, textLine ) )
processCmd( myStack, textLine );
}
inFile.close();
inFile.clear();
dumpStack( myStack );
}
return 0;
}
/*--------------------------------------------------------------------------+
| processCmd( Integer_stack& myStack, string textLine ) |
| |
| Purpose: process the command read from the data file |
| |
| Parameters: textLine contains the data to be parsed |
| Pre-conditions: myStack needs to be a valid Integer_stack object |
| Post-conditions: myStack is modified based on command contents |
| |
| Returns: none |
+--------------------------------------------------------------------------*/
void processCmd( Integer_stack& myStack, string textLine )
{
Integer_stack::stackCode tmpCode = Integer_stack::isUNKNOWN;
cmdData theCmd;
if( parseCmd( textLine, theCmd ) )
{
switch( theCmd.itsCommand )
{
case PUSH:
cout << "push ";
switch( theCmd.itsTarget )
{
case 'a':
cout << " a ";
cout.setf( ios_base::right );
cout << setw( 9 ) << theCmd.itsData;
cout.setf( ios_base::left );
cout << " ";
tmpCode = myStack.pusha( theCmd.itsData );
break;
case 'b':
cout << " b ";
cout.setf( ios_base::right );
cout << setw( 9 ) << theCmd.itsData;
cout.setf( ios_base::left );
cout << " ";
tmpCode = myStack.pushb( theCmd.itsData );
break;
default:
cout << "----- --------- ";
break;
}
cout << myStack.getErrStr( tmpCode ) << endl;
break;
case POP:
cout << "pop ";
switch( theCmd.itsTarget )
{
case 'a':
cout << " a ";
cout.setf( ios_base::right );
cout << setw( 9 ) << "";
cout.setf( ios_base::left );
cout << " ";
tmpCode = myStack.popa();
break;
case 'b':
cout << " b ";
cout.setf( ios_base::right );
cout << setw( 9 ) << "";
cout.setf( ios_base::left );
cout << " ";
tmpCode = myStack.popb();
break;
default:
cout << "----- --------- ";
break;
}
cout << myStack.getErrStr( tmpCode ) << endl;
break;
default:
cout << "unrecognized command" << endl;
break;
}
}
cout.setf( ios_base::left );
}
/*--------------------------------------------------------------------------+
| parseCmd( string textLine, cmdData& theCmd ) |
| |
| Purpose: parst the command from a text line |
| |
| Parameters: textLine contains the data to be parsed |
| theCmd will receive the command, if any |
| |
| Pre-conditions: none |
| Post-conditions: theCmd is modified to contain the command/parameters |
| |
| Returns: true if successful, false otherwise |
+--------------------------------------------------------------------------*/
bool parseCmd( string textLine, cmdData& theCmd )
{
int nPos;
theCmd.itsCommand = UNDEFINED;
theCmd.itsTarget = 0x00;
theCmd.itsData = -1;
nPos = textLine.find( "push " );
if( nPos >= 0 )
{
theCmd.itsCommand = PUSH;
theCmd.itsTarget = textLine[ 5 ];
const char* tmpPtr = textLine.c_str();
theCmd.itsData = atoi( tmpPtr+7 );
return true;
}
nPos = textLine.find( "pop " );
if( nPos >= 0 )
{
theCmd.itsCommand = POP;
theCmd.itsTarget = textLine[ 4 ];
return true;
}
return false;
}
/*--------------------------------------------------------------------------+
| dumpStack( Integer_stack& myStack ) |
| |
| Purpose: dumps the contents of the stack(s) to the display |
| |
| Parameters: myStack is a reference to a stack object |
| |
| Pre-conditions: none |
| Post-conditions: myStack is emptied - no items will remain |
| |
| Returns: none |
+--------------------------------------------------------------------------*/
void dumpStack( Integer_stack& myStack )
{
Integer_stack::stackCode tmpCode;
int tmpInt;
int tmpPtr;
cout << endl << endl << "A Stack" << endl;
tmpPtr = 0;
do
{
tmpCode = myStack.topa( tmpInt );
if( tmpCode == Integer_stack::isSUCCESS )
{
cout << "A[" << setw(2) << tmpPtr << "] = " << setw(10) << tmpInt << endl;
tmpPtr++;
myStack.popa();
}
} while( tmpCode == Integer_stack::isSUCCESS );
cout << endl << endl << "B Stack" << endl;
tmpPtr = 0;
do
{
tmpCode = myStack.topb( tmpInt );
if( tmpCode == Integer_stack::isSUCCESS )
{
cout << "B[" << setw(2) << tmpPtr << "] = " << setw(10) << tmpInt << endl;
tmpPtr++;
myStack.popb();
}
} while( tmpCode == Integer_stack::isSUCCESS );
}
/*--------------------------------------------------------------------------+
| readData( ifstream& inFile, string& textLine ) |
| |
| Purpose: read a line of text from the data file |
| |
| Parameters: inFile is the data stream |
| textLine receives the data |
| |
| Pre-conditions: inFile is a valid input stream |
| Post-conditions: textLine is modified to contain the data read |
| |
| Returns: true if successful, false otherwise |
+--------------------------------------------------------------------------*/
bool readData( ifstream& inFile, string& textLine )
{
bool lOkay = false;
getline( inFile, textLine );
if( ! inFile.fail() )
lOkay = true;
return lOkay;
}
/*--------------------------------------------------------------------------+
| openFile( ifstream& inFile ) |
| |
| Purpose: open a user specified data file |
| |
| Parameters: inFile is the data file stream to open |
| |
| Pre-conditions: none |
| Post-conditions: inFile will be opened for input if the file is found |
| |
| Returns: true if successful, false otherwise |
+--------------------------------------------------------------------------*/
bool openFile( ifstream& inFile )
{
const int MAXLEN = 80;
bool lOpen = false;
char fileName[ MAXLEN + 1 ];
while( ! lOpen )
{
cout << "Enter file name (blank to exit): ";
cin.getline( fileName, MAXLEN, '\n' );
if( ! strlen( fileName ) )
{
cout << endl << endl << "Press ENTER to exit the program";
cin.get();
exit( 0 );
}
inFile.open( fileName );
if( ! (lOpen = inFile.good()) )
{
cout << endl << "ERROR: could not open data file " << fileName << endl;
inFile.clear();
}
}
return lOpen;
}
/*--------------------------------------------------------------------------+
| dispPrologue() |
| |
| Purpose: display the author and program name |
| |
| Parameters: none |
| |
| Pre-conditions: none |
| Post-conditions: the screen is cleared, and the prologue is displayed |
| |
| Returns: none |
+--------------------------------------------------------------------------*/
void dispPrologue()
{
system( "cls" );
cout << " Name: Jerome Richards" << endl;
cout << " Course: CNS-2400 F01" << endl;
cout << "Program: Integer Stack" << endl;
cout << " File: jrp2.exe" << endl;
cout << endl << endl;
}