#include #include using namespace std; int char_to_dec(char input) { if(input >= '0' && input <= '9') return input - '0'; return input - 'A' + 10; } char dec_to_char(int input) { if(input <= 9 && input >= 0) return (char)(input + 48); return (char)(input + 65 - 10); } bool check_correct(int base, string input) { for(int i=0;i= base) return false; } return true; } void transfer_base(int base, int to_base, string input) { long long decimal_number = 0; long long base_backup = 1; string output = ""; //to decimal for(int i=input.size() - 1;i>=0;--i) { decimal_number += char_to_dec(input[i]) * base_backup; base_backup *= base; } //to to_base if(decimal_number == 0) output = "0"; while(decimal_number > 0) { int modular = decimal_number % to_base; output.insert(0, 1, dec_to_char(modular)); decimal_number /= to_base; } cout << input << " base " << base << " = " << output << " base " << to_base << endl; } int main() { int base, to_base; string input; while(cin >> base >> to_base >> input) { if(check_correct(base, input)) transfer_base(base, to_base, input); else cout << input << " is an illegal base " << base << " number" << endl ; } return 0; }
文章標籤
全站熱搜
創作者介紹
創作者 eric30 的頭像
eric30

*Our Blog*

eric30 發表在 痞客邦 留言(0) 人氣(0)