1 //Designing a sequential stack class with data elements of type int2 //requirements: When the stack operation is abnormal, the exception processing module outputs the value of the data element currently in the stack and designs a test program3#include <iostream.h>4 5 classpushonfull{6 Private:7 intvalue;8 Public:9Pushonfull (intx) {TenValue=x; One } A intValue () { - returnvalue; - } the }; - - classpoponempty{ - }; + - classseqstack{ + Private: A int*data; at intmaxstacksize; - inttop; - Public: -Seqstack (intn); -~Seqstack (); - in voidPush (Const intitem); - intPop (); to }; + -Seqstack::seqstack (intN) { thetop=0; *Maxstacksize=N; $Data=New int[n];Panax Notoginseng } - theseqstack::~Seqstack () { + Deletedata; A } the + voidSeqstack::P Ush (Const intItem) { - if( This->top== This-maxstacksize) $ ThrowPushonfull (item);//Throw Exception $ -data[top]=item; -top++; the } - Wuyi intseqstack::P op () { the if(top==-1){//here is a question, top==-1 or top==0 suitable - Throwpoponempty (); Wu } -top--; About returnData[top]; $ } - - intMain () { -Seqstack Mystack (Ten); A Try{ + for(intI=0;i< One; i++){ the Mystack.push (i); - } $}Catch(Pushonfull obj) { thecout<<"the stack is full."<<Endl; thecout<<"the element that is currently going into the stack is"<<obj. Value () <<Endl; the } the - //try{ in //for (int i=0;i<11;i++) { the //Cout<<mystack.pop () <<endl; the // } About //}catch (poponempty) { the //cout<< "Error! Stack is empty" <<endl; the // } the return 0; +}
C + + Learning (30)