#include <stdio.h>
#include <stdlib.h>
#define maxsize 100
typedef struct stack
{
int data[maxsize];//静态栈
int top;
}Stack;
void initStack(Stack* s)
{
s->top=-1;//如果初始化为-1,push函数中就先++,再赋值,如果初始化为0,就就先赋值,再++
}
int isempty(Stack* s)//判断是否空栈
{
return s->top==-1;
}
int isfull(Stack* s)//判断是否栈满
{
return s->top==maxsize-1;
}
void push(Stack* s,int e)
{
if(isfull(s))
{
printf("Stack overflow!\n");
exit(-1);
}
s->data[++(s->top)]=e;
}
int pop(Stack* s)
{
if(isempty(s))
{
printf("no elemtype!\n");
exit(-1);
}
return s->data[(s->top)--];
}
void convertdecimaltobase(int del,int r)
{
Stack s;
initStack(&s);
while(del>0)
{
push(&s,del%r);//把余的结果存入栈中,例如十进制1348,八进制为2504
del=del/r;//第一次余得4,先入栈,就后出栈
}
printf("转换的等效%d进制数:>",r);
while(!isempty(&s))
{
printf("%d",pop(&s));//依次出栈
}
printf("\n");
}
int main()
{
int decimalnumber,base;///分别是十进制数和要转=转换的位(例如八进制位)
printf("请输入十进制数:>\n");
scanf("%d",&decimalnumber);
printf("enter the base to convert:>\n");
scanf("%d",&base);
convertdecimaltobase(decimalnumber,base);
return 0;
}