DS||进制转换

#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); 
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值