我是靠谱客的博主 优美板凳,最近开发中收集的这篇文章主要介绍c++二,八,十,十六进制的互相转换(包括小数,负数,且保留小数点后六位)关于本文章内容说明进制间的转换思想功能目的代码实现,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

关于本文章内容说明

文章基本只是将作者本人编写的代码进行复制粘贴,而这份代码是作者大一上学期期末作业,若有不足之处,还请见谅。原因主要还是快到期末了,不想写代码,想着“借鉴”网上大佬们的代码,但是没找到合适的,于是自己就写一份吧。

进制间的转换思想

因为十进制的方便,所以利用十进制作为中间桥梁进行进制之间的转换。对于浮点数进行整数部分与小数部分的分离来计算,并在最后相加。

功能目的

1.各进制数字符串转换为十进制正数值;
2.分离十进制数值的整数和小数部分;
3.对分离出的整数、小数部分分别做转换,将其转换为目标进制;
4将转换好的整数、小数部分以及符号位拼接为字符串输出。

代码实现

菜单项的实现

构建一级菜单,利用循环构建,并且调用二级菜单

void menu()
{
	int n = -1;
	while (n != 0)
	{
		cout << "n**********************";
		cout << "n(1)2进制数转换";
		cout << "n(2)8进制数转换";
		cout << "n(3)10进制数转换";
		cout << "n(4)16进制数转换";
		cout << "n(0)退出";
		cout << "n**********************";
		cout << "n请输入功能号:";
		cin >> n;
		while (n < 0 || n>4)
		{
			cout << "n输入错误,请重新输入功能号:";
			cin >> n;
		}
		if (n == 1)
			two_menu_1();
		else if (n == 2)
			two_menu_2();
		else if (n == 3)
			two_menu_3();
		else if (n == 4)
			two_menu_4();
		else return;
	}
}

构建二级菜单,并且能够返回上一级菜单,共有四个分别对应(二进制,八进制,十进制,十六进制)

void two_menu_1()
{
	int n = -1;
	string x;
	while (n != 0)
	{
		cout << "n*******************************";
		cout << "n(1)2进制数  --->  8进制数";
		cout << "n(2)2进制数  --->  10进制数";
		cout << "n(3)2进制数  --->  16进制数";
		cout << "n(0)返回上级目录";
		cout << "n*******************************";
		cout << "n请输入功能号:";
		cin >> n;
		while (n < 0 || n>3)
		{
			cout << "n输入错误,请重新输入功能号:";
			cin >> n;
		}
		if (n == 1)
		{
			cout << "n请输入2进制数:";
			cin >> x;
			ndec(2, x);
			decn(x, 8);
			cout << "n8进制数为:" << x;
		}
		else if (n == 2)
		{
			cout << "n请输入2进制数:";
			cin >> x;
			ndec(2, x);
			cout << "n10进制数为:" << x;
		}
		else if (n == 3)
		{
			cout << "n请输入2进制数:";
			cin >> x;
			ndec(2, x);
			decn(x, 16);
			cout << "n16进制数为:" << x;
		}
		else menu();
	}
}//第一个子菜单
void two_menu_2()
{
	int n = -1;
	string x;
	while (n != 0)
	{
		cout << "n*******************************";
		cout << "n(1)8进制数  --->  2进制数";
		cout << "n(2)8进制数  --->  10进制数";
		cout << "n(3)8进制数  --->  16进制数";
		cout << "n(0)返回上级目录";
		cout << "n*******************************";
		cout << "n请输入功能号:";
		cin >> n;
		while (n < 0 || n>3)
		{
			cout << "";
			cin >> n;
		}
		if (n == 1)
		{
			cout << "请输入8进制数:";
			cin >> x;
			ndec(8, x);
			decn(x, 2);
			cout << "n2进制数为:" << x;
		}
		else if (n == 2)
		{
			cout << "请输入8进制数:";
			cin >> x;
			ndec(8, x);
			cout << "n10进制数为:" << x;
		}
		else if (n == 3)
		{
			cout << "请输入8进制数:";
			cin >> x;
			ndec(8, x);
			decn(x, 16);
			cout << "n16进制数为:" << x << endl;
		}
		else menu();
	}
}
void two_menu_3()
{
	int n = -1;
	string x;
	while (n != 0)
	{
		cout << "n*******************************";
		cout << "n(1)10进制数  --->  2进制数";
		cout << "n(2)10进制数  --->  8进制数";
		cout << "n(3)10进制数  --->  16进制数";
		cout << "n(0)返回上级目录";
		cout << "n*******************************";
		cout << "n请输入功能号:";
		cin >> n;
		while (n < 0 || n>3)
		{
			cout << "n输入错误,请重新输入功能号:";
			cin >> n;
		}
		if (n == 1)
		{
			cout << "n请输入10进制数:";
			cin >> x;
			decn(x, 2);
			cout << "n2进制数为:" << x;
		}
		else if (n == 2)
		{
			cout << "n请输入10进制数:";
			cin >> x;
			decn(x, 8);
			cout << "n8进制数为:" << x;
		}
		else if (n == 3)
		{
			cout << "n请输入10进制数:";
			cin >> x;
			decn(x, 16);
			cout << "n16进制数为:" << x;
		}
		else menu();
	}
}
void two_menu_4()
{
	int n = -1;
	string x;
	while (n != 0) {
		cout << "n*******************************";
		cout << "n(1)16进制数  --->  2进制数";
		cout << "n(2)16进制数  --->  8进制数";
		cout << "n(3)16进制数  --->  10进制数";
		cout << "n(0)返回上级目录";
		cout << "n*******************************";
		cout << "n请输入功能号:";
		cin >> n;
		while (n < 0 || n>3)
		{
			cout << "n输入错误,请重新输入功能号:";
			cin >> n;
		}
		if (n == 1)
		{
			cout << "n请输入16进制数:";
			cin >> x;
			ndec(16, x);
			decn(x, 2);
			cout << "n2进制数为:" << x;
		}
		else if (n == 2)
		{
			cout << "n请输入16进制数:";
			cin >> x;
			ndec(16, x);
			decn(x, 8);
			cout << "n8进制数为:" << x;
		}
		else if (n == 3)
		{
			cout << "n请输入16进制数:";
			cin >> x;
			ndec(16, x);
			cout << "n10进制数为:" << x;
		}
		else menu();
	}
}

