Once again I am having a problem. I am trying to do varoius operations with stacks. It doesn't look like it is filling the stack. I only tried to print the stack and print the second value so far. I saw that they were not working so I stopped even though I have written the other functions. It also sounds like I have a loop in the print2nd function. Any advice???
Code:#include <iostream> #include <cstdlib> #include "stackType.h" #include<fstream> //function prototypes void PrintStack(stack& s); void Print2nd(stack& s); void PrintBottom(stack& s ); void Palindrome(stack& s ); // declaring file stream variables ifstream inData; ofstream outData; int main() { //declaration of variables el_t element; stack original; const int SENTINEL = -999; int c = 0; //fills stack with at least 25 elements from an input file while((!original.StackIsFull() && element != SENTINEL)) { inData>>element; original.Push(element); } //Opening of files inData.open("a:/stackinfo.txt"); outData.open("a:/stackoutput.txt"); //Operations on Functions or Function Calls outData<<"copy of stack is printed and original stack is unchanged"<<endl; PrintStack(original); outData<<"print 2nd element of stack"<<endl; Print2nd(original); //Close files inData.close(); outData.close(); // system("PAUSE"); return 0; } // Function to print all elements in a stack and leave stack unchanged // after printing //Pre: The stack is non empty //Post: void PrintStack(stack& s) { el_t e; stack temp; while (!s.StackIsEmpty()) { s.Pop(e); temp.Push(e); outData<<e; } while(!temp.StackIsEmpty()) { temp.Pop(e); s.Push(e); } } //Function prints 2nd element from the stack and leaves stack unchanged void Print2nd(stack& s) { el_t e1, e2; while(!s.StackIsEmpty()) { s.Pop(e1); s.Pop(e2); outData<<e2; s.Push(e2); s.Push(e1); } } //Function prints bottom of stack and leaves stack empty void PrintBottom(stack& s ) { el_t e; stack temp; while(!s.StackIsEmpty()) { s.Pop(e); temp.Push(e); } cout<<e; } //Function determines whether the characters form a palindrome void Palindrome(stack& s ) { stack u,u2; el_t ch, ch1; bool equal = true; while(!s.StackIsEmpty()) { s.Pop(ch); u.Push(ch); u2.Push(ch); } while(!u.StackIsEmpty()) { u.Pop(ch); s.Push(ch); } while(!u2.StackIsEmpty() && equal) { u2.Pop(ch); s.Pop(ch1); if (ch != ch1) equal = false; } if (equal) cout<<"Palindrome"<<endl; else cout<<"Not a Palindrome"<<endl; } //end of Palindrome Function // File: stackType.h Class, constant values, and type definitions for c++ static // implementation of ADT stack. This is the specification file for the class stack #ifndef STACK_H #define STACK_H // application dependent size of stack const unsigned int MAX_NUM_ELS = 256; typedef char el_t; // type varies with application typedef int index_t; class stack { public: stack(); //stack constructor ~stack(); //stack deconstructor bool StackIsEmpty(void); //Is stack empty? bool StackIsFull(void); //Is stack full? void Push(el_t); //push item void Pop(el_t&); //pop to item private: void StackError(char*); //error handler index_t top; //top index: -1 if empty el_t el[MAX_NUM_ELS]; //container for elements }; #endif // File: stackao.cpp Contents: C++ source code for static // implementation of ADT stack. This is the implementation file. // #include<iostream> //#include<stdlib.h> #include "stackType.h" using namespace std; //Default constructor //Pre: None //Post: The stack object is initialized to be empty // stack::stack(void) { top = -1; } //Stack destructor //Pre: The stack has gone out of scope or an explicit call // to ~stack has occurred. //Post: The stack is empty. stack::~stack() { top = -1; } //Boolean function that returns TRUE if the stack is empty //Pre: The stack has been initialized. //Post: The function returns TRUE if the stack is empty, // FALSE otherwise. bool stack::StackIsEmpty(void) { return (top == -1 ? true : false); } //Boolean function that returns TRUE if a stack is full //Pre: The sack has been initialized. //Post: The function has returned TRUE if the stack is full, // FALSE otherwise. bool stack::StackIsFull(void) { return (top == (MAX_NUM_ELS -1) ? true : false); } //Function to push an element onto a non_full stack //Pre: e is a data element of typr el_t. // The stack is not full. //Post: If the stack was not full, e was placed on top of // the stack; otherwise StackError was called. void stack::Push(el_t e) { if ( StackIsFull()) StackError("Stack is fulle\n"); else { ++top; el[top] = e; } } // Function to pop an element from a nonempty stack and // store it in the refernced variable // Pre: The stack is nonempty. // Post: The top stack element has been removed and stored // in e. If the stack was empty, StackError was called. void stack::Pop(el_t& e) { if (StackIsEmpty()) StackError(" Stack is empty \n"); else { e = el[top]; --top; } } // Function to print an error message and abort the program //Pre: An unrecoverable error occurred. // ErrorMessage is an error message. //Post: ErrorMessage has been output and the program aborted. void stack::StackError( char* ErrorMessage) { cerr<< ErrorMessage; exit(1); }



LinkBack URL
About LinkBacks


