private: int count; Stack_entry entry[maxstack]; }; // class
#endif
#include "Stack.hpp"
Stack :: Stack() /*Pre: None. Post: The stack is initialized to be empty.*/ { count = 0; }
bool Stack :: empty() const /*Pre: None. Post: If the Stack is empty, true is returned. Otherwise false is returned.*/ { bool outcome = true; if(count > 0) outcome = false;
return outcome; }
Error_code Stack :: push(const Stack_entry &item) /*Pre: None. Post: If the Stack is not full, item is added to the top of the Stack. If the Stack is full, an Error_code of overflow is returned and the Stack is left unchanged.*/ { Error_code outcome = success; if(count >= maxstack) outcome = overflow; else entry[count++] = item;
return outcome; }
Error_code Stack :: pop() /*Pre: None. Post: If the Stack is not empty, the top of the Stack is removed. If the Stack is empty, an Error_code of underflow is returned.*/ { Error_code outcome = success; if(count == 0) outcome = underflow; else --count;
return outcome; }
Error_code Stack :: top(Stack_entry &item) const /*Pre: None. Post: If the Stack is not empty, the top of the Stack is returned in item. If the Stack is empty an Error_code of underflow is returned.*/ { Error_code outcome = success; if(count == 0) outcome = underflow; else item = entry[count - 1];
return outcome; }
#ifndef UTMUTILITY_HPP #define UTMUTILITY_HPP
#include <iostream> // defines cout and cin #include <fstream> // file input/output #include <iomanip> // useful io manipulator functions setw etc. #include <cstdlib> // conversion routines and system() calls #include <cmath> // math functions like pwr(x,y) #include <cctype> // char functions #include <string> // string functions #include <climits> // numeric limits #include <cstddef> // C library language support #include <ctime> // date and time functions // all above from current Standard ANSI/ISO C++ using namespace std; // allows us to use the ANSI/ISO functions without // the :: notation
// function prototypes void file_check( ifstream &); //From Roux -- accepts filename and stops // program if file cannot be opened. void pause(); //From Roux -- pauses program and waits for a // key to be pressed. void clrscr(); //From Roux -- clears the command window. bool user_says_yes(); //From Kruse & Ryba -- asks for y/n answer.
////////////////////////////////////////////////////////////////// void file_check( ifstream &filename ) /// Stops program if no input file { if (!filename) { cout << "Problem opening input file." << endl; pause(); // pause function must be here exit(1);// Halt the program } }// end of file_check function
void pause() ///// Pause until a key is pressed. { system("pause"); // from cstdlib
}// end of pause()
void clrscr() //// Clear the screen { system("cls"); // from cstdlib } // end of clrscr()
bool user_says_yes() // function is from Kruse & Ryba, Appendix C { int c; bool initial_response = true; do { // Loop until appropriate input is received. if (initial_response) cout << " (y/n)?" << flush; else cout << "Respond with either y or n: " << flush; do { // Ignore white space c = cin.get(); } while (c == '\n' || c == ' ' || c == '\t' ); initial_response = false; } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N' ); return (c == 'y' || c == 'Y');