Path: utzoo!attcan!uunet!aplcen!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!mips!prls!pyramid!infmx!briand From: briand@infmx.UUCP (brian donat) Newsgroups: comp.lang.c++ Subject: Re: Beginner's Corner Summary: Feed Back 4 of 4 Keywords: First C++ Code. Message-ID: <4339@infmx.UUCP> Date: 26 May 90 00:24:03 GMT References: <4316@infmx.UUCP> Organization: Informix Software Inc., Menlo Park, CA. Lines: 172 // // 4 of 4 // // Finally! End of discussion is immenent! // // Nothing much is changed in main(). Afterall it's only used to // test and demonstrate. // I've just added an idiotic error test VError(). I believe I // stated that it seems rather awkward getting error info out of // a class and across header file boundaries, didn't I? // Yes, I did. // // So, I do have one or two observances at this point though. // // 1. As promised, I found that I could tinker and change // class definitions without making significant changes to // my main(). // // 2. My reorganizing of STACK and QUEUE in classes was // reflected by what seems a longer compile time overall. // Keep those '.o's around. ar 'em -> lib.xxx. // // Thanks to those of you who took part in this little exercise. ---------------------------------Cut HERE--------------------------------- /////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // \\ // testmain.c Version 2.00 \\ // tests for VLIST class : May 25, 1990, Brian L. Donat \\ // \\ // CC testmain.c stack.c queue.c vlist.c -o \\ // progname | more \\ // \\ /////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // // This program demonstrates and provides testing for the VLIST class // and its derivatives, STACK and QUEUE. // /////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ #include #include "vlist.h" #include "list.h" #include "stack.h" #include "queue.h" enum boolean { FALSE, TRUE }; enum errtype { STACKFULL, STACKEMPT, QUEUEFULL, QUEUEEMPT }; void listTest(), listScan(LIST &), stackTest(), queueTest(); int VError(int, int); main() { listTest(); stackTest(); cout << "\n\n" << flush; queueTest(); cout << "\n\n" << flush; } void listTest() { LIST intList; // Note the following does not test out // the error return global listError, but // this has been validated elsewhere as // either stackError or queueError. cout << "\nLINKED LIST TEST:\n"; intList.add(9); intList.insert(3); intList.step(); intList.insert(2); intList.backstep(); intList.remove(); intList.step(); if(intList.find(2)) intList.insert(23); else intList.tailend(); intList.insert(88); intList.insert(55); intList.add(22); intList.headend(); intList.insert(100); listScan(intList); cout << "\n\n"; } // Output the LIST, head -> tail. void listScan(LIST &intList) { intList.headend(); if(intList.getsize()) { cout << "\n" << intList.getval(); intList.step(); for(int i = intList.getsize() - 1; i > 0; --i) { cout << ", " << intList.getval(); intList.step(); } } else cout << "\nList is Empty!!\n\n"; } // Build a stack from the LIST class void stackTest(void) { STACK intStack; ELEMENT E; cout << "\nSTACK TEST:\n"; intStack.push(1); if(VError(STACKFULL, stackError)) return; intStack.push(2); if(VError(STACKFULL, stackError)) return; E = intStack.pop(); if(VError(STACKEMPT, stackError)) return; cout << "\n" << E; intStack.push(3); if(VError(STACKFULL, stackError)) return; intStack.push(4); if(VError(STACKFULL, stackError)) return; E = intStack.pop(); if(VError(STACKEMPT, stackError)) return; cout << "\n" << E; E = intStack.pop(); if(VError(STACKEMPT, stackError)) return; cout << "\n" << E; E = intStack.pop(); if(VError(STACKEMPT, stackError)) return; cout << "\n" << E; intStack.push(5); if(VError(STACKFULL, stackError)) return; E = intStack.pop(); if(VError(STACKEMPT, stackError)) return; cout << "\n" << E; E = intStack.pop(); if(VError(STACKEMPT, stackError)) return; cout << "\n" << E << "\n\n"; } // Build a queue from the LIST class void queueTest(void) { QUEUE intQueue; ELEMENT E; cout << "\nQUEUE TEST:\n"; intQueue.enqueue(1); if(VError(QUEUEFULL, queueError)) return; intQueue.enqueue(2); if(VError(QUEUEFULL, queueError)) return; E = intQueue.dequeue(); if(VError(QUEUEEMPT, queueError)) return; cout << "\n" << E; intQueue.enqueue(3); if(VError(QUEUEFULL, queueError)) return; intQueue.enqueue(4); if(VError(QUEUEFULL, queueError)) return; E = intQueue.dequeue(); if(VError(QUEUEEMPT, queueError)) return; cout << "\n" << E; E = intQueue.dequeue(); if(VError(QUEUEEMPT, queueError)) return; cout << "\n" << E; E = intQueue.dequeue(); if(VError(QUEUEEMPT, queueError)) return; cout << "\n" << E; intQueue.enqueue(5); if(VError(QUEUEFULL, queueError)) return; E = intQueue.dequeue(); if(VError(QUEUEEMPT, queueError)) return; cout << "\n" << E; E = intQueue.dequeue(); if(VError(QUEUEEMPT, queueError)) return; cout << "\n" << E << "\n\n"; } // Yes, Yes, I agree! This is a horrible way of doing things. // But for brevity, I'm keeping things simple and therefore // somewhat awkward too! int VError(int errtype, int err = listError) { if(err) switch(errtype) { case STACKFULL: cout << "\nError! Stack is Full!"; break; case STACKEMPT: cout << "\nError! Stack is Empty!"; break; case QUEUEFULL: cout << "\nError! Queue is Full!"; break; case QUEUEEMPT: cout << "\nError! Queue is Empty!"; break; default: cout << "\nError of Unkown Type!"; break; } return err; } -- infmx!briand@pyramid