Skip to content

Commit d37acb2

Browse files
authored
Add files via upload
Version 1; Convert; base1 to base2. 2 <= base1,base2 <= 36. Input through console or file:`input.txt` Output through console or file:`output.txt` Precautions; Make sure data type can't exceeds(overflow).
1 parent 269e1bd commit d37acb2

File tree

1 file changed

+218
-0
lines changed

1 file changed

+218
-0
lines changed

BaseConversionAlgorithm V1.cpp

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
#include<iostream>
2+
#include<string.h>
3+
#include<stdio.h>
4+
#include<stdlib.h>
5+
#include<fstream>
6+
#include<ctime>
7+
8+
using namespace std;
9+
10+
char ch[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
11+
char ch1[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
12+
13+
const long int size1=1000000;//4094 IS FIX FOR CHAR
14+
char instring[size1*2];
15+
unsigned int process[3][size1]={0};
16+
//100000 digits of decimal to hex 397 seconds
17+
//168 FFF->output from randomDECIMAL->INPUT
18+
//168
19+
void dis(long int n,long int f=0,long int k1=0,long int num=0)
20+
{
21+
long int k;
22+
if(k1!=0 && num != 0)
23+
cout<<k1<<"-"<<n+1<<"-|";
24+
for(k=n;k>=0;k--)
25+
cout<<ch1[process[0][k]];
26+
if(!(f == 0 && process[2][0] ==0))
27+
{cout<<".";
28+
for(k=0;k<=f;k++)
29+
cout<<ch1[process[2][k]];
30+
}
31+
cout<<endl;
32+
}
33+
34+
int main()
35+
{
36+
fstream input,output;
37+
clock_t start;
38+
double duration;
39+
long int rows=size1,trows;//int n=sizeof(process[0])/2;
40+
long int j,i,k,m=0,m1=0,l,p,o,dec;
41+
long int base1,base2,nbase2,ilen=0,flen=0,pos,f1,flimit=100;
42+
int choice=0,temp,numtemp,i2,save,value=0,j1;
43+
long int dd=0,tr=0,allzero=0;
44+
base1=10;
45+
base2=16;
46+
trows=rows;
47+
save=0;
48+
begining:
49+
//cout<<"size"<<size<<endl;
50+
rows=trows;
51+
{ for(p=0;p<rows;p++){instring[p]=process[0][p]=process[1][p]=process[2][p]=0;}}
52+
53+
54+
cout<<"________________________________________________________________________________________________________________"<<endl;
55+
cout<<" DATA REPRESENTATION"<<endl;
56+
cout<<"1.CONVERT A (BASE1="<<base1<<") TO (BASE2="<<base2<<") \n ( FRACTION LIMIT ="<<flimit<<" SAVE OUTPUT=";
57+
if(save==1)
58+
{
59+
cout<<"YES"<<endl;
60+
}
61+
else
62+
cout<<"NO"<<endl;
63+
cout<<"2.CHANGE THE BASE , FRACTION LIMIT & SAVING OUTPUT?"<<endl;
64+
65+
cout<<"3.CLEAR SCREEN "<<endl;
66+
cout<<"4.HELP "<<endl;
67+
cout<<"5.EXIT "<<endl;
68+
cout<<"ENTER YOUR CHOICE =";
69+
cin>>choice;
70+
if(choice==1)
71+
{
72+
goto case1_start;
73+
}
74+
else if (choice==2)
75+
{
76+
cout<<"BASE1 = ";
77+
cin>>base1;
78+
cout<<"BASE2 = ";
79+
cin>>base2;
80+
cout<<"FRACTION LIMIT = ";
81+
cin>>flimit;
82+
cout<<"1.SAVE OUTPUT IN FILE (YES =1 IF NO = 0 )) = ";
83+
cin>>save;
84+
goto begining;
85+
}
86+
else if (choice==3)
87+
{ system("cls");
88+
goto begining;
89+
}
90+
else if(choice ==4)
91+
{ cout<<" *.OUTPUT AUTOMATICALLY SAVED IN FILE \"output.txt\" IF YOU SET THE OPTION (NOTE: NO NEED TO CREATE FILE)"<<endl;
92+
cout<<" *.BEFORE CHOOOSING OPTION(1.2.INPUT THROUGH FILE) INPUT SHOULD BE SAVED IN FILE \"input.txt\" "<<endl;
93+
goto begining;
94+
}
95+
else if(choice ==5)
96+
{
97+
exit(0);
98+
}
99+
else if(choice> 6 || choice <=0)
100+
{
101+
cout<<"INVALID CHOICE "<<endl;
102+
goto begining;
103+
}
104+
case1_start:
105+
106+
107+
cout<<" 1.1.INPUT THROUGH CONSOLE 1.2.INPUT THROUGH FILE"<<endl;
108+
109+
cout<<" ENTER YOUR CHOICE = ";
110+
cin>>choice;
111+
if(choice==1)
112+
{ cout<<"ENTER THE NUMBER (base)"<<base1<<endl;
113+
cin>>instring;}
114+
else if(choice==2)
115+
{
116+
input.open("input.txt",ios::in);
117+
input>>instring;
118+
cout<<instring<<endl;
119+
input.close();
120+
}
121+
else
122+
{cout<<"INVALID CHOICE RE-ENTER"<<endl;
123+
goto case1_start;
124+
}
125+
//strrev(instring);
126+
ilen=strlen(instring);
127+
cout<<"LENGTH ="<<ilen<<endl;
128+
if(ilen > sizeof(instring))
129+
{
130+
cout<<" STRING SIZE LIMIT IS = "<<sizeof(instring)<<endl;
131+
goto case1_start;
132+
}
133+
for(p=0;p<ilen;p++)
134+
{
135+
if(instring[p]=='.')
136+
break;
137+
}
138+
for(i=0;i<ilen;i++)
139+
{
140+
if(instring[i]>57)
141+
instring[i]=instring[i]-7;
142+
instring[i]=instring[i]-48;
143+
}
144+
k=0;
145+
for(i=ilen-1;i>p;i--)
146+
{
147+
process[1][k]=instring[i];
148+
k++;
149+
}
150+
dec=1;
151+
cout<<"please wait(processiong ......) "<<endl;
152+
start = std::clock();
153+
flen=ilen-p;
154+
ilen=p;
155+
dd=0;
156+
f1=-1;
157+
nbase2=base2-1;
158+
for(k=0;k<ilen;k++)
159+
{ for(p=0;(process[0][p]*=base1),p<dec;p++);
160+
process[0][0]+=instring[k];
161+
for(i=0;dec>i;i++)
162+
{ i2=i;
163+
while(process[0][i2] > nbase2)
164+
{
165+
if(dd<i2){dd=i2;}
166+
process[0][i2+1]+=process[0][i2]/base2;
167+
process[0][i2]%=base2;
168+
i2++;
169+
}
170+
}
171+
dec=dd+2;
172+
//dis(dec-1);
173+
}
174+
do
175+
{ allzero=0;
176+
for(p=0;(process[1][p]*=base2),p<flen;p++);
177+
for(i=0;flen-1>i;i++)
178+
{
179+
process[1][i+1]+=process[1][i]/base1;
180+
process[1][i]%=base1;
181+
}
182+
process[2][++f1]=process[1][flen-1];
183+
//dis(dec-1,f1);
184+
process[1][flen-1]=0;
185+
for(p=0;p<flen;p++){ if(process[1][p]!=0 && f1 < flimit-1){ allzero=1; break; } }
186+
}while(allzero==1);
187+
duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
188+
pos=0;
189+
for(p=0;p<rows;p++)
190+
{
191+
if(process[0][p]>0)
192+
pos=p;
193+
}
194+
cout<<endl<<"EQUIVALENT TO (base)"<<base2<<endl;
195+
dis(pos,f1);
196+
if(save==1)
197+
{ for(i=0;i<ilen;i++)
198+
{
199+
instring[i]=0;
200+
}
201+
p=0;
202+
for(i=pos;i>=0;i--)
203+
{instring[p]=ch[process[0][i]];p++;}
204+
if(!(f1 == 0 && process[2][0] ==0))
205+
{ instring[p]='.';
206+
for(k=0;k<=f1;k++)
207+
{ p++;
208+
instring[p]=ch[process[2][k]];
209+
}
210+
}
211+
output.open("output.txt",ios::out);
212+
output<<instring;
213+
output.close();
214+
}
215+
std::cout<<"TIME TAKEN FOR CONVERTION "<< duration <<endl;
216+
goto begining;
217+
return 0;
218+
}

0 commit comments

Comments
 (0)