C++经典编程题目(五)进制转换器

 5. 输入一个十进数,将其转换成 N 进制数(0
#include 
#include 

using namespace std;

void convert2otherRadix(int num,int radix)
{
	char outString[100];
	int i = 0;
	int tempNum = num;

	if (radix<=10)
	{
		while ( tempNum )
		{
			outString[i] = tempNum%radix + 48;
			tempNum = tempNum / radix;
			i++;
		}
		outString[i] = '\0';
		i = i-1;
		cout << "Result is :" << endl;
		while (i>=0)
		{
			cout << outString[i];
			i--;
		}
		cout << endl;
		return;
	}
	if ((radix > 10) && (radix<=16))
	{
		int Remainder = 0;
		while (tempNum)
		{
			Remainder = tempNum%radix;
			if (Remainder>9)
			{
				outString[i] = Remainder + 55;
			}
			else
			{
				outString[i] = Remainder + 48;
			}		
			tempNum = tempNum / radix;
			i++;
		}
		outString[i] = '\0';
		i = i-1;
		cout << "Result is :" << endl;
		while (i >= 0)
		{
			cout << outString[i];
			i--;
		}
		cout << endl;
		return;
	}
}

int main()
{
	int radix;
	int num;

	cout << "Please input the radix you want :" << endl;
	cin >> radix;

	cout << "Please input the number you want  to convert :" << endl;
	cin >> num;

	convert2otherRadix(num, radix);

	system("pause");
	return 0;
}

你可能感兴趣的:(C++学习笔记)