进制转换

#include <iostream>
#include <stack>
#include <cmath>
using namespace std;

int main()
{
	stack<int> s;
	int n, m, r;
	while(cin >> n >> r)
	{
		if(n < 0)	
		{
			n = -n;
			cout << "-";
		}
		while(n != 0)
		{
			s.push(n % r);
			n = n / r;
		}
		while(!s.empty())
		{
			if(r > 10)
			{
				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();
			}
			else	cout << s.top();
			s.pop();
		}
		cout << endl;
	}
	
	return 0;
}

  

原文地址:https://www.cnblogs.com/mjn1/p/11275149.html