Skip to content

Commit 3f142c6

Browse files
authored
More redable format of V1
1 parent 32e696f commit 3f142c6

File tree

1 file changed

+302
-0
lines changed

1 file changed

+302
-0
lines changed

BaseConversionAlgorithm V2.cpp

Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
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 DIGITS[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";;
11+
12+
const long int size1 = 1000000;
13+
char inputString[size1 * 2];
14+
15+
// ---------------------------
16+
// Renamed arrays
17+
// ---------------------------
18+
unsigned int integerWork[size1] = {0};
19+
unsigned int fractionWork[size1] = {0};
20+
unsigned int fractionOut[size1] = {0};
21+
22+
// --------------------------------------------------
23+
void dis(long int n, long int f = -1, long int k1 = 0, long int num = 0)
24+
{
25+
long int k;
26+
27+
if (k1 != 0 && num != 0)
28+
cout << k1 << "-" << n+1 << "-|";
29+
30+
for (k = n; k >= 0; k--)
31+
cout << DIGITS[ integerWork[k] ];
32+
33+
// Only print fractional part if f >= 0 (meaning some fractional digits were produced)
34+
if (f >= 0 && !(f == 0 && fractionOut[0] == 0))
35+
{
36+
cout << ".";
37+
for (k = 0; k <= f; k++)
38+
cout << DIGITS[ fractionOut[k] ];
39+
}
40+
cout << endl;
41+
}
42+
// --------------------------------------------------
43+
44+
int main()
45+
{
46+
fstream input, output;
47+
clock_t start;
48+
double duration;
49+
50+
long int rows = size1, trows;
51+
long int i, j, k, p;
52+
53+
long int o, m = 0, m1 = 0, l;
54+
55+
long int base1, base2, maxDigitBase2;
56+
long int integerInputLen = 0;
57+
long int fractionInputLen = 0;
58+
59+
long int highestIntPos;
60+
long int fracLen; // will hold final fractional length (can be -1 if none)
61+
62+
long int fractionLimit = 100;
63+
64+
int choice = 0, temp, numtemp;
65+
int save, value = 0;
66+
67+
long int intLenProcessed, carryIndex;
68+
69+
base1 = 10;
70+
base2 = 16;
71+
72+
maxDigitBase2 = base2 - 1;
73+
trows = rows;
74+
save = 0;
75+
76+
begining:
77+
78+
rows = trows;
79+
80+
// clear buffers
81+
for (p = 0; p < rows; p++)
82+
{
83+
inputString[p] = 0;
84+
integerWork[p] = 0;
85+
fractionWork[p] = 0;
86+
fractionOut[p] = 0;
87+
}
88+
89+
cout << "________________________________________________________________________________________________________________" << endl;
90+
cout << " DATA REPRESENTATION" << endl;
91+
92+
cout << "1.CONVERT A (BASE1=" << base1 << ") TO (BASE2=" << base2 << ") \n ( FRACTION LIMIT =" << fractionLimit << " SAVE OUTPUT=";
93+
if (save == 1) cout << "YES" << endl;
94+
else cout << "NO" << endl;
95+
96+
cout << "2.CHANGE THE BASE , FRACTION LIMIT & SAVING OUTPUT?" << endl;
97+
cout << "3.CLEAR SCREEN " << endl;
98+
cout << "4.HELP " << endl;
99+
cout << "5.EXIT " << endl;
100+
cout << "ENTER YOUR CHOICE = ";
101+
cin >> choice;
102+
103+
if (choice == 1)
104+
goto case1_start;
105+
else if (choice == 2)
106+
{
107+
cout << "BASE1 = ";
108+
cin >> base1;
109+
cout << "BASE2 = ";
110+
cin >> base2;
111+
cout << "FRACTION LIMIT = ";
112+
cin >> fractionLimit;
113+
cout << "SAVE OUTPUT (1=YES 0=NO) = ";
114+
cin >> save;
115+
goto begining;
116+
}
117+
else if (choice == 3)
118+
{
119+
system("cls");
120+
goto begining;
121+
}
122+
else if (choice == 4)
123+
{
124+
cout << " *.OUTPUT SAVED IN \"output.txt\" IF ENABLED.\n";
125+
cout << " *.INPUT FOR FILE MODE MUST BE IN \"input.txt\"\n";
126+
goto begining;
127+
}
128+
else if (choice == 5)
129+
{
130+
exit(0);
131+
}
132+
else
133+
{
134+
cout << "INVALID CHOICE\n";
135+
goto begining;
136+
}
137+
138+
case1_start:
139+
140+
cout << " 1.INPUT CONSOLE 2.INPUT FILE\nENTER = ";
141+
cin >> choice;
142+
143+
if (choice == 1)
144+
{
145+
cout << "ENTER NUMBER (base" << base1 << "): "<<endl;
146+
cin >> inputString;
147+
}
148+
else if (choice == 2)
149+
{
150+
input.open("input.txt", ios::in);
151+
input >> inputString;
152+
cout << inputString << endl;
153+
input.close();
154+
}
155+
else
156+
{
157+
cout << "INVALID\n";
158+
goto case1_start;
159+
}
160+
161+
integerInputLen = strlen(inputString);
162+
cout << "LENGTH = " << integerInputLen << endl;
163+
164+
if (integerInputLen > (long)sizeof(inputString))
165+
{
166+
cout << "STRING SIZE LIMIT = " << sizeof(inputString) << endl;
167+
goto case1_start;
168+
}
169+
170+
// find decimal point
171+
for (p = 0; p < integerInputLen; p++)
172+
if (inputString[p] == '.')
173+
break;
174+
175+
// convert ASCII to numeric (in-place)
176+
for (i = 0; i < integerInputLen; i++)
177+
{
178+
if (inputString[i] > 57) // A-F etc
179+
inputString[i] -= 7;
180+
inputString[i] -= 48; // '0'
181+
}
182+
183+
// copy fraction part reverse into fractionWork[]
184+
k = 0;
185+
for (i = integerInputLen - 1; i > p; i--)
186+
fractionWork[k++] = inputString[i];
187+
188+
cout << "please wait(processing...)" << endl;
189+
190+
start = std::clock();
191+
192+
fractionInputLen = integerInputLen - p;
193+
integerInputLen = p;
194+
195+
carryIndex = 0;
196+
fracLen = -1;
197+
intLenProcessed = 1;
198+
199+
// ---------------------------------------------------
200+
// INTEGER PART CONVERSION
201+
// ---------------------------------------------------
202+
for (k = 0; k < integerInputLen; k++)
203+
{
204+
for (p = 0; (integerWork[p] *= base1), p < intLenProcessed; p++) {}
205+
206+
integerWork[0] += inputString[k];
207+
208+
for (i = 0; i < intLenProcessed; i++)
209+
{
210+
long int idx = i;
211+
212+
while (integerWork[idx] > maxDigitBase2)
213+
{
214+
if (carryIndex < idx) carryIndex = idx;
215+
integerWork[idx+1] += integerWork[idx] / base2;
216+
integerWork[idx] %= base2;
217+
idx++;
218+
}
219+
}
220+
221+
intLenProcessed = carryIndex + 2;
222+
}
223+
224+
// ---------------------------------------------------
225+
// FRACTIONAL PART
226+
// ---------------------------------------------------
227+
long int fractionResultIndex = -1;
228+
bool fractionNotZero = true;
229+
230+
// Only run fractional loop if there was a fractional part in the input
231+
if (fractionInputLen > 0)
232+
{
233+
do
234+
{
235+
for (p = 0; p < fractionInputLen; p++)
236+
{
237+
fractionWork[p] *= base2;
238+
}
239+
for (i = 0; i < fractionInputLen - 1; i++)
240+
{
241+
fractionWork[i + 1] += fractionWork[i] / base1;
242+
fractionWork[i] %= base1;
243+
}
244+
245+
// store extracted digit
246+
fractionOut[++fractionResultIndex] = fractionWork[fractionInputLen - 1];
247+
248+
// CORRECT: clear working buffer (not fractionOut)
249+
fractionWork[fractionInputLen - 1] = 0;
250+
251+
fractionNotZero = false;
252+
for (p = 0; p < fractionInputLen; p++)
253+
{
254+
if (fractionWork[p] != 0)
255+
{
256+
fractionNotZero = true;
257+
break;
258+
}
259+
}
260+
}
261+
while (fractionNotZero && fractionResultIndex < fractionLimit - 1);
262+
}
263+
264+
// set final fracLen to produced index (can be -1 if no fraction produced)
265+
fracLen = fractionResultIndex;
266+
267+
duration = (std::clock() - start) / (double) CLOCKS_PER_SEC;
268+
269+
highestIntPos = 0;
270+
for (p = 0; p < rows; p++)
271+
if (integerWork[p] > 0)
272+
highestIntPos = p;
273+
274+
cout << endl << "EQUIVALENT TO (base)" << base2 << endl;
275+
dis(highestIntPos, fracLen);
276+
277+
if (save == 1)
278+
{
279+
for (i = 0; i < integerInputLen; i++)
280+
inputString[i] = 0;
281+
282+
p = 0;
283+
for (i = highestIntPos; i >= 0; i--)
284+
inputString[p++] = DIGITS[ integerWork[i] ];
285+
286+
if (!(fracLen == 0 && fractionOut[0] == 0) && fracLen >= 0)
287+
{
288+
inputString[p++] = '.';
289+
for (k = 0; k <= fracLen; k++)
290+
inputString[p++] = DIGITS[ fractionOut[k] ];
291+
}
292+
293+
output.open("output.txt", ios::out);
294+
output << inputString;
295+
output.close();
296+
}
297+
298+
cout << "TIME TAKEN = " << duration << endl;
299+
goto begining;
300+
301+
return 0;
302+
}

0 commit comments

Comments
 (0)