GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Oct-2008, 02:28
glacier glacier is offline
New Member
 
Join Date: Sep 2006
Posts: 19
glacier is an unknown quantity at this point

Test driver code for stack class


hi,
I am very confuse in writing a test program for stack class
o/p should be like this this with using try and catch block
Choose a valid stack operation:
1 - Push an item
2 - Pop an item
3 - Get Top item
4 - Determine if stack is full
5 - Determine if stack is empty
6 - Clear all items from the stack
7 - Print the stack
0 - Quit

Selection:

codes for stack.h and cpp are here,

CPP / C++ / C Code:
// stack.h
const int MAX_SIZE= 20;
class FullStack                         //Exception class thrown by Push when stack is full.
{};

class EmptyStack                        //Exception class thrown by Pop and Top when stack is empty.
{};


class StackType

{
    public:
        StackType();                     // Class constructor
        void Clear();                    // Clear the stack contents
        bool IsFull() const;             // Determines whether the stack is full
        bool IsEmpty() const;            // Determines whether the stack is empty
        void Push(char item);            // Adds an item to the top of the stack.
        char Pop();                      // Removes and returns the top item from the stack
        char Top();                      // Returns a copy of top item on the stack.
        void Print();                    // Prints contents of stack to screen from top to bottom


    private:
        char   items[MAX_SIZE];          //array
        int    top;                      // Top of stack . array subscript value or -1


};



// stack.cpp




StackType::StackType()

{
    top = -1;
}


bool StackType::IsEmpty() const
{
    return (top == -1);
}

bool StackType::IsFull() const
{
    return (top == MAX_SIZE-1);
}


void StackType::Push(char newItem)
{
    if (IsFull())
        throw FullStack();
    top++;
    items[top] = newItem;
}



char StackType::Pop()

{
    if(IsEmpty())
        throw EmptyStack();
    top--;
    return items[top];
}
Last edited by LuciWiz : 02-Oct-2008 at 02:57. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #2  
Old 02-Oct-2008, 03:10
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: need help test driver code for stack class


Quote:
Originally Posted by glacier
I am very confuse in writing a test program for stack class...
Treat main() as the highest abstraction of the problem:
  • Create a single stack instance.
  • Create a loop which will continually display the text described:
    Code:
    Choose a valid stack operation: 1 - Push an item 2 - Pop an item 3 - Get Top item 4 - Determine if stack is full 5 - Determine if stack is empty 6 - Clear all items from the stack 7 - Print the stack 0 - Quit Selection:
  • Receive user input.
  • Based upon whatever is received, call the appropriate member function with whatever parameters are required.
It looks like you have a start on at least the stack operations.
  #3  
Old 03-Oct-2008, 04:14
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Test driver code for stack class


Create test suite.
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
User defined headers davis Miscellaneous Programming Forum 6 16-Feb-2006 19:40
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 08:10
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13
CSS Layout question oihjk Web Design Forum 3 28-May-2003 12:36

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:12.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.