非法性判断

注意小数点的个数,不符合进制要求的数字或字符。
n为进制,x为输入的进制数,sign为正负,point为小数点个数。(可以用do while进行化简)

int legal(int n, string& x,int sign)
{
	unsigned int point = 0, illegal = 0,i;
	if (x[0] == '-')
		sign++;
	for (i = 0 + sign; i < x.length(); i++)
	{
		if (x[i] == '.')
		{
			point++;
			continue;
		}
		if (x[i] < '0' || x[i] >= n + '0')
			illegal++;
		if(n==16&&(x[i]>='A'||x[i]<='F'))
			continue;
	}
	while (point >= 2 || illegal > 0)
	{
		cout << "输入错误,请重新输入:";
		cin >> x;
		sign = 0;
		if (x[0] == '-')
			sign++;
		point = 0, illegal = 0;
		for (i = 0 + sign; i < x.size(); i++)
		{
			if (x[i] == '.')
			{
				point++;
				continue;
			}
			if (x[i] < '0' || x[i]>n + '0')
				illegal++;
			if (n == 16 && (x[i] >= 'A' || x[i] <= 'F'))
				continue;
		}
	}
	return point;
}

其他进制转换为十进制

void ndec(int  n, string& x)
{
	unsigned int whole_n = 0, i, j, k, point = 0, sign = 0, num = 1, whole = 0, small_n = 0, wn = 1, sn = 0, control_small = 0;//whole为整数部分,whole_n为整数位数(若为负数不会加一)
	double small = 0, new_num;
	if (x[0] == '-')
		sign++;
	point=legal(n, x, sign);
	if (point == 1)
	{
		for (i = 0 + sign; i < x.size(); i++, whole_n++)
			if (x[i] == '.')
				break;
		for (i = i + 1; i < x.size(); i++)
			small_n++;
		for (j = 0 + sign; j < whole_n + sign; j++)
		{
			num = 1;
			for (k = 0; k < whole_n + sign - j - 1; k++)
				num = num * n;
			if (x[j] >= 'A' && x[j] <= 'F')
				whole = whole + (int(x[j]) - 55) * num;
			else whole = whole + (int(x[j]) - 48) * num;
		}
		for (control_small = 0, j = j + 1; j < x.size(); j++)
		{
			if (control_small >= 6)
				break;
			num = 1;
			for (k = 0; k < j - whole_n - sign; k++)
				num = num * n;
			if (x[j] >= 'A' && x[j] <= 'F')
				small = small + (int(x[j]) - double(55)) / num;
			else small = small + (int(x[j]) - double(48)) / num;
			control_small++;
		}
	}
	else
	{
		for (i = 0 + sign; i < x.size(); i++)
			whole_n++;
		for (i = 0 + sign; i < x.size() + sign; i++)
		{
			num = 1;
			for (k = 0; k < x.size() - i - 1; k++)
				num = num * n;
			if (x[i] >= 'A' && x[i] <= 'F')
				whole = whole + (int(x[i]) - 55) * num;
			else whole = whole + (int(x[i]) - 48) * num;
		}
	}
	new_num = whole + small;
	double t = new_num;//以下是将数字转化为字符串
	x = "";
	if (sign)
		x += '-';
	while (int(t) / 10 != 0)
	{
		wn++;
		t = t / 10;
	}
	t = new_num;
	if (point)
	{
		while (t * 10 != (int(t)) * double(10))
		{
			sn++;
			t = t * 10;
		}
	}
	t = new_num;
	for (i = 0 + sign; i < wn + sign; i++)
	{
		num = 1;
		for (k = 0; k < wn + sign - i - 1; k++)
			num = num * 10;
		x += int(t / num) + '0';
		t = int(t) % num;
	}
	if (point)
	{
		x += '.';
		t = new_num;
		for (i = 0; i < sn; i++)
		{
			x += int((t - int(t)) * 10) + '0';
			t = t * 10;
		}
	}
}

