十进制转换成八进制
c++ 实现代码
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define OK 1
#define STACKINITSIZE 100
#define STACKCREMENT 10
#define OVERFLOW -2
#define ERROR 0
typedef int sElemType;
typedef int Status ;
typedef struct {
int *base;
int *top;
int stacksize;
}SqStack;
Status initstack(SqStack &s);
Status push(SqStack &s,int e );
Status pop(SqStack &s,int &e);
void conversion(SqStack &s);
Status initstack(SqStack &s)
{
s.base=( sElemType *)malloc( STACKCREMENT* sizeof(sElemType));
if(!s.base)exit ( OVERFLOW );
s.top=s.base;
s.stacksize= STACKINITSIZE;
return OK;
}
Status push(SqStack &s,int e ){
if(s.top-s.base>=s.stacksize){
s.base=(sElemType *)realloc(s.base,(s.stacksize+STACKINITSIZE)*sizeof(sElemType ));
if(!s.base)exit(OVERFLOW);
s.top=s.base+s.stacksize;
s.stacksize+=STACKCREMENT;
}
*s.top++=e;
return OK;
}
Status pop(SqStack &s,int &e){
if(s.top==s.base)return ERROR;
e=*--s.top;
return OK;
}
void conversion(){
int n;
int e;
SqStack s;
initstack(s);
printf("请输入10进制数:\n");
scanf("%d",&n);
while(n){
push(s,n%8);
n=n/8;
}
while(s.top!=s.base){
pop(s,e);
printf("%d",e);
}
}
void main(){
int e;
SqStack s;
initstack(s);
conversion();
}