• 通过例子进阶学习C++(八)进制转换


    本文是通过例子学习C++的第八篇,通过这个例子可以掌握进制之间的转换。

    1.问题描述

    在日常生活中,使用十进制进行数学运算,每个数位上的数字只能是0~9,不同位上对应的数值的权重是不同的,如数字123,个位的3代表3,十位上的数字2代表20,百位上的1代表100。

    在计算机中使用二进制,每个数位上的数字只能是0~1,不同位上的数字代表的权重不同,如数字(1011)2,其中中各位1代表十进制的1,十位1代表2,千位上的1代表8,依此类推……

    以此类推,常用的还有8进制,16进制;

    其中16进制,每个数位上的数字只能是0~7,如(111)8。个位上的1表示1,十位上的1表示8,千位上的1表示64,依此类推……

    其中16进制,每个数位上的数字只能是0~9,A、B、C、D、E、F,如(111)16。个位上的1表示1,十位上的1表示16,千位上的1表示256,依此类推……

    本文将介绍各种数值之间的转换。

    2.二进制和十进制之间的转换

    二进制转十进制,方法是按权展开。

    #include<iostream>
    #include<string.h>
    using namespace std;
    
    int main(){
    	cout<<"请输入一个二进制数:";
    	char s[100];
    	long num=0;
    	long index = 1;
    	gets(s);
    	for(int t=strlen(s)-1;t>=0;t--){
    		//cout<<s[t]<<"--";
    		num += (s[t]-'0') * index;
    		index = index * 2;
    	}
    	
    	cout<<num;
    	return 0;
    }
    

    程序运行效果如下:

    十进制转二进制,方法是除2取余,倒过来即可。

    #include<iostream>
    #include<stack>
    using namespace std;
    
    int main(){
    	stack<int> s;
    	int n;
    	cout<<"请输入一个10进制数:"; 
    	cin>>n;
    	while(n){
    		s.push(n%2);
    		n /= 2;
    	}
    	while(! s.empty()){
    		cout<<s.top();
    		s.pop();
    	}
    	return 0;
    }
    

    程序运行效果如下:

    3.十进制和十六进制的转换

    下面以十进制转十六进制的两种实现方法,和十进制转二进制类似,采用除16取余,倒过来即可。

    3.1 通过stack和if语句实现

    #include<iostream>
    #include<stack>
    using namespace std;
    int main(){
    	stack<int>s;
    	int n;
    	cin>>n;
    	while(n>0)
    	{
    		s.push(n%16);
    		n /= 16;
    	}   
    	while(!s.empty()){
    		//cout<<s.top();
    		if(s.top()==10) cout<<'A';
    		else if(s.top()==11) cout<<'B';
    		else if(s.top()==12) cout<<'C';
    		else if(s.top()==13) cout<<'D';
    		else if(s.top()==14) cout<<'E';
    		else if(s.top()==15) cout<<'F';
    		else cout<<s.top();
    		s.pop();
    	}
    	
    	return 0;
    }
    

    3.2 通过stack和switch语句实现

    #include<iostream>
    #include<stack>
    using namespace std;
    int main(){
    	stack<int>s;
    	int n;
    	cout<<"请输入一个十进制数:";
    	cin>>n;
    	while(n>0)
    	{
    		s.push(n%16);
    		n /= 16;
    	}   
    	while(!s.empty()){
    		//cout<<s.top();
    		switch(s.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;
    			case 15:
    				cout<<'F';
    				break;
    			default:
    				cout<<s.top();
    		}
    		s.pop();
    	}
    	
    	return 0;
    }
    

    程序运行效果如下:

    4.总结

    本着Talk is cheap. Show me the code原则,代码实现不做过多解释。

    本文从构思到完成,可谓是耗费了大量的心血。

    如果您阅读本文后哪怕有一丢丢收获,请不要吝啬你手中关注点赞的权力,谢谢!

    另外,如果需要相关代码,请留言,可以提供完整源代码

  • 相关阅读:
    JS精度问题(0.1+0.2 = 0.3吗?)
    力导向算法的研究与改进
    React Hooks的memo和useCallback
    React Hooks vs Vue Composition Api
    docker常用命令
    win10一台电脑上配置多个git账户
    eslint+prettier 统一代码风格
    c#中关于值类型,引用类型在栈,堆栈的分配
    js里的__proto__和prototype
    golang之冒泡排序
  • 原文地址:https://www.cnblogs.com/siweihz/p/15898004.html
Copyright © 2020-2023  润新知