#include <stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define STACK_INIT_SIZE 100;
#define STACKINCREMENT 10;
#define OK 1;
#define ERROR 0;
typedef int Status;
//顺序栈的定义
typedef struct{
int *base;
int *top;
int stacksize;
}SqStack;
SqStack S;
//构建一个空栈
Status InitStack(SqStack &S){
S.base = (int *)malloc(STACK_INIT_SIZE * sizeof(int));
if(!S.base)
exit(1);
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return OK;
}
//判断是否为空栈
bool StackEmpty(SqStack S){
if(S.base == S.top)
return true;
else
return false;
}
//元素e进栈
Status Push(SqStack S, int e){
if(S.top-S.base >= S.stacksize){//栈满追加存储空间
S.base = (int*)realloc(S.base,(S.stacksize+STACKINCREMENT) * sizeof(int));
if(!S.base)
return ERROR;
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*S.top++ = e;
return OK;
}
//元素e出栈
int Pop(SqStack &S){
int e;
if(S.top == S.base)
return ERROR;
e = *--S.top;
return e;
}
//进制转化
void conversion(int N,int d){
int e;
InitStack(S);
while(N){
Push(S,N%8);
N /= d;
}
while(!StackEmpty(S)){
e = Pop(S);
printf("%d",e);
}
}
int main(){
int N,d;
printf("请输入要转换的数\n");
scanf("%d",&N);
printf("请输入要转换的进制\n");
scanf("%d",&d);
conversion(N,d);
return 0;
}
#include<stdlib.h>
#include<malloc.h>
#define STACK_INIT_SIZE 100;
#define STACKINCREMENT 10;
#define OK 1;
#define ERROR 0;
typedef int Status;
//顺序栈的定义
typedef struct{
int *base;
int *top;
int stacksize;
}SqStack;
SqStack S;
//构建一个空栈
Status InitStack(SqStack &S){
S.base = (int *)malloc(STACK_INIT_SIZE * sizeof(int));
if(!S.base)
exit(1);
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return OK;
}
//判断是否为空栈
bool StackEmpty(SqStack S){
if(S.base == S.top)
return true;
else
return false;
}
//元素e进栈
Status Push(SqStack S, int e){
if(S.top-S.base >= S.stacksize){//栈满追加存储空间
S.base = (int*)realloc(S.base,(S.stacksize+STACKINCREMENT) * sizeof(int));
if(!S.base)
return ERROR;
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*S.top++ = e;
return OK;
}
//元素e出栈
int Pop(SqStack &S){
int e;
if(S.top == S.base)
return ERROR;
e = *--S.top;
return e;
}
//进制转化
void conversion(int N,int d){
int e;
InitStack(S);
while(N){
Push(S,N%8);
N /= d;
}
while(!StackEmpty(S)){
e = Pop(S);
printf("%d",e);
}
}
int main(){
int N,d;
printf("请输入要转换的数\n");
scanf("%d",&N);
printf("请输入要转换的进制\n");
scanf("%d",&d);
conversion(N,d);
return 0;
}