Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- string str,nb;
- int b,c;
- char cn[10001];
- char * Convert(int from,int to,const char * s,char * out)
- {
- if(s==NULL)
- return NULL;
- if(from<2||from>36||to<2||to>36)
- return NULL;
- int il=strlen(s);
- int *fs=new int[il];
- int k=0;
- int i,j;
- for(i=il-1;i>=0;--i)
- {
- if(s[i]>='0'&&s[i]<='9')
- {
- fs[k]=(int)(s[i]-'0');
- }
- else
- {
- if(s[i]>='A'&&s[i]<='Z')
- {
- fs[k]=10+(int)(s[i]-'A');
- }
- else if(s[i]>='a'&&s[i]<='z')
- {
- fs[k]=10+(int)(s[i]-'a');
- }
- else
- {
- delete[]fs;
- return NULL;
- }
- }
- ++k;
- }
- for(i=0;i<il;++i)
- {
- if(fs[i]>=from )
- return NULL;
- }
- double x=ceil(log(from )/log (to));
- int ol=1+(il*x);
- int * ts=new int[ol];
- int * cums=new int [ol];
- for (i=0;i<ol;i++)
- {
- ts[i]=0;
- cums[i]=0;
- }
- ts[0]=1;
- for(i =0;i<il;++i)
- {
- for(j=0;j<ol;++j)
- {
- cums[j]+=ts[j]*fs[i];
- int temp=cums[j],rem=0,ip=j;
- do
- {
- rem=temp/to;
- cums[ip]=temp-rem*to;
- ++ip;
- if(ip>=ol)
- {
- if(rem>0)
- {
- delete[]ts;
- delete[]cums;
- delete[]fs;
- return NULL;
- }
- break;
- }
- cums[ip]+=rem;
- temp=cums[ip];
- }
- while (temp>=to);
- }
- for(j=0;j<ol;++j)
- ts[j]=ts[j]*from;
- for(j=0;j<ol;++j)
- {
- int temp=ts[j],rem=0,ip=j;
- do
- {
- rem=temp/to;
- ts[ip]=temp-rem*to;
- ++ip;
- if (ip >= ol)
- {
- if (rem>0)
- {
- delete[]ts;
- delete[]cums;
- delete[]fs;
- return NULL;
- }
- break;
- }
- ts[ip]+=rem;
- temp = ts[ip];
- }
- while(temp>=to);
- }
- }
- if(out==NULL )
- out=(char*)malloc(sizeof(char)*(ol+1));
- int spos=0;
- bool first=false;
- for(i=ol-1;i>=0;--i)
- {
- if (cums[i]!=0)
- first=true;
- if (!first)
- continue;
- if (cums[i]<10)
- out[spos]=(char)(cums[i]+'0');
- else
- out[spos]=(char)(cums[i]+'A'-10);
- ++spos;
- }
- out[spos]=0;
- delete[]ts;
- delete[]cums;
- delete[]fs;
- return out;
- }
- int main()
- {
- ifstream f("base_converter.in");
- getline(f,str);
- f.close();
- vector<string>x;
- istringstream buffer(str);
- for(string w;buffer>>w;)
- x.push_back(w);
- for(unsigned int j=0;j<x.size();++j)
- switch(j)
- {
- case 0: nb=x[j];break;
- case 1: b=stoi(x[j]);break;
- case 2: c=stoi(x[j]);break;
- }
- int lnb=nb.length();
- char char_array[lnb+1];
- strcpy(char_array,nb.c_str());
- Convert(b,c,char_array,cn);
- ofstream g("base_converter.out");
- g<<cn;
- g.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement