求利用栈把一个10进制数转换城16进制数(c语言)的程序
来源:百度知道 编辑:UC知道 时间:2024/05/21 06:32:23
用c语言写一个十进制转十六进制的程序
用栈来实现这个算法
用栈来实现这个算法
运行示例:
输入:100000
输出:186a0
程序源码如下:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int stack[MAX];
int top = 0;
int push(int *stack, int *top, int x)
{
if (*top == MAX)
return 0;
stack[(*top)++] = x;
return 1;
}
int pop(int *stack, int *top, int *x)
{
if (*top == 0)
return 0;
*x = stack[--(*top)];
return 1;
}
int empty(int *stack, int top)
{
return top == 0;
}
int main()
{
unsigned num = 0;
scanf("%u", &num);
while (num)
{
push(stack, &top, num % 16);
num /= 16;
}
if (empty(stack, top))
push(stack, &top, 0);
while (!empty(stack, top))