十进制转换为其他进制

void decn(string& x, int n)
{
	unsigned int whole_n = 0, small_n = 0, i, j, point = 0, sign = 0, whole = 0, control_small;
	double small = 0, new_num, t;
	if (x[0] == '-')
		sign++;
	point = legal(10, x, sign);
	for (i = 0 + sign; i < x.size(); i++)
	{
		if (x[i] == '.')
			break;
		whole *= 10;
		whole = whole + int(x[i] - '0');
	}
	for (j = x.size() - 1; j > i; j--)
	{
		small = small + x[j] - '0';
		small = small / 10;
	}
	new_num = whole + small;
	x = "";
	if (sign)
		x = "-";
	fun_1(whole, x, n);
	t = small;
	if (point)
	{
		x += '.';
		control_small = 0;
		while (control_small < 5 && int(t * n) != t * n)
		{
			if (t * n >= 10)
				x = x + char(int(t * n) + 55);
			else
				x = x + char(int(t * n) + 48);
			t = t * n - int(t * n);
			control_small++;
		}
		if (t * n >= 10)
			x = x + char(int(t * n) + 55);
		else
			x = x + char(int(t * n) + 48);
	}
}
void fun_1(int i, string& x, int n)
{
	if (i >= n)
	{
		fun_1(i / n, x, n);
		if (i >= 10)
			x = x + char(i % n + 55);
		else
			x = x + char(i % n + 48);
	}
	else
	{
		if (i >= 10)
			x = x + char(i + 55);
		else
			x = x + char(i + 48);
	}
}

全部代码

#include<string>
#include <iostream>
#include<iomanip>
using namespace std;
void menu();
void two_menu_1();
void two_menu_2();
void two_menu_3();
void two_menu_4();
void ndec(int, string&);
void decn(string&, int);
void fun_1(int, string&, int);
int legal(int, string&,int);


int main()
{
	menu();
	return 0;
}


