Stack.h
#ifndef _STACK_H_#define _STACK_H_namespace Stack{ class Stack_error{ private: char* error_msg; public: Stack_error(char* msg):error_msg(msg){} const char* what(){ return error_msg; } }; templateclass Stack_static_array{ private: T arr[MAXLEN]; unsigned count; public: Stack_static_array(); Stack_static_array(unsigned,T); Stack_static_array(const Stack_static_array &); bool empty() const; bool full() const; void push(const T&) throw(Stack_error); void pop() throw(Stack_error); T get_top() const throw(Stack_error); void print() const; }; template class Stack_dynamic_array{ private: T *arr; unsigned count; unsigned size; void enlarge(); void lessen(); public: Stack_dynamic_array(); Stack_dynamic_array(unsigned,T); Stack_dynamic_array(const Stack_dynamic_array &); ~Stack_dynamic_array(); bool empty() const; void push(const T&); void pop() throw(Stack_error); T get_top() const throw(Stack_error); void print() const; }; }#endif#include"Stack.cpp"
Stack.cpp
#includetemplate Stack::Stack_static_array ::Stack_static_array():count(0){}template Stack::Stack_static_array ::Stack_static_array(unsigned len,T element){ count=len; if(len){ for(unsigned i=0;i Stack::Stack_static_array ::Stack_static_array(const Stack_static_array & oth_obj){ count=oth_obj.count; for(unsigned i=0;i bool Stack::Stack_static_array ::empty() const{ return count==0;}template bool Stack::Stack_static_array ::full() const{ return count==MAXLEN;}template void Stack::Stack_static_array ::push(const T& element) throw(Stack_error){ if(count==MAXLEN) throw Stack_error("ERROR: FULL Stack!\n"); else{ arr[count]=element; ++count; }}template void Stack::Stack_static_array ::pop() throw(Stack_error){ if(!count) throw Stack_error("ERROR: EMPTY Stack!\n"); else --count;}template T Stack::Stack_static_array ::get_top() const throw(Stack_error){ if(!count) throw Stack_error("ERROR: EMPTY Stack!\n"); else return arr[count-1];}template void Stack::Stack_static_array ::print() const{ using namespace std; for(unsigned i=0;i Stack::Stack_dynamic_array ::Stack_dynamic_array():count(0),size(1){ arr=new T[size];}template Stack::Stack_dynamic_array ::Stack_dynamic_array(unsigned mcount,T element):count(mcount){ if(mcount){ if(count<1024){ size=1; while(count>size) size*=2; } else{ for(size=1024;count>size;size+=1024){} } arr=new T[size]; for(unsigned i=0;i Stack::Stack_dynamic_array ::Stack_dynamic_array(const Stack_dynamic_array & oth_obj){ count=oth_obj.count; size=oth_obj.size; arr=new T[size]; for(unsigned i=0;i Stack::Stack_dynamic_array ::~Stack_dynamic_array(){ delete [] arr;}template void Stack::Stack_dynamic_array ::enlarge(){ if(size<1024) size*=2; else size+=1024; T* temp=new T[size]; for(unsigned i=0;i void Stack::Stack_dynamic_array ::lessen(){ if(size!=1){ if(size>1024) size-=1024; else size/=2; T* temp=new T[size]; for(unsigned i=0;i bool Stack::Stack_dynamic_array ::empty() const{ return count==0;}template void Stack::Stack_dynamic_array ::push(const T& element){ if(count==size) enlarge(); arr[count]=element; ++count;}template void Stack::Stack_dynamic_array ::pop() throw(Stack_error){ if(!count) throw Stack_error("ERROR: EMPTY Stack!\n"); --count; if(count==size/2||count==size-1024) lessen();}template T Stack::Stack_dynamic_array ::get_top() const throw(Stack_error){ if(!count) throw Stack_error("ERROR: EMPTY Stack!\n"); return arr[count-1];}template void Stack::Stack_dynamic_array ::print() const{ using namespace std; for(unsigned i=0;i
有何不妥之处还望斧正