|
| 1 | +#include<iostream> |
| 2 | +#include<string.h> |
| 3 | +#include<stdio.h> |
| 4 | +#include<stdlib.h> |
| 5 | + |
| 6 | + |
| 7 | +using namespace std; |
| 8 | + |
| 9 | +char DIGITS[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";; |
| 10 | + |
| 11 | +const long int MAX = 1000000; |
| 12 | +char inputString[MAX * 2]; |
| 13 | + |
| 14 | +// --------------------------- |
| 15 | +// Renamed arrays |
| 16 | +// --------------------------- |
| 17 | +unsigned int integerWork[MAX] = {0}; |
| 18 | +unsigned int fractionWork[MAX] = {0}; |
| 19 | +unsigned int fractionOut[MAX] = {0}; |
| 20 | + |
| 21 | +// -------------------------------------------------- |
| 22 | +void display(long int n, long int f) |
| 23 | +{ |
| 24 | + long int k; |
| 25 | + for (k = n; k >= 0; k--) |
| 26 | + cout << DIGITS[ integerWork[k] ]; |
| 27 | + |
| 28 | + // Only print fractional part if f >= 0 (meaning some fractional digits were produced) |
| 29 | + if (f >= 0 && !(f == 0 && fractionOut[0] == 0)) |
| 30 | + { |
| 31 | + cout << "."; |
| 32 | + for (k = 0; k <= f; k++) |
| 33 | + cout << DIGITS[ fractionOut[k] ]; |
| 34 | + } |
| 35 | + cout << endl; |
| 36 | +} |
| 37 | +// -------------------------------------------------- |
| 38 | + |
| 39 | +int main() |
| 40 | +{ |
| 41 | + |
| 42 | + long int i, k, p; |
| 43 | + |
| 44 | + |
| 45 | + long int base1, base2, maxDigitBase2; |
| 46 | + long int integerInputLen = 0; |
| 47 | + long int fractionInputLen = 0; |
| 48 | + |
| 49 | + long int highestIntPos; |
| 50 | + long int fracLen; // will hold final fractional length (can be -1 if none) |
| 51 | + |
| 52 | + long int fractionLimit = 100; |
| 53 | + |
| 54 | + int choice = 0; |
| 55 | + |
| 56 | + long int intLenProcessed, carryIndex; |
| 57 | + |
| 58 | + base1 = 10; |
| 59 | + base2 = 2; |
| 60 | + |
| 61 | + maxDigitBase2 = base2 - 1; |
| 62 | + |
| 63 | +begining: |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + // clear buffers |
| 68 | + for (p = 0; p < MAX; p++) |
| 69 | + { |
| 70 | + inputString[p] = 0; |
| 71 | + integerWork[p] = 0; |
| 72 | + fractionWork[p] = 0; |
| 73 | + fractionOut[p] = 0; |
| 74 | + } |
| 75 | + |
| 76 | + cout << "________________________________________________________________________________________________________________" << endl; |
| 77 | + cout << " DATA REPRESENTATION" << endl; |
| 78 | + cout << "1.CONVERT BASE1=" << base1 << " TO BASE2=" << base2 << " \n FRACTION LIMIT =" << fractionLimit <<endl; |
| 79 | + cout << "2.CHANGE THE BASE , FRACTION LIMIT & SAVING OUTPUT?" << endl; |
| 80 | + cout << "3.CLEAR SCREEN " << endl; |
| 81 | + cout << "4.EXIT " << endl; |
| 82 | + cout << "ENTER YOUR CHOICE = "; |
| 83 | + cin >> choice; |
| 84 | + |
| 85 | + if (choice == 1) |
| 86 | + goto case1_start; |
| 87 | + else if (choice == 2) |
| 88 | + { |
| 89 | + cout << "BASE1 = "; |
| 90 | + cin >> base1; |
| 91 | + cout << "BASE2 = "; |
| 92 | + cin >> base2; |
| 93 | + cout << "FRACTION LIMIT = "; |
| 94 | + cin >> fractionLimit; |
| 95 | + goto begining; |
| 96 | + } |
| 97 | + else if (choice == 3) |
| 98 | + { |
| 99 | + system("cls"); |
| 100 | + goto begining; |
| 101 | + } |
| 102 | + else if (choice == 4) |
| 103 | + { |
| 104 | + exit(0); |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + cout << "INVALID CHOICE\n"; |
| 109 | + goto begining; |
| 110 | + } |
| 111 | + |
| 112 | +case1_start: |
| 113 | + |
| 114 | + cout << "ENTER NUMBER (base" << base1 << "): "<<endl; |
| 115 | + cin >> inputString; |
| 116 | + |
| 117 | + integerInputLen = strlen(inputString); |
| 118 | + cout << "LENGTH = " << integerInputLen << endl; |
| 119 | + |
| 120 | + // find decimal point |
| 121 | + for (p = 0; p < integerInputLen; p++) |
| 122 | + if (inputString[p] == '.') |
| 123 | + break; |
| 124 | + |
| 125 | + // convert ASCII to numeric (in-place) |
| 126 | + for (i = 0; i < integerInputLen; i++) |
| 127 | + { |
| 128 | + if (inputString[i] > 57) // A-F etc |
| 129 | + inputString[i] -= 7; |
| 130 | + inputString[i] -= 48; // '0' |
| 131 | + } |
| 132 | + |
| 133 | + // copy fraction part reverse into fractionWork[] |
| 134 | + k = 0; |
| 135 | + for (i = integerInputLen - 1; i > p; i--) |
| 136 | + fractionWork[k++] = inputString[i]; |
| 137 | + |
| 138 | + cout << "please wait(processing...)" << endl; |
| 139 | + |
| 140 | + fractionInputLen = integerInputLen - p; |
| 141 | + integerInputLen = p; |
| 142 | + |
| 143 | + carryIndex = 1; |
| 144 | + fracLen = -1; |
| 145 | + intLenProcessed = 1; |
| 146 | + |
| 147 | + // --------------------------------------------------- |
| 148 | + // INTEGER PART CONVERSION |
| 149 | + // --------------------------------------------------- |
| 150 | + for (k = 0; k < integerInputLen; k++) |
| 151 | + { |
| 152 | + for (p = 0; (integerWork[p] *= base1), p < intLenProcessed; p++) {} |
| 153 | + |
| 154 | + integerWork[0] += inputString[k]; |
| 155 | + |
| 156 | + for (i = 0; i < intLenProcessed; i++) |
| 157 | + { |
| 158 | + long int idx = i; |
| 159 | + while (integerWork[idx] > maxDigitBase2) |
| 160 | + { |
| 161 | + integerWork[idx+1] += integerWork[idx] / base2; |
| 162 | + integerWork[idx] %= base2; |
| 163 | + if (carryIndex < ++idx) carryIndex = idx; |
| 164 | + } |
| 165 | + } |
| 166 | + intLenProcessed = carryIndex; |
| 167 | + } |
| 168 | + |
| 169 | + // --------------------------------------------------- |
| 170 | + // FRACTIONAL PART |
| 171 | + // --------------------------------------------------- |
| 172 | + long int fractionResultIndex = -1; |
| 173 | + bool fractionNotZero = true; |
| 174 | + |
| 175 | + // Only run fractional loop if there was a fractional part in the input |
| 176 | + if (fractionInputLen > 0) |
| 177 | + { |
| 178 | + do |
| 179 | + { |
| 180 | + for (p = 0; p < fractionInputLen; p++) |
| 181 | + { |
| 182 | + fractionWork[p] *= base2; |
| 183 | + } |
| 184 | + for (i = 0; i < fractionInputLen - 1; i++) |
| 185 | + { |
| 186 | + fractionWork[i + 1] += fractionWork[i] / base1; |
| 187 | + fractionWork[i] %= base1; |
| 188 | + } |
| 189 | + |
| 190 | + // store extracted digit |
| 191 | + fractionOut[++fractionResultIndex] = fractionWork[fractionInputLen - 1]; |
| 192 | + |
| 193 | + // CORRECT: clear working buffer (not fractionOut) |
| 194 | + fractionWork[fractionInputLen - 1] = 0; |
| 195 | + |
| 196 | + fractionNotZero = false; |
| 197 | + for (p = 0; p < fractionInputLen; p++) |
| 198 | + { |
| 199 | + if (fractionWork[p] != 0) |
| 200 | + { |
| 201 | + fractionNotZero = true; |
| 202 | + break; |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + while (fractionNotZero && fractionResultIndex < fractionLimit - 1); |
| 207 | + } |
| 208 | + |
| 209 | + // set final fracLen to produced index (can be -1 if no fraction produced) |
| 210 | + fracLen = fractionResultIndex; |
| 211 | + |
| 212 | + highestIntPos = 0; |
| 213 | + for (p = 0; p < MAX; p++) |
| 214 | + if (integerWork[p] > 0) |
| 215 | + highestIntPos = p; |
| 216 | + |
| 217 | + cout << endl << "EQUIVALENT TO (base)" << base2 << endl; |
| 218 | + display(highestIntPos, fracLen); |
| 219 | + |
| 220 | + goto begining; |
| 221 | + |
| 222 | + return 0; |
| 223 | +} |
0 commit comments