C++编写程序实现 二进制 八进制 十进制 十六进制相互转换 格式如下 一级菜单: (1)2进制数转换 (2)8进制数转换 (3)10进制数转换 (4)16进制数转换 (0)退出 二级菜单:(只是以2进制转其他进制为例,其他进制相同) (1)2进制数 ---> 8进制数 (2)2进制数 ---> 10进制数 (3)2进制数 ---> 16进制数 (0)返回上级目录
时间: 2023-12-03 13:46:56 浏览: 85
好的,以下是程序的代码:
```
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
// 将二进制字符串转为十进制数
int binToDec(string bin)
{
int dec = 0;
for (int i = 0; i < bin.length(); i++) {
dec += (bin[i] - '0') * pow(2, bin.length() - i - 1);
}
return dec;
}
// 将十进制数转为二进制字符串
string decToBin(int dec)
{
string bin = "";
while (dec != 0) {
bin = to_string(dec % 2) + bin;
dec /= 2;
}
return bin;
}
// 将二进制字符串转为八进制字符串
string binToOct(string bin)
{
string oct = "";
int len = bin.length();
// 补全到3的倍数
while (len % 3 != 0) {
bin = "0" + bin;
len = bin.length();
}
// 每三位二进制转为一位八进制
for (int i = 0; i < len; i += 3) {
string sub = bin.substr(i, 3);
int dec = binToDec(sub);
oct += to_string(dec);
}
return oct;
}
// 将八进制字符串转为二进制字符串
string octToBin(string oct)
{
string bin = "";
int len = oct.length();
// 每一位八进制转为3位二进制
for (int i = 0; i < len; i++) {
int dec = oct[i] - '0';
string sub = decToBin(dec);
// 不足3位补全
while (sub.length() < 3) {
sub = "0" + sub;
}
bin += sub;
}
// 去掉前导0
int pos = bin.find_first_not_of('0');
if (pos != string::npos) {
bin = bin.substr(pos);
} else {
bin = "0";
}
return bin;
}
// 将二进制字符串转为十六进制字符串
string binToHex(string bin)
{
string hex = "";
int len = bin.length();
// 补全到4的倍数
while (len % 4 != 0) {
bin = "0" + bin;
len = bin.length();
}
// 每四位二进制转为一位十六进制
for (int i = 0; i < len; i += 4) {
string sub = bin.substr(i, 4);
int dec = binToDec(sub);
if (dec < 10) {
hex += to_string(dec);
} else {
hex += (char)('A' + dec - 10);
}
}
return hex;
}
// 将十六进制字符串转为二进制字符串
string hexToBin(string hex)
{
string bin = "";
int len = hex.length();
// 每一位十六进制转为4位二进制
for (int i = 0; i < len; i++) {
int dec = 0;
if (hex[i] >= '0' && hex[i] <= '9') {
dec = hex[i] - '0';
} else if (hex[i] >= 'A' && hex[i] <= 'F') {
dec = hex[i] - 'A' + 10;
}
string sub = decToBin(dec);
// 不足4位补全
while (sub.length() < 4) {
sub = "0" + sub;
}
bin += sub;
}
// 去掉前导0
int pos = bin.find_first_not_of('0');
if (pos != string::npos) {
bin = bin.substr(pos);
} else {
bin = "0";
}
return bin;
}
int main()
{
int choice1, choice2;
string num;
cout << "一级菜单:" << endl;
cout << "(1)2进制数转换" << endl;
cout << "(2)8进制数转换" << endl;
cout << "(3)10进制数转换" << endl;
cout << "(4)16进制数转换" << endl;
cout << "(0)退出" << endl;
cout << "请输入您的选择:";
cin >> choice1;
while (choice1 != 0) {
switch (choice1) {
case 1:
cout << "二级菜单:" << endl;
cout << "(1)2进制数 ---> 8进制数" << endl;
cout << "(2)2进制数 ---> 10进制数" << endl;
cout << "(3)2进制数 ---> 16进制数" << endl;
cout << "(0)返回上级目录" << endl;
cout << "请输入您的选择:";
cin >> choice2;
while (choice2 != 0) {
cout << "请输入一个二进制数:";
cin >> num;
switch (choice2) {
case 1:
cout << "转换结果为:" << binToOct(num) << endl;
break;
case 2:
cout << "转换结果为:" << binToDec(num) << endl;
break;
case 3:
cout << "转换结果为:" << binToHex(num) << endl;
break;
default:
cout << "输入错误,请重新输入!" << endl;
break;
}
cout << "请输入您的选择:";
cin >> choice2;
}
break;
case 2:
cout << "二级菜单:" << endl;
cout << "(1)8进制数 ---> 2进制数" << endl;
cout << "(2)8进制数 ---> 10进制数" << endl;
cout << "(3)8进制数 ---> 16进制数" << endl;
cout << "(0)返回上级目录" << endl;
cout << "请输入您的选择:";
cin >> choice2;
while (choice2 != 0) {
cout << "请输入一个八进制数:";
cin >> num;
switch (choice2) {
case 1:
cout << "转换结果为:" << octToBin(num) << endl;
break;
case 2:
cout << "转换结果为:" << stoi(num, nullptr, 8) << endl;
break;
case 3:
cout << "转换结果为:" << binToHex(octToBin(num)) << endl;
break;
default:
cout << "输入错误,请重新输入!" << endl;
break;
}
cout << "请输入您的选择:";
cin >> choice2;
}
break;
case 3:
cout << "二级菜单:" << endl;
cout << "(1)10进制数 ---> 2进制数" << endl;
cout << "(2)10进制数 ---> 8进制数" << endl;
cout << "(3)10进制数 ---> 16进制数" << endl;
cout << "(0)返回上级目录" << endl;
cout << "请输入您的选择:";
cin >> choice2;
while (choice2 != 0) {
cout << "请输入一个十进制数:";
cin >> num;
switch (choice2) {
case 1:
cout << "转换结果为:" << decToBin(stoi(num)) << endl;
break;
case 2:
cout << "转换结果为:" << oct(stoi(num)) << endl;
break;
case 3:
cout << "转换结果为:" << hex << stoi(num) << endl;
break;
default:
cout << "输入错误,请重新输入!" << endl;
break;
}
cout << "请输入您的选择:";
cin >> choice2;
}
break;
case 4:
cout << "二级菜单:" << endl;
cout << "(1)16进制数 ---> 2进制数" << endl;
cout << "(2)16进制数 ---> 8进制数" << endl;
cout << "(3)16进制数 ---> 10进制数" << endl;
cout << "(0)返回上级目录" << endl;
cout << "请输入您的选择:";
cin >> choice2;
while (choice2 != 0) {
cout << "请输入一个十六进制数:";
cin >> num;
switch (choice2) {
case 1:
cout << "转换结果为:" << hexToBin(num) << endl;
break;
case 2:
cout << "转换结果为:" << binToOct(hexToBin(num)) << endl;
break;
case 3:
cout << "转换结果为:" << stoi(num, nullptr, 16) << endl;
break;
default:
cout << "输入错误,请重新输入!" << endl;
break;
}
cout << "请输入您的选择:";
cin >> choice2;
}
break;
default:
cout << "输入错误,请重新输入!" << endl;
break;
}
cout << "一级菜单:" << endl;
cout << "(1)2进制数转换" << endl;
cout << "(2)8进制数转换" << endl;
cout << "(3)10进制数转换" << endl;
cout << "(4)16进制数转换" << endl;
cout << "(0)退出" << endl;
cout << "请输入您的选择:";
cin >> choice1;
}
return 0;
}
```
这个程序可以实现二进制、八进制、十进制、十六进制之间的相互转换。在输入选择后,会进入对应的二级菜单,再根据提示输入转换的数值即可得到转换结果。
阅读全文