“除基取余 + 顺序栈” 实现十进制数转换成其他进制数,代码如下:
#include <stdio.h>
#define MAX_L 100
//定义栈
typedef struct
{
int data[MAX_L];
int top;
}Stack;
//进制转换
//origin是待转数,right是要转的目的数的权
void Convert(int origin, int right)
{
Stack s; //初始化栈
static char *lookup = "0123456789ABCDEF"; //用于输出
s.top = -1;
while(origin)
{
s.data[++(s.top)] = origin % right;
origin /= right;
}
while(s.top != -1)
{
putchar(lookup[s.data[s.top--]]);
}
printf("\n");
}
int main()
{
int num, r;
while(1)
{
puts("请输入要转换的数和要转换的进制权(eg:12345,2):");
scanf("%d,%d", &num, &r);
Convert(num, r);
}
scanf("%d",&r);
return 0;
}
原文地址:http://www.cnblogs.com/wmllz/p/3984038.html