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);
}

    
    
   
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值