void menu()
{
	int n = -1;
	while (n != 0)
	{
		cout << "n**********************";
		cout << "n(1)2进制数转换";
		cout << "n(2)8进制数转换";
		cout << "n(3)10进制数转换";
		cout << "n(4)16进制数转换";
		cout << "n(0)退出";
		cout << "n**********************";
		cout << "n请输入功能号:";
		cin >> n;
		while (n < 0 || n>4)
		{
			cout << "n输入错误,请重新输入功能号:";
			cin >> n;
		}
		if (n == 1)
			two_menu_1();
		else if (n == 2)
			two_menu_2();
		else if (n == 3)
			two_menu_3();
		else if (n == 4)
			two_menu_4();
		else return;
	}
}//主菜单的实现
void two_menu_1()
{
	int n = -1;
	string x;
	while (n != 0)
	{
		cout << "n*******************************";
		cout << "n(1)2进制数  --->  8进制数";
		cout << "n(2)2进制数  --->  10进制数";
		cout << "n(3)2进制数  --->  16进制数";
		cout << "n(0)返回上级目录";
		cout << "n*******************************";
		cout << "n请输入功能号:";
		cin >> n;
		while (n < 0 || n>3)
		{
			cout << "n输入错误,请重新输入功能号:";
			cin >> n;
		}
		if (n == 1)
		{
			cout << "n请输入2进制数:";
			cin >> x;
			ndec(2, x);
			decn(x, 8);
			cout << "n8进制数为:" << x;
		}
		else if (n == 2)
		{
			cout << "n请输入2进制数:";
			cin >> x;
			ndec(2, x);
			cout << "n10进制数为:" << x;
		}
		else if (n == 3)
		{
			cout << "n请输入2进制数:";
			cin >> x;
			ndec(2, x);
			decn(x, 16);
			cout << "n16进制数为:" << x;
		}
		else menu();
	}
}//第一个子菜单
void two_menu_2()
{
	int n = -1;
	string x;
	while (n != 0)
	{
		cout << "n*******************************";
		cout << "n(1)8进制数  --->  2进制数";
		cout << "n(2)8进制数  --->  10进制数";
		cout << "n(3)8进制数  --->  16进制数";
		cout << "n(0)返回上级目录";
		cout << "n*******************************";
		cout << "n请输入功能号:";
		cin >> n;
		while (n < 0 || n>3)
		{
			cout << "";
			cin >> n;
		}
		if (n == 1)
		{
			cout << "请输入8进制数:";
			cin >> x;
			ndec(8, x);
			decn(x, 2);
			cout << "n2进制数为:" << x;
		}
		else if (n == 2)
		{
			cout << "请输入8进制数:";
			cin >> x;
			ndec(8, x);
			cout << "n10进制数为:" << x;
		}
		else if (n == 3)
		{
			cout << "请输入8进制数:";
			cin >> x;
			ndec(8, x);
			decn(x, 16);
			cout << "n16进制数为:" << x << endl;
		}
		else menu();
	}
}
void two_menu_3()
{
	int n = -1;
	string x;
	while (n != 0)
	{
		cout << "n*******************************";
		cout << "n(1)10进制数  --->  2进制数";
		cout << "n(2)10进制数  --->  8进制数";
		cout << "n(3)10进制数  --->  16进制数";
		cout << "n(0)返回上级目录";
		cout << "n*******************************";
		cout << "n请输入功能号:";
		cin >> n;
		while (n < 0 || n>3)
		{
			cout << "n输入错误,请重新输入功能号:";
			cin >> n;
		}
		if (n == 1)
		{
			cout << "n请输入10进制数:";
			cin >> x;
			decn(x, 2);
			cout << "n2进制数为:" << x;
		}
		else if (n == 2)
		{
			cout << "n请输入10进制数:";
			cin >> x;
			decn(x, 8);
			cout << "n8进制数为:" << x;
		}
		else if (n == 3)
		{
			cout << "n请输入10进制数:";
			cin >> x;
			decn(x, 16);
			cout << "n16进制数为:" << x;
		}
		else menu();
	}
}
void two_menu_4()
{
	int n = -1;
	string x;
	while (n != 0) {
		cout << "n*******************************";
		cout << "n(1)16进制数  --->  2进制数";
		cout << "n(2)16进制数  --->  8进制数";
		cout << "n(3)16进制数  --->  10进制数";
		cout << "n(0)返回上级目录";
		cout << "n*******************************";
		cout << "n请输入功能号:";
		cin >> n;
		while (n < 0 || n>3)
		{
			cout << "n输入错误,请重新输入功能号:";
			cin >> n;
		}
		if (n == 1)
		{
			cout << "n请输入16进制数:";
			cin >> x;
			ndec(16, x);
			decn(x, 2);
			cout << "n2进制数为:" << x;
		}
		else if (n == 2)
		{
			cout << "n请输入16进制数:";
			cin >> x;
			ndec(16, x);
			decn(x, 8);
			cout << "n8进制数为:" << x;
		}
		else if (n == 3)
		{
			cout << "n请输入16进制数:";
			cin >> x;
			ndec(16, x);
			cout << "n10进制数为:" << x;
		}
		else menu();
	}
}

void ndec(int  n, string& x)
{
	unsigned int whole_n = 0, i, j, k, point = 0, sign = 0, num = 1, whole = 0, small_n = 0, wn = 1, sn = 0, control_small = 0;//whole为整数部分,whole_n为整数位数(若为负数不会加一)
	double small = 0, new_num;
	if (x[0] == '-')
		sign++;
	point=legal(n, x, sign);
	if (point == 1)
	{
		for (i = 0 + sign; i < x.size(); i++, whole_n++)
			if (x[i] == '.')
				break;
		for (i = i + 1; i < x.size(); i++)
			small_n++;
		for (j = 0 + sign; j < whole_n + sign; j++)
		{
			num = 1;
			for (k = 0; k < whole_n + sign - j - 1; k++)
				num = num * n;
			if (x[j] >= 'A' && x[j] <= 'F')
				whole = whole + (int(x[j]) - 55) * num;
			else whole = whole + (int(x[j]) - 48) * num;
		}
		for (control_small = 0, j = j + 1; j < x.size(); j++)
		{
			if (control_small >= 6)
				break;
			num = 1;
			for (k = 0; k < j - whole_n - sign; k++)
				num = num * n;
			if (x[j] >= 'A' && x[j] <= 'F')
				small = small + (int(x[j]) - double(55)) / num;
			else small = small + (int(x[j]) - double(48)) / num;
			control_small++;
		}
	}
	else
	{
		for (i = 0 + sign; i < x.size(); i++)
			whole_n++;
		for (i = 0 + sign; i < x.size() + sign; i++)
		{
			num = 1;
			for (k = 0; k < x.size() - i - 1; k++)
				num = num * n;
			if (x[i] >= 'A' && x[i] <= 'F')
				whole = whole + (int(x[i]) - 55) * num;
			else whole = whole + (int(x[i]) - 48) * num;
		}
	}
	new_num = whole + small;
	double t = new_num;//以下是将数字转化为字符串
	x = "";
	if (sign)
		x += '-';
	while (int(t) / 10 != 0)
	{
		wn++;
		t = t / 10;
	}
	t = new_num;
	if (point)
	{
		while (t * 10 != (int(t)) * double(10))
		{
			sn++;
			t = t * 10;
		}
	}
	t = new_num;
	for (i = 0 + sign; i < wn + sign; i++)
	{
		num = 1;
		for (k = 0; k < wn + sign - i - 1; k++)
			num = num * 10;
		x += int(t / num) + '0';
		t = int(t) % num;
	}
	if (point)
	{
		x += '.';
		t = new_num;
		for (i = 0; i < sn; i++)
		{
			x += int((t - int(t)) * 10) + '0';
			t = t * 10;
		}
	}
}
void decn(string& x, int n)
{
	unsigned int whole_n = 0, small_n = 0, i, j, point = 0, sign = 0, whole = 0, control_small;
	double small = 0, new_num, t;
	if (x[0] == '-')
		sign++;
	point = legal(10, x, sign);
	for (i = 0 + sign; i < x.size(); i++)
	{
		if (x[i] == '.')
			break;
		whole *= 10;
		whole = whole + int(x[i] - '0');
	}
	for (j = x.size() - 1; j > i; j--)
	{
		small = small + x[j] - '0';
		small = small / 10;
	}
	new_num = whole + small;
	x = "";
	if (sign)
		x = "-";
	fun_1(whole, x, n);
	t = small;
	if (point)
	{
		x += '.';
		control_small = 0;
		while (control_small < 5 && int(t * n) != t * n)
		{
			if (t * n >= 10)
				x = x + char(int(t * n) + 55);
			else
				x = x + char(int(t * n) + 48);
			t = t * n - int(t * n);
			control_small++;
		}
		if (t * n >= 10)
			x = x + char(int(t * n) + 55);
		else
			x = x + char(int(t * n) + 48);
	}
}

void fun_1(int i, string& x, int n)
{
	if (i >= n)
	{
		fun_1(i / n, x, n);
		if (i >= 10)
			x = x + char(i % n + 55);
		else
			x = x + char(i % n + 48);
	}
	else
	{
		if (i >= 10)
			x = x + char(i + 55);
		else
			x = x + char(i + 48);
	}
}
int legal(int n, string& x,int sign)
{
	unsigned int point = 0, illegal = 0,i;
	if (x[0] == '-')
		sign++;
	for (i = 0 + sign; i < x.length(); i++)
	{
		if (x[i] == '.')
		{
			point++;
			continue;
		}
		if (x[i] < '0' || x[i] >= n + '0')
			illegal++;
		if(n==16&&(x[i]>='A'||x[i]<='F'))
			continue;
	}
	while (point >= 2 || illegal > 0)
	{
		cout << "输入错误,请重新输入:";
		cin >> x;
		sign = 0;
		if (x[0] == '-')
			sign++;
		point = 0, illegal = 0;
		for (i = 0 + sign; i < x.size(); i++)
		{
			if (x[i] == '.')
			{
				point++;
				continue;
			}
			if (x[i] < '0' || x[i]>n + '0')
				illegal++;
			if (n == 16 && (x[i] >= 'A' || x[i] <= 'F'))
				continue;
		}
	}
	return point;
}



最后

以上就是优美板凳为你收集整理的c++二,八,十,十六进制的互相转换(包括小数,负数,且保留小数点后六位)关于本文章内容说明进制间的转换思想功能目的代码实现的全部内容,希望文章能够帮你解决c++二,八,十,十六进制的互相转换(包括小数,负数,且保留小数点后六位)关于本文章内容说明进制间的转换思想功能目的代码实现所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(73)

评论列表共有 0 条评论

立即
投稿
返回
顶部