#include"stdio.h"
#include"stdlib.h"
#define MaxLen 1000
typedef int ElemType;
#define OK 1
#define ERROR 0
typedef int Status;
typedef struct StackNode
{
ElemType data;
struct StackNode *next;
} StackNode;
typedef struct Stack
{
StackNode *top;
}Stack;
Status InitStack(Stack *Sp)
{
Sp->top =NULL;
return OK;
}
Status ClearStack(Stack *Sp)
{
StackNode *delnode, *p=Sp->top ;
while(p!=NULL)
{
delnode = p;
p=p->next ;
free(delnode);
}
return OK;
}
Status DestoryStack(Stack *Sp)
{
return ClearStack(Sp);
}
int IsEmpty(Stack S)
{
return (S.top ==NULL);
}
Status Push(Stack *Sp,ElemType e)
{
StackNode *newnode=(StackNode *)malloc(sizeof(StackNode));
if(newnode==NULL)
return ERROR;
newnode->data =e;
newnode->next =Sp->top ;
Sp->top =newnode;
return OK;
}
Status Pop(Stack *Sp,ElemType *ep)
{
StackNode *delnode;
if(IsEmpty(*Sp))
return ERROR;
delnode =Sp->top ;
Sp->top =delnode->next ;
*ep=delnode->data ;
free(delnode);
return OK;
}
Status GetTop(Stack S,ElemType *ep)
{
if(IsEmpty(S))
return ERROR;
*ep=S.top->data ;
return OK;
}
Status Print(Stack S)
{
StackNode *p;
for(p=S.top ;p!=NULL;p=p->next )
printf("%d ",p->data );
printf("\n");
return OK;
}
void Transform(int from,char *to,int base)
{
Stack S;
int e,i=0;
InitStack(&S);
while(from)
{
Push(&S,from%base);
from/=base;
}
while(!IsEmpty(S))
{
Pop(&S,&e);
if(e<10)
to[i++]=e+'0';
else
to[i++]=e-10+'A';
}
to[i]='\0';
DestoryStack(&S);
}
int main()
{
int e,base;
char result[MaxLen];
while(scanf("%d%d",&e,&base)!=EOF)
{
Transform(e,result,base);
printf("The result is: %s\n",result);
}
}
DS||进制转换
最新推荐文章于 2020-03-07 11:06:47 发布