UVA_389_Basically Speaking

#include<iostream>    
#include<sstream>    
#include<string>    
#include<vector>    
#include<list>    
#include<set>    
#include<map>    
#include<stack>    
#include<queue>    
#include<algorithm>  
#include<bitset>
#pragma warning(disable:4996)    
using std::cin;
using std::cout;
using std::endl;
using std::stringstream;
using std::string;
using std::vector;
using std::list;
using std::pair;
using std::set;
using std::multiset;
using std::map;
using std::multimap;
using std::stack;
using std::queue;
using std::priority_queue;
using std::bitset;
int ToDecimal(const string &str,const int &mode)
{
	int ret = 0;
	for (size_t i = 0; i < str.size(); i++)
	{
		ret *= mode;
		switch (str[i])
		{
		case '0':ret += 0; break;
		case '1':ret += 1; break;
		case '2':ret += 2; break;
		case '3':ret += 3; break;
		case '4':ret += 4; break;
		case '5':ret += 5; break;
		case '6':ret += 6; break;
		case '7':ret += 7; break;
		case '8':ret += 8; break;
		case '9':ret += 9; break;
		case 'A':ret += 10; break;
		case 'B':ret += 11; break;
		case 'C':ret += 12; break;
		case 'D':ret += 13; break;
		case 'E':ret += 14; break;
		case 'F':ret += 15; break;
		default:break;
		}
	}
	return ret;
}
string ToQuery(int decimal,const  int &mode)
{
	string str;
	while (decimal)
	{
		char character;
		if (decimal%mode >= 0 && decimal%mode <= 9)
		{
			character = decimal%mode + '0';
		}
		else
		{
			character = decimal%mode - 10 + 'A';
		}
		str = character + str;
		decimal /= mode;
	}
	if (str.size())
	{
		//std::reverse(str.begin(), str.end());
		return str;
	}
	else
	{
		return "0";
	}
}
int main()
{
	//freopen("input.txt", "r", stdin);    
	//freopen("output.txt", "w", stdout);
	string str; int mode1, mode2;
	while (cin >> str >> mode1 >> mode2)
	{
		int decimal = ToDecimal(str, mode1);
		str = ToQuery(decimal, mode2);
		if (str.size() <= 7)
		{
			for (int i = 7 - str.size(); i > 0; i--)
			{
				cout << ' ';
			}
			cout << str << endl;
		}
		else
		{
			cout << "  ERROR" << endl;
		}
	}
	return 0;
}

你可能感兴趣的:(uva)