#include#include #include #include "study.h"//里面写的#define DAY1.... #ifdef DAY6 //课后题3,栈, #ifndef Hfile //头文件部分 typedef unsigned long unlong; using namespace std; class stack { private: enum{ MAX = 10 }; unlong* item; int size; int top; public: stack(int n = MAX); //防止构造时生成默认的,默认的会直接复制对象,里面指针还是没变 stack(const stack& st); ~stack(); bool isempty()const; bool isfull()const; bool push(const unlong & data); bool pop(unlong& data); void show(); //防止直接相等 stack & operator=(const stack& st); }; #endif stack::stack(int n) { if (n > 10)n = 10; size = 0; item = new unlong[n]; top = n; std::cout << "created a stack Maxsize" << top << endl; } stack::stack(const stack& st) { top = st.top; size = st.size; item = new unlong[top]; memcpy(item, st.item, sizeof(unlong) * st.top); } stack::~stack() { delete [] item; } //1是空的,0是非空 bool stack::isempty()const { //for (int i = 0; i < top; i++) //{ // if (item[i] != 0)return 0; //} if (size == 0)return 1; return 0; } //1是满的,0不满 bool stack::isfull()const { //for (int i = 0; i < top; i++) //{ // if (item[i] == 0)return 0; //} if (size == top)return 1; else return 0; } //1是成功,0是满的的推不入 bool stack::push(const unlong& data) { if (isfull()) { std::cout << "full and can not to push" << endl; return 0; } item[size]=data; size++; std::cout << "push ;" << data << endl; } //1是成功,0是空的,推不出 bool stack::pop(unlong& data) { if (isempty()) { std::cout << "empty and can not to pop" << endl; return 0; } data=item[size-1]; item[size - 1] = 0; size--; std::cout<<"pop ;"<< data<



