用堆栈实现由十进制数向其他进制的转换

#include"Stack.h"
#include<stdio.h>
#include"TryStack.h"
#include<stdlib.h>
#define _CRT_SECURE_NO_DEPRECATE

#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1

void convert(Stack &s, int N,int n)
{
	InitStack(&s);
	do
	{
		Push(&s, N%n);
		N /= n;
	} while (N!=0);
	int i, e;
	while (!IsEmpty(&s))
	{
		e = Pop(&s);
		if (e > 9)//十六进制时输出字母
		{
			e = e + 55;
			printf("%c", e);
		}
		else
		{
			printf("%d", e);
		}


	}
	printf("\n");
}

int main()
{
	Stack s;
	InitStack(&s);
	/*for (int i = 1; i < 20; i += 2)
		Push(&s, i);
	Print(&s);

	printf("栈顶的元素为:\n");
	int k = Top(&s);
	printf("%d\n", k);
	Clear(&s);
	if (IsEmpty(&s))
		printf("栈为空\n");
	else
		printf("栈不为空\n");
	*/
    unsigned n, N;//要转换的进制数和要转换的数
	printf("输入要转换的十进制数和要转换为的进制数:\n");
	scanf("%d,%d", &N, &n);
	printf("%d转换为%d进制后为:\n", N, n);
	convert(s, N, n);

	system("pause");
	return 0;
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值