本文主要是介绍C语言实现数值转换(十进制转八进制),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原理:
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语言实现数值转换(十进制转八进制)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!