输入一个十进制正整数,再输入要转换到的进制。
输出结果。
使用链栈
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef int datatype;
typedef struct LinkStack
{
datatype data;
struct LinkStack* next;
}LinkStack, *pLinkStack;
void initLinkStack(pLinkStack* p_link_stack)
{
*p_link_stack = NULL;
}
bool isEmptyLinkStack(pLinkStack p_link_stack)
{
if (!p_link_stack)
return true;
return false;
}
bool pushLinkStack(pLinkStack* p_link_stack, datatype push_data)
{
pLinkStack p_new = calloc(1, sizeof(LinkStack));
if (!p_new)
return false;
p_new->data = push_data;
p_new->next = *p_link_stack;
*p_link_stack = p_new;
return true;
}
bool popLinkStack(pLinkStack* p_link_stack, datatype* pop_data)
{
if (isEmptyLinkStack(*p_link_stack))
return false;
*pop_data = (*p_link_stack)->data;
pLinkStack p_temp = *p_link_stack;
*p_link_stack = (*p_link_stack)->next;
free(p_temp);
p_temp = NULL;
return true;
}
void conversion(int number1, int number2)
{
pLinkStack p_link_stack = NULL;
initLinkStack(&p_link_stack);
while (number1)
{
if (!pushLinkStack(&p_link_stack, number1 % number2))
{
printf("Can NOT Allocate Memory\n");
return;
}
number1 = number1 / number2;
}
datatype temp = 0;
while (popLinkStack(&p_link_stack, &temp))
{
printf("%d", temp);
}
puts("");
}
int main()
{
int number1, number2;
printf("Please Input a Decimal Integer:\n");
scanf("%d", &number1);
printf("Please Input a System Number:\n");
scanf("%d", &number2);
printf("The Result is:\n");
conversion(number1, number2);
return 0;
}
方法解决。