Skip to content

Commit 38575cd

Browse files
authored
Update BaseConversionAlgorithm V2.cpp
Redable version of V1
1 parent 3f142c6 commit 38575cd

File tree

1 file changed

+16
-28
lines changed

1 file changed

+16
-28
lines changed

BaseConversionAlgorithm V2.cpp

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,20 @@ using namespace std;
99

1010
char DIGITS[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";;
1111

12-
const long int size1 = 1000000;
13-
char inputString[size1 * 2];
12+
const long int MAX = 1000000;
13+
char inputString[MAX * 2];
1414

1515
// ---------------------------
1616
// Renamed arrays
1717
// ---------------------------
18-
unsigned int integerWork[size1] = {0};
19-
unsigned int fractionWork[size1] = {0};
20-
unsigned int fractionOut[size1] = {0};
18+
unsigned int integerWork[MAX] = {0};
19+
unsigned int fractionWork[MAX] = {0};
20+
unsigned int fractionOut[MAX] = {0};
2121

2222
// --------------------------------------------------
23-
void dis(long int n, long int f = -1, long int k1 = 0, long int num = 0)
23+
void display(long int n, long int f)
2424
{
2525
long int k;
26-
27-
if (k1 != 0 && num != 0)
28-
cout << k1 << "-" << n+1 << "-|";
29-
3026
for (k = n; k >= 0; k--)
3127
cout << DIGITS[ integerWork[k] ];
3228

@@ -47,10 +43,8 @@ int main()
4743
clock_t start;
4844
double duration;
4945

50-
long int rows = size1, trows;
51-
long int i, j, k, p;
46+
long int i, k, p;
5247

53-
long int o, m = 0, m1 = 0, l;
5448

5549
long int base1, base2, maxDigitBase2;
5650
long int integerInputLen = 0;
@@ -61,24 +55,21 @@ int main()
6155

6256
long int fractionLimit = 100;
6357

64-
int choice = 0, temp, numtemp;
65-
int save, value = 0;
58+
int choice = 0,save=0;
6659

6760
long int intLenProcessed, carryIndex;
6861

6962
base1 = 10;
70-
base2 = 16;
63+
base2 = 2;
7164

7265
maxDigitBase2 = base2 - 1;
73-
trows = rows;
74-
save = 0;
7566

7667
begining:
7768

78-
rows = trows;
69+
7970

8071
// clear buffers
81-
for (p = 0; p < rows; p++)
72+
for (p = 0; p < MAX; p++)
8273
{
8374
inputString[p] = 0;
8475
integerWork[p] = 0;
@@ -192,7 +183,7 @@ int main()
192183
fractionInputLen = integerInputLen - p;
193184
integerInputLen = p;
194185

195-
carryIndex = 0;
186+
carryIndex = 1;
196187
fracLen = -1;
197188
intLenProcessed = 1;
198189

@@ -208,17 +199,14 @@ int main()
208199
for (i = 0; i < intLenProcessed; i++)
209200
{
210201
long int idx = i;
211-
212202
while (integerWork[idx] > maxDigitBase2)
213203
{
214-
if (carryIndex < idx) carryIndex = idx;
215204
integerWork[idx+1] += integerWork[idx] / base2;
216205
integerWork[idx] %= base2;
217-
idx++;
206+
if (carryIndex < ++idx) carryIndex = idx;
218207
}
219208
}
220-
221-
intLenProcessed = carryIndex + 2;
209+
intLenProcessed = carryIndex;
222210
}
223211

224212
// ---------------------------------------------------
@@ -267,12 +255,12 @@ int main()
267255
duration = (std::clock() - start) / (double) CLOCKS_PER_SEC;
268256

269257
highestIntPos = 0;
270-
for (p = 0; p < rows; p++)
258+
for (p = 0; p < MAX; p++)
271259
if (integerWork[p] > 0)
272260
highestIntPos = p;
273261

274262
cout << endl << "EQUIVALENT TO (base)" << base2 << endl;
275-
dis(highestIntPos, fracLen);
263+
display(highestIntPos, fracLen);
276264

277265
if (save == 1)
278266
{

0 commit comments

Comments
 (0)