|
| 1 | +#include <cmath> |
1 | 2 | #include <string> |
2 | 3 |
|
3 | 4 | #include "test_framework/generic_test.h" |
4 | 5 | using std::string; |
5 | 6 |
|
| 7 | +/* |
| 8 | +- num can be between int max and min |
| 9 | +- int can be negative, and may have prefixes |
| 10 | +- 0 is valid in any base |
| 11 | +- b1, b2 are 2-16 |
| 12 | +- converting bin string to dec int: multiply each digit by power of two, sum and |
| 13 | +return |
| 14 | +- converting dec int to bin string: left to right starting from closest pow of 2 |
| 15 | +less than num: # set bit and subtract value. |
| 16 | +*/ |
| 17 | + |
| 18 | +#define ASCII_ZERO 48 |
| 19 | +#define ASCII_A_LOWER 97 |
| 20 | +#define ASCII_A_UPPER 65 |
| 21 | + |
| 22 | +int digitToInt(const char digit) { |
| 23 | + if ('0' <= digit && digit <= '9') { |
| 24 | + return digit - ASCII_ZERO; |
| 25 | + } else if ('A' <= digit && digit <= 'F') { |
| 26 | + return digit - ASCII_A_UPPER + 10; |
| 27 | + } else if ('a' <= digit && digit <= 'f') { |
| 28 | + return digit - ASCII_A_LOWER + 10; |
| 29 | + } |
| 30 | + return -1; |
| 31 | +} |
| 32 | + |
| 33 | +char intToDigit(const int val) { |
| 34 | + if (0 <= val && val <= 9) { |
| 35 | + return val + ASCII_ZERO; |
| 36 | + } else if (0xA <= val && val <= 0xF) { |
| 37 | + return val - 10 + ASCII_A_UPPER; |
| 38 | + } |
| 39 | + return '!'; |
| 40 | +} |
| 41 | + |
| 42 | +int stringToInt(const string& val, int oldBase) { |
| 43 | + bool neg = val[0] == '-'; |
| 44 | + int total = 0; |
| 45 | + int pow = 1; |
| 46 | + for (int i = val.size() - 1; i >= (neg ? 1 : 0); i--) { |
| 47 | + total += (digitToInt(val[i]) * pow); |
| 48 | + pow *= oldBase; |
| 49 | + } |
| 50 | + return total * (neg ? -1 : 1); |
| 51 | +} |
| 52 | +string intToString(int value, int newBase) { |
| 53 | + if (!value) { |
| 54 | + return "0"; |
| 55 | + } |
| 56 | + bool neg = value < 0; |
| 57 | + value = abs(value); |
| 58 | + |
| 59 | + string representation = neg ? "-" : ""; |
| 60 | + long exp = 0; |
| 61 | + long sym = 1; |
| 62 | + while ((sym * long(pow(newBase, exp))) <= value) { |
| 63 | + exp++; |
| 64 | + } |
| 65 | + exp--; |
| 66 | + |
| 67 | + while (value) { |
| 68 | + sym = newBase - 1; |
| 69 | + while ((sym * int(pow(newBase, exp))) > value && sym > 0) { |
| 70 | + sym--; |
| 71 | + } |
| 72 | + representation += intToDigit(sym); |
| 73 | + value -= (sym * int(pow(newBase, exp))); |
| 74 | + exp--; |
| 75 | + } |
| 76 | + while (exp >= 0) { |
| 77 | + representation += '0'; |
| 78 | + exp--; |
| 79 | + } |
| 80 | + |
| 81 | + return representation; |
| 82 | +} |
| 83 | + |
6 | 84 | string ConvertBase(const string& num_as_string, int b1, int b2) { |
7 | | - // TODO - you fill in here. |
8 | | - return ""; |
| 85 | + int base10Val = stringToInt(num_as_string, b1); |
| 86 | + string newVal = intToString(base10Val, b2); |
| 87 | + return newVal; |
9 | 88 | } |
10 | 89 |
|
11 | 90 | int main(int argc, char* argv[]) { |
|
0 commit comments