1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
#include <string> #include <stdio.h> #include <sstream> #include <iostream> using namespace std; int fBase,tBase; string str,str1; char ch[200]; int k; int convert() { unsigned long long d; unsigned long long r=0; unsigned long long _pow=1; if(str[str.size()-1]>'9') { d=str[str.size()-1]-55; if(d>=fBase) return -1; } else { d=str[str.size()-1]-'0'; if(d>=fBase) return -1; } //cout<<_pow<<" "<<d<<"\n"; r+=d; for(int i=str.size()-2;i>=0;i--) { _pow*=fBase; if(str[i]>'9') { d=str[i]-55; if(d>=fBase) return -1; } else { d=str[i]-'0'; if(d>=fBase) return -1; } //cout<<_pow<<" "<<d<<"\n"; r+=(d*_pow); } //cout<<"r = "<<r<<"\n"; k=0; if(r==0) { ch[k]='0'; k++; } while(r) { if((r%tBase)>9) ch[k]=(((r%tBase)+55)+'\0'); else ch[k]=((r%tBase)+'0'); r/=tBase; k++; } return 0; } int main() { while(cin>>fBase) { cin>>tBase; cin>>str; str1=""; //cout<<str<<"\n"; if(convert()==-1) cout<<str<<" is an illegal base "<<fBase<<" number\n"; else { cout<<str<<" base "<<fBase<<" = "; for(int i=k-1;i>=0;i--) printf("%c",ch[i]); cout<<" base "<<tBase<<"\n"; } } return 0; } |