大家好啊,今天為大家帶來的是自己實現的用C++編寫的簡單進制轉換器,用於10進制數和8進制數,16進制數,2進制數的相互轉換.
首先,說明一下什麼是進制.n進制就是一種用來表示數值的方法,n進制,顧名思義,逢n進1.我們日常生活中使用的基本都是10進制數,逢10進1;現代計算機處理器所能處理的只能是2進制數,雖然好像前蘇聯曾經嘗試研制10進制計算機,最後當然無疾而終.
計算機使用2進制的原因是它實現簡單,僅有0和1兩個碼元,又和自然世界某些事物的兩種狀態相對應(比如開關的開和斷開,電平的高和低等).在我們記錄和匯編語言的編寫中,常常使用16進制數,便於書寫,因為一個16進制數對應4個2進制數.一個8進制數對應3個2進制數,而一個10進制數對應4個2進制數,存在6個冗余碼.在高級程序設計語言中,2進制,8進制,16進制分別用0b,0,0x作為前綴表示,如0xB2代表10進制數178.在匯編語言中,用B,O,H作為作為後綴表示,如30H代表十進制數48,同時也是字符'0'的ASCII碼.
實現該小程序的關鍵是數制轉換算法.無符號整數從任意進制到10進制,按權展開即可;從十進制到任意進制,采用短除法取余數直至商為零.
短除法示例,十進制數53對應二進制數0b110101
下面進入正題,整個工程由自定義頭文件convertfuncs.h和源程序文件main.cpp構成,代碼如下:
convertfuncs.h:
1 #ifndef CONVERTFUNCS_H_INCLUDED
2 #define CONVERTFUNCS_H_INCLUDED //包含警戒
3
4 #include<iostream>
5
6 using namespace std;
7
8 const unsigned limit = (unsigned)(sizeof(unsigned)*8*0.3+1); //限定可處理的10進制數最多位數,由機器實現決定
9
10 string deciToHex(unsigned deci) { //10進制轉16進制函數
11
12 string hexStr(0.75*limit, ' '); //目標字符串預留出一定空間,16進制數位數為對應10進制數的3/4,調用string類的構造函數
13 int Value=0; //Value保存每次短除法的余數
14 int i = 0;
15
16 if( deci < 10) //待轉換數小於10時,16和10進制表示方法相同
17 return string(1,(char)deci);
18
19 for(; deci != 0; ++i, deci /= 16) { //短除法循環
20 Value = deci%16;
21 switch(Value) { //多分支選擇表示10~15的字母
22 case 10 :hexStr.at(i) = 'A'; break;
23 case 11 :hexStr.at(i) = 'B'; break;
24 case 12 :hexStr.at(i) = 'C'; break;
25 case 13 :hexStr.at(i) = 'D'; break;
26 case 14 :hexStr.at(i) = 'E'; break;
27 case 15 :hexStr.at(i) = 'F'; break;
28 default :hexStr.at(i) = Value+'0'; //用數字表示的要將數字轉化為對應的字符
29 }
30 }
31 hexStr=hexStr.substr(0, i); //取有字符的字串
32
33 reverse(hexStr.begin(), hexStr.end()); //使用迭代器反轉字符串,因為寫入的高低位顛倒
34
35 return hexStr; //返回對應的16進制數字符串
36 }
37
38 string deciToOct(unsigned deci) { //10進制轉8進制函數,結構類似於上
39
40 string hexStr(limit, ' ');
41 int Value=0;
42 int i = 0;
43
44 if( deci < 8)
45 return string(1,(char)deci);
46
47 for(; deci != 0; ++i, deci /= 8) {
48 Value = deci%8;
49 hexStr.at(i) = Value+'0';
50 }
51
52 hexStr=hexStr.substr(0, i);
53
54 reverse(hexStr.begin(), hexStr.end());
55
56 return hexStr;
57 }
58
59 string deciToBin(unsigned deci) { //10進制轉2進制函數,結構類似於上
60
61 string hexStr(3*limit, ' ' );
62 int Value=0;
63 int i = 0;
64
65 for(; deci != 0; ++i, deci /= 2) {
66 Value = deci%2;
67 hexStr.at(i) = Value+'0';
68 }
69
70 hexStr=hexStr.substr(0, i);
71
72 reverse(hexStr.begin(), hexStr.end());
73
74 return hexStr;
75 }
76
77 long anyToDeci(string any, unsigned scale){ //按權展開函數
78
79 long sum = 0; //sum為累加和
80 int n = any.length(); //使用string類的方法獲得字符串長度
81
82 for(int i = 0; i < n; i++)
83 if(any.at(i) >= '0' && any.at(i) <= '9')
84 sum += (any.at(i) - '0')* pow(scale, n-1-i); //按權展開的冪乘和累加
85 else if(any.at(i) >= 'a' && any.at(i) <= 'f') //對16進制用字母表示的數的處理
86 sum += (any.at(i) - 'a'+10)* pow(scale, n-1-i);
87 else
88 sum += (any.at(i) - 'A'+10)* pow(scale, n-1-i);
89 return sum;
90 }
91
92 #endif // CONVERTFUNCS_H_INCLUDED
limit是輸入的十進制數最多位數.若sizeof(unsigned)在某機器上為4,即無符號整數占用4Byte,也即32位,最大數為232,根據210≈103,有220≈106 ,230≈109,因此232至少用10位10進制數表示,因此輸入的十進制數最多10位.
main.cpp:
1 #include<iostream>
2 #include<algorithm> //reverse函數聲明在次頭文件中
3 #include"convertfuncs.h" //自定義頭文件
4
5 using namespace std;
6
7 int main(){
8
9 system("color 3F"); //設置控制台窗口背景色和前景色
10
11 int k = 0;
12 unsigned deci = 0; //輸入的10進制數
13 extern string deciToHex(unsigned);
14 extern string deciToOct(unsigned);
15 extern string deciToBin(unsigned);
16 extern long anyToDeci(string, unsigned); //函數原型
17 string any = ""; //預留的空字符串
18 unsigned scale=0; //進制標識
19
20 cout<<"無符號整數數制轉換器"<<endl;
21 cout<<"By Alexios Yan"<<endl;
22 cout<<endl;
23
24 while(true){ //無限循環功能菜單
25 cin.sync(); //清空輸入緩沖區
26 cout<<"按任意鍵繼續"<<endl;
27 cin.get();
28 system("cls"); //清屏
29 cout<<"功能列表:"<<endl;
30 cout<<"0. 退出"<<endl;
31 cout<<"1. 10進制到16進制"<<endl;
32 cout<<"2. 10進制到8進制"<<endl;
33 cout<<"3. 10進制到2進制"<<endl;
34 cout<<"4. 2或8或16進制到10進制"<<endl;
35 cout<<endl;
36 cout<<"請選擇:"<<endl;
37 cin>>k;
38
39 switch(k){ //根據選擇調用不同函數
40 case 0 : exit(0);
41 break;
42 case 1 : cout<<"請輸入需要轉換的10進制數(不多於"<<limit-1<<"位):"<<endl;
43 cin>>deci;
44 cout<<"對應的16進制數是0x"<<deciToHex(deci)<<endl;
45 cout<<endl;
46 break;
47 case 2 : cout<<"請輸入需要轉換的10進制數(不多於"<<limit-1<<"位):"<<endl;
48 cin>>deci;
49 cout<<"對應的8進制數是0"<<deciToOct(deci)<<endl;
50 cout<<endl;
51 break;
52 case 3 : cout<<"請輸入需要轉換的10進制數(不多於"<<limit-1<<"位):"<<endl;
53 cin>>deci;
54 cout<<"對應的2進制數是0b"<<deciToBin(deci)<<endl;
55 cout<<endl;
56 break;
57 case 4 : cout<<"請輸入需要轉換到10進制的數:"<<endl;
58 cin>>any;
59 cout<<"請輸入該數的進制:"<<endl;
60 cin>>scale;
61 cout<<"結果是"<<anyToDeci(any, scale)<<endl;
62 cout<<endl;
63 break;
64 default : cout<<"選擇錯誤,請重新選擇"<<endl;
65 cout<<endl;
66 break;
67 }
68 }
69 return 0;
70 }
本工程在GCC編譯器下編譯通過,成功運行.在GCC編譯器在,不允許在switch語塊中定義變量.
運行截圖:





謝謝大家!轉載請注明出處,謝謝合作!