栈——十进制转八进制
顺序栈的基本操作
//"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=e;
S.top++;
return "OK";
}
string pop(SqStack &S,SElemType &e){
if(S.base == S.top) return "ERROE";
S.top--;
e = *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; }