原理:
N = (N div d) * d + N mod d;
源码如下:
#include
#include
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
typedef struct Stack{
int data;
struct Stack *next;
}Stack, *StackList;
StackList S;
StackList InitStack()
{
S = (StackList)malloc(sizeof(Stack));
S->next = NULL;
return S;
}
void Push(StackList S, int e)
{
StackList tmp;
tmp = (Stack *)malloc(sizeof(Stack));
tmp->data = e;
tmp->next = S->next;
S->next = tmp;
return;
}
void Pos(StackList S, int *e)
{
StackList r;
r = S->next;
if(r == NULL){
printf("it's empty stack.\n");
return;
}
*e = r->data;
S->next = r->next;
free(r);
}
int StackEmpty(StackList S)
{
if(S->next == NULL)
return TRUE;
else
return FALSE;
}
void conversion(StackList S)
{
int n, e;
S = InitStack();
printf("please input the digital:");
scanf("%d", &n);
while(n){
Push(S, n%8);
n = n / 8;
}
while(!StackEmpty(S)){
Pos(S, &e);
printf("%d ", e);
}
}
int
main(void)
{
conversion(S);
putc('\n', stdout);
}
本文介绍了一种用于将八进制数转换为十进制数的算法,并通过C语言实现了一个简单的程序来演示这个过程。程序包括初始化堆栈、入栈、出栈、判断堆栈是否为空等功能,最终实现从输入的八进制数字到十进制数字的转换。
7632

被折叠的 条评论
为什么被折叠?



