实现堆栈输入输出,十进制转八进制和十六进制

本文介绍了使用堆栈实现十进制数向八进制及十六进制转换的方法,并提供了完整的C++源代码示例。通过不断除以目标进制并记录余数的方式完成转换,同时展示了如何处理十六进制的特殊字符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现堆栈输入输出,十进制转八进制和十六进制


1.入栈

代码如下(示例):

void intput(ElemType a[], int& top,ElemType item)
{
	if (top == (N - 1))//判断栈满
	{
		cout << "堆栈满" << endl;
		return;
	}
	top++;
	a[top]=item;

}

2.出栈

代码如下(示例):

void output(ElemType a[], int& top, ElemType& item)
{
	if (top == (-1))//判断栈空
	{
		cout << "堆栈空" << endl;
		return;
	}
	item = a[top];
	top--;
}

3.十进制转八进制

100/8=12…4
12 / 8=1…4
1 / 8=0…1
100的八进制为144
只需要把每次的余数保留就是得到的结果,一直除到商为0

void DecimaltoOctal(int num)
{
	int a[N];
	int top=-1;
	do {
		top++;
		a[top] = num % 8;
		num = num / 8;
	} while (num != 0);
	cout << "八进制:";
	do {
		cout <<  a[top--];
	} while (top!=-1);
}

4.八进制转十六进制

八进制转十六进制 与 十进制转八进制 同理
只需要把每次的余数保留就是得到的结果,一直除到商为0
但在输出的时候十六进制:
A=10 B=11 … F=15 稍微改动即可

void DecimaltoHexadecimal(int num)
{
	int a[N];
	int top = -1;
	do {
		top++;
		a[top]=num % 16;
		num = num / 16;
	} while (num != 0);
	cout << "十六进制:";
	do {
		if (a[top] > 9)
		{
			switch (a[top]) {
			case 10:
				cout << 'A'; break;
			case 11:
				cout << 'B';  break;
			case 12:
				cout << 'C';  break;
			case 13:
				cout << 'D'; break;
			case 14:
				cout << 'E';  break;
			default:
				cout << 'F';  break;
			}
		}
		else
		{
			cout << a[top];
		}
		top--;
	} while (top != -1);
}

5.附上源码

#include<iostream>
using namespace std;
#define N 100
typedef int ElemType;
void intput(ElemType a[], int& top,ElemType item)
{
	if (top == (N - 1))//判断栈满
	{
		cout << "堆栈满" << endl;
		return;
	}
	top++;
	a[top]=item;

}
void output(ElemType a[], int& top, ElemType& item)
{
	if (top == (-1))//判断栈空
	{
		cout << "堆栈空" << endl;
		return;
	}
	item = a[top];
	top--;
}
void DecimaltoOctal(int num)
{
	int a[N];
	int top=-1;
	do {
		top++;
		a[top] = num % 8;
		num = num / 8;
	} while (num != 0);
	cout << "八进制:";
	do {
		cout <<  a[top--];
	} while (top!=-1);
}
void DecimaltoHexadecimal(int num)
{
	int a[N];
	int top = -1;
	do {
		top++;
		a[top]=num % 16;
		num = num / 16;
	} while (num != 0);
	cout << "十六进制:";
	do {
		if (a[top] > 9)
		{
			switch (a[top]) {
			case 10:
				cout << 'A'; break;
			case 11:
				cout << 'B';  break;
			case 12:
				cout << 'C';  break;
			case 13:
				cout << 'D'; break;
			case 14:
				cout << 'E';  break;
			default:
				cout << 'F';  break;
			}
		}
		else
		{
			cout << a[top];
		}
		top--;
	} while (top != -1);
}
int main()
{
	ElemType a[N];
	int top = -1;
	ElemType item;

	DecimaltoOctal(100);
	DecimaltoHexadecimal(100);

	cout << ' ' << endl;
	cout<<"输入需进栈数量:";
	int n;
	cin >> n;
	for (int i = 0;i < n;i++)
	{
		cin >> item;
		intput(a, top, item);
	}

	cout << "输入需退栈数量:";
	cin >> n;
	for (int i = 0;i < n;i++)
	{
		output(a, top, item);
		cout<<item<<" ";
	}
	return 0;
}

6.运行结果

八进制:144十六进制:64
输入需进栈数量:3
1 2 3
输入需退栈数量:3
3 2 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值