#include<stdio.h>
#include <malloc.h>
typedef struct
{
int * base;
int * top;
int stacksize;
}SqStack;
void InitStack(SqStack &S)
{
S.base=S.top=(int *)malloc(100*sizeof(int));
}
void Push(SqStack & S,int e)
{
*S.top=e;
S.top++;
}
void Pop(SqStack & S,int & e)
{
S.top--;
e=*S.top;
}
int StackEmpty(SqStack & S)
{
if(S.top==S.base) return 1;
else return 0;
}
void conversin()
{
int N;
int M;
SqStack S;
int e;
InitStack(S);//构造空栈
printf("输入要转化的进制(例如 2 ,8 ):");
scanf("%d",&M);
printf("输入要转化的十进制数字(例如 21 ):");
scanf("%d",&N);
int nn;
nn=N;
while(N)
{
Push(S,N%M);
N=N/M;
}
printf("%d转换成%d进制后的数字是:",nn,M);
while(!StackEmpty(S))//判断s是否为空,为空StackEmpty(S)返回1;
{
Pop(S,e);
printf("%d",e);
}
printf("\n\n\n\n");
}
void main()
{
conversin();
}
#include <malloc.h>
typedef struct
{
int * base;
int * top;
int stacksize;
}SqStack;
void InitStack(SqStack &S)
{
S.base=S.top=(int *)malloc(100*sizeof(int));
}
void Push(SqStack & S,int e)
{
*S.top=e;
S.top++;
}
void Pop(SqStack & S,int & e)
{
S.top--;
e=*S.top;
}
int StackEmpty(SqStack & S)
{
if(S.top==S.base) return 1;
else return 0;
}
void conversin()
{
int N;
int M;
SqStack S;
int e;
InitStack(S);//构造空栈
printf("输入要转化的进制(例如 2 ,8 ):");
scanf("%d",&M);
printf("输入要转化的十进制数字(例如 21 ):");
scanf("%d",&N);
int nn;
nn=N;
while(N)
{
Push(S,N%M);
N=N/M;
}
printf("%d转换成%d进制后的数字是:",nn,M);
while(!StackEmpty(S))//判断s是否为空,为空StackEmpty(S)返回1;
{
Pop(S,e);
printf("%d",e);
}
printf("\n\n\n\n");
}
void main()
{
conversin();
}