#include"stdio.h"#include"stdlib.h"#defineMAX_SIZE100#defineINCREMENT10typedefstruct{int*base;int*top;intstacksize;}Stack;voidInitStack(Stack&S){S.base=(int*)malloc(MAX_S...
#include"stdio.h"
#include"stdlib.h"
#define MAX_SIZE 100
#define INCREMENT 10
typedef struct {
int *base;
int *top;
int stacksize;
}Stack;
void InitStack(Stack &S)
{
S.base=(int *)malloc(MAX_SIZE*sizeof(int));
if(!S.base) exit(-1);
S.top=S.base;
S.stacksize=MAX_SIZE;
}
void Push(Stack &S,int e)
{
if(S.top-S.base==S.stacksize)
{
S.base=(int *)realloc(S.base,(S.stacksize+INCREMENT)*sizeof(int));
S.top=S.base+S.stacksize;
S.stacksize+=INCREMENT;
}
*S.top=e;
S.top++;
}
void Pop(Stack &S,int &e)
{
if(S.top==S.base)
;
else
{
S.top--;
e=*S.top;
}
}
bool StackEmpty(Stack S)
{
if(S.top==S.base)
return true;
return false;
}
void ZhuanHuan(int n,int d,Stack &S)
{
InitStack(S);
while(n)
{
Push(S,n%d);
n/=d;
}
}
void main()
{
int n,d,e;
Stack S;
scanf("%d%d",&n,&d);//输入要转换的数和要转换成的进制
ZhuanHuan(n,d,S);
while(!StackEmpty(S))
{
Pop(S,e);
printf("%d",e);
}
putchar('\n');
}
展开