匿名用户
1级
2013-12-05 回答
#include
#include
#include
typedef unsigned int data_type; //结点数62616964757a686964616fe78988e69d8331333332643835据域数据类型
typedef struct snode //结点类型定义
{
data_type bit;
struct snode *next;
} snode;
typedef struct stk //栈类型定义
{
snode *top;
size_t cnt;
} stk;
stk *init(void) //栈初始化
{
return calloc(1,sizeof(stk));
}
data_type pop(stk *a) //出栈
{
data_type top_bit=a->top->bit;
snode *top=a->top;
a->top=a->top->next;
a->cnt--;
free(top);
return top_bit;
}
data_type top(stk *a)
{
return a->top->bit;
}
int push(data_type np,stk *a) //压栈
{
snode *topush=malloc(sizeof(snode));
topush->bit=np;
topush->next=a->top;
a->top=topush;
return ++(a->cnt);
}
void prt(stk *a) //输出进制转换后的结果
{
while (a->cnt)
if (top(a)<10)
printf("%u",pop(a));
else printf("%c",'A'+pop(a)-10);
}
void chg(data_type dec,unsigned int tob) //将无符号十进制整数dec转换为tob进制,并输出转换结果
{
stk *stack=init();
while (dec)
{
push(dec%tob,stack);
dec/=tob;
}
prt(stack);
free(stack);
}
int main(int argc, char* argv[])
{
data_type dec;
unsigned int tob;
scanf("%u",&dec);//输入原始无符号十进制整数
scanf("%u",&tob);//输入要转换到的进制,例如2或者8
chg(dec,tob);//输出转换结果
return 0;
}