使用栈把十进制数转换成任意进制

#include <stdio.h>
#include <stdlib.h>
#define STACKSIZE 20
typedef struct sqlist{ 
    int *base;
    int *top;
    int stacksize;
}SqStack;

//创建栈 
void InitStack(SqStack &s){
	s.base = new int[STACKSIZE];
	if (!s.base){
		exit(0);
	}
	s.top = s.base;
	s.stacksize = STACKSIZE;
}

//判空
int StackEmpty(SqStack &s){
	if (s.base == s.top){
		return 1;
	} else {
		return 0;
	}
} 

//压入栈 
void Push(SqStack &s, int n){
	if (s.top - s.base == s.stacksize){
		printf("栈满"); 
		exit(0) ;
	}
	*s.top ++ = n;
} 

void Pop(SqStack &s, int &e){
	if (s.top == s.base){
		printf("栈空"); 
		exit(0) ;
	}
	e = *--s.top; 
}

//进制的转换 
void jinZhi(SqStack &S, int num, int k){
	int x, e;
	char ch[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
	do{
		x = num % k;
		num /= k;
		Push(S, x);
	}while(num);
	printf("转换后的%d进制的数值为:", k);
	while(StackEmpty(S) != 1){
		Pop(S, e);
		printf("%c",ch[e]);
	} 
}
int main(){
    SqStack S;
	InitStack(S);
	jinZhi(S, 1500, 16);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值