Decree (SQSTACK, C ++ version)

xiaoxiao2021-03-06  111

/ * stack.h * / # iFNDef __sqstack_h __ # define __sqstack_h__ 1

#include

Extern "C" {void exit (int);}

const Int ndefaultstacksize = 50; // The number of STACK elements

/ / The following sequential storage structure (sequential stack) C class definition template // is declared as template class class stack {private: t * stacklist; / / Pointer INT stacksize; // Store Stack (array) size int top; / / Indicates the position of the top element (array subscript) PUBLIC: // Constructor Stack (INT INITSIZE = NDEFAULTSTACKSIZE) {IF (INITSIZE <1) INITSIZE = NDEFAULTSTACKSIZE; StackList = New T INITSIZE]; / / The STACKLIST assigns storage space if (! stacklist) {CERR << "" The STACK assigned storage space failed! The program will terminate.

"<< endl; exit (1);} stacksize = INITSIZE; // Initialization stack size TOP = -1; // Initialization Stack top position} // Destructure function ~ stack () {if (stacklist) delete [] stacklist Stacksize = 0; TOP = -1;} // Decision stack is empty int Stackempty () const {return top <0; // TOP == -1;} // Judgment Whether stack full stckfull () const { Return Top == Stacksize - 1;} // Return to the element in the stack int Stocklength () const {return Top 1;} // Stack Void Push (const t & item) {if (TOP> = stacksize - 1) {CERR << "The stack is full, and it is impossible to continue the stack operation! "<< Endl; Return;} TOP ; // Modify the top position (subscript) stacklist [TOP] = item; // Type T requires support = operator} // out of the stack T POP () {t temp ; If (TOP> = 0) {TEMP = stacklist [TOP]; // Transfer Stack Top Element Value Top -; // Modify Stack Top Location} Else Cerr << "The stack is empty and cannot continue from the stack operation ! "<< endl; returnization;} // read the top element T peek () const {t returnvalue; if (top> = 0) ReturnValue = stacklist [top]; else cerr <<" stack is empty, unread Take the top element! "<< Endl; Return ReturnValue;} // Clear Stack Void ClearsTack () {TOP = -1; // Top Location Reset}}

// convert the decimal number NUM into TOSYS (2-36) enrollment value (deposit Destnum in a string and return) char * decimalconversion (unsigned long Num, unsigned char TOSYS, CHAR DESTNUM []) {IF ( ! destnum) Return NULL; Stack s (80); unsigned char i = 0; int item; if (Tosys <2 || Tosys> 36) {DESTNUM [0] = '/ 0'; return destnum;} While (NUM) {s.push (NUM% TOSYS); Num = Num / Tosys;} while (! s.stackephone ()) {item = S.POP (); destnum [i] = (unsigned char) (item ) (Item <10? '0': ('a'-10)); i ;} destnum [i] =' / 0 '; return destnum;} # Endif / *! __ SQSTACK_H__ * /

// stacktest.cpp # incrude #include "stack.h"

Void main () {stack S1 (10), S2 (10); char * weekday [] = {"MON", "Tue", "WED", "THR", "Fri", "SAT" , "Sun"}; for (Register INT i = 0; i <7; i ) s1.push (weekday [i]); cout << =========== S1 ===== ===== "<< endl; for (i = 0; i <7; i ) {cout << SETW (5) << S1.PEEK (); s2.push (s1.pop ());} COUT << Endl; cout << =========== S2 =========== << endl; for (i = 0; i <7; i ) cout << SETW (5) << S2.POP (); cout << endl;

Char s [80]; cout << Decimalconversion (1024, 2, s) << endl;}

转载请注明原文地址:https://www.9cbs.com/read-101699.html

New Post(0)