diff --git a/ex3/ex3.1/Stack.cc b/ex3/ex3.1/Stack.cc new file mode 100644 index 0000000..ef7977d --- /dev/null +++ b/ex3/ex3.1/Stack.cc @@ -0,0 +1,26 @@ +//Stack.cc +#include +#include "Stack.hh" + +void Stack::push(double c) { + if (full()) { + std::cout << "Stack::push() Error: stack is full" << std::endl ; + return ; + } + s[count++] = c ; +} + +double Stack::pop() { + if (empty()) { + std::cout << "Stack::pop() Error: stack is empty" << std::endl ; + return 0 ; + } + return s[--count] ; +} + +void Stack::inspect() { + for ( int i = nitems() - 1; i >= 0; i-- ) + { + std::cout << i << ": " << s[i] << std::endl; + } +} diff --git a/ex3/ex3.1/Stack.hh b/ex3/ex3.1/Stack.hh new file mode 100644 index 0000000..3057af0 --- /dev/null +++ b/ex3/ex3.1/Stack.hh @@ -0,0 +1,27 @@ +//Stack.h + +#ifndef STACK_H +#define STACK_H + +const int LEN = 80 ; // default stack length +class Stack { + // Interface + public: + Stack() { init(); } + ~Stack() {} + int nitems() { return count ; } + bool full() { return (count==LEN) ; } + bool empty() { return (count==0) ; } + + void push( double c ); + double pop(); + void inspect(); + + // Implementation + private: + void init() { count = 0 ; } + double s[LEN] ; + int count ; + +}; +#endif diff --git a/ex3/ex3.1/main.cpp b/ex3/ex3.1/main.cpp new file mode 100644 index 0000000..f97d504 --- /dev/null +++ b/ex3/ex3.1/main.cpp @@ -0,0 +1,36 @@ +#include +#include "Stack.hh" + +/* + * Using 100 elements is too much for the default stack + * But it fails gracefully so when popping it starts from 80 + */ +using namespace std ; + +int main() { + Stack s ;// initialize Stack + + // Write doubles into Stack + int i ; + for (i=0 ; i<100 ; i++) { + cout << "pushing value " << i*i << " in stack" << endl ; + s.push(i*i) ; + } + + // Count doubles in fifo + cout << s.nitems() << " value in stack" << endl ; + + cout << "Inspect the FIFO" << endl; + s.inspect(); + + + // Read doubles back from fifo + while (!s.empty()) { + double val = s.pop() ; + cout << "popping value " << val << " from stack" << endl ; + } + + cout << "Inspect the FIFO" << endl; + s.inspect(); + return 0 ; +}