棧——十進位制轉八進位制
阿新 • • 發佈:2020-11-18
順序棧的基本操作
//"SqStack.h" #include<iostream> using namespace std; #define SElemType int #define MAXSIZE 100 typedef struct{ SElemType *base; SElemType *top; int stacksize; }SqStack; string InitStack(SqStack &S){ S.base = new SElemType[MAXSIZE]; S.top = S.base; S.stacksize=MAXSIZE; return "OK"; } string Push(SqStack &S,SElemType e){ if(S.top-S.base == S.stacksize) return "ERROR"; S.top++; *S.top=e; return "OK"; } string pop(SqStack &S,SElemType &e){ if(S.base == S.top) return "ERROE"; e = *S.top; S.top--; return "OK"; } SElemType GetTop(SqStack S){if(S.top != S.base){ return *(S.top-1); } } int StackEmpty(SqStack S){ if(S.top == S.base) return 1; return 0; }
#include<iostream> #include"SqStack.h" using namespace std; /* 十進位制轉八進位制 */ int main(){ SqStack S ; InitStack(S); cout <<"Input the number to conver"; int N; cin >> N; while(N){ Push(S,N%8); //將除8餘數存進棧中 N = N/8; } while (!StackEmpty(S)) { SElemType e; pop(S,e); //按棧的順序逐個輸出 cout<<e; } system("pause"); return 0; }