利用顺序栈实现非负十进制数转换为其他进制数(数制转换)

Conversion函数思路:

  1. 传入参数:要转换的数n(非负),要转换成的基数base
  2. 方法:根据除基取余的转换方式,将余数压入栈中,然后余数出栈,同时根据ascii编码转换为字符输出

代码:

#include<iostream>
using namespace std;
const int maxsize=100;
//顺序栈的类型定义
typedef int datatype;
typedef struct{
	datatype data[maxsize];
	int Top;
}SeqStack;

//函数原型的声明
void InitStack(SeqStack*& S);		//栈的初始化函数
int Empty(SeqStack* S);				//判断空栈函数
int Push(SeqStack* S,datatype e);	//入栈函数
int Pop(SeqStack* S,datatype& e);	//出栈函数
void Conversion(int,int);
//主函数
int main(){
	int p,q;
	cout<<"Please enter a decimal number:"<<endl;
	cin>>p;
	cout<<"Please enter cardinality:"<<endl;
	cin>>q;
	cout<<"Result of conversion:"<<endl;
	Conversion(p,q);
	return 0;
}
//函数的定义
void InitStack(SeqStack*& S)
{
	S=(SeqStack*)malloc(sizeof(SeqStack));
	S->Top=0;
}
int Push(SeqStack* S,datatype e){
	if(S->Top>=maxsize-1){
		cout<<"栈上溢!"<<endl;
		return 0;
	}
	else{
		S->data[++S->Top]=e;
		return 1;
	}
}
int Pop(SeqStack* S,datatype& e){
	if(Empty(S)){
		cout<<"栈下溢!"<<endl;
		return 0;
	}
	else{
		e=S->data[S->Top--];
		return 1;
	}
}
int Empty(SeqStack* S)
{
	if(S->Top<=0)return 1;
	else
		return 0;
}
void Conversion(int n,int base){
	datatype e;
	SeqStack* S;
	InitStack(S);
	while(n!=0){
		Push(S,n%base);
		n=n/base;
	}
	while(!Empty(S)){
		Pop(S,e);
		if(e>9)printf("%c",e+55);	//超过9进制,余数会大于9时,字符映射到大写字母
		else printf("%c",e+48);		//未超过9进制,余数不大于9时,映射到1-9
	}
	cout<<endl;
}

示例:10转换为16进制的A
在这里插入图片描述

200转换为16进制的C8
在这里插入图片描述

  • 4
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用实现数制转换的完整程序: ```c++ #include <iostream> #include <stack> #include <fstream> using namespace std; // 十进制转二进制 void decimalToBinary(int decimal, stack<int>& binaryStack) { while (decimal != 0) { binaryStack.push(decimal % 2); decimal /= 2; } } // 十进制转八进制 void decimalToOctal(int decimal, stack<int>& octalStack) { while (decimal != 0) { octalStack.push(decimal % 8); decimal /= 8; } } // 十进制转十六进制 void decimalToHexadecimal(int decimal, stack<int>& hexadecimalStack) { while (decimal != 0) { hexadecimalStack.push(decimal % 16); decimal /= 16; } } int main() { int decimal; cout << "请输入一个非负十进制:"; cin >> decimal; stack<int> binaryStack, octalStack, hexadecimalStack; decimalToBinary(decimal, binaryStack); decimalToOctal(decimal, octalStack); decimalToHexadecimal(decimal, hexadecimalStack); ofstream outFile("conversion.txt"); if (!outFile) { cerr << "无法打开文件!" << endl; exit(1); } // 输出二进制 outFile << "二进制:"; while (!binaryStack.empty()) { outFile << binaryStack.top(); binaryStack.pop(); } outFile << endl; // 输出八进制 outFile << "八进制:"; while (!octalStack.empty()) { outFile << octalStack.top(); octalStack.pop(); } outFile << endl; // 输出十六进制 outFile << "十六进制:"; while (!hexadecimalStack.empty()) { int remainder = hexadecimalStack.top(); if (remainder < 10) outFile << remainder; else outFile << (char)('A' + remainder - 10); hexadecimalStack.pop(); } outFile << endl; outFile.close(); cout << "转换成功,结果已保存到 conversion.txt 文件中。" << endl; return 0; } ``` 程序中定义了三个函,用于将十进制转换为二进制、八进制和十六进制。这三个函利用实现转换过程,具体实现方式是不断将余,直到商为0。最后,再从中取出各位字,转换后的。 程序运行后,首先要求用户输入一个非负十进制。然后,程序调用上述三个函,将结果存放到对应的中。最后,程序将转换结果输出到文件 conversion.txt 中。输出时,需要注意将十六进制中大于等于10的余转换为对应的字母。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值