#include<iostream.h> class stack{ private: int count,maxsize; char *elem; public: stack(int size) { maxsize=size; count=0; elem=new char[maxsize]; } void inputstack(char &e) { if(count==maxsize) cout<<"栈已满,无法入栈"<<endl; else { count++; elem[count-1]=e; } } void outputstack(char &e) { if(count==0) cout<<"栈已为空,无法出栈"<<endl; else { e=elem[count-1]; count--; } } int length() { return count; } }; void main() { char e; int size; cout<<"请输入栈的最大容量"<<endl; cin>>size; stack stack1(size); int n; char ch; while(1) { cout<<"1表示继续,-1表示退出"<<endl; cin>>n; if(n==-1) break; else { cout<<"请输入元素"<<endl; cin>>ch; stack1.inputstack(ch); } } cout<<"栈的长度为"<<stack1.length()<<endl; cout<<"出栈顺序为"<<endl; while(1) { if(stack1.length()==0) break; else { stack1.outputstack(e); cout<<e<<" "<<' '; } } }