This is another one of my numerical analysis university assignments. It is an extension of binary to decimal conversion code. The program converts a number in any base to a number in any other base. The user inputs the original number, the base it was written in and the base it wants to convert to. It then outputs the number in the converted base.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | /******************************************************* * MYCPLUS Sample Code - https://www.mycplus.com * * * * This code is made available as a service to our * * visitors and is provided strictly for the * * purpose of illustration. * * * * Please direct all inquiries to saqib at mycplus.com * *******************************************************/ #include <iostream.h> #include <conio.h> #include <string.h> #include <math.h> int Validate(char[], int, int); // returns if a valid number was typed in void ConvToDec(char[], int, int, int); // changes original number into a decimal void EvaluateWhole(long, int); void EvaluateFract(double, int); const int MAXSTRING = 30 ; void main() { char old_string[MAXSTRING]; int original_base, new_base ; clrscr(); cout << "\nPlease enter the number you would like to convert" << " (max-30 characters): " ; cin.getline(old_string, MAXSTRING, '\n') ; cout << "\nPlease enter the base you were writing in(eg.2-binary): " ; cin >> original_base ; if (!Validate(old_string, original_base, strlen(old_string))) cout << "\nThe number you entered did not match with the base you specified!" ; else { cout << "\nPlease enter the base you want to convert to: " ; cin >> new_base ; ConvToDec(old_string, original_base, new_base, strlen(old_string)); } getch(); }//Begin int Validate(char str[], int base, int length) { int aski_val ; aski_val = '0' + base ; if (base>10) aski_val += ('A' - '9') -1; // Adjust so 'A' comes immediately after 9 for (int i=0; i<length; i++) { if (str[i] >= aski_val) return 0; // invalid } return 1; // valid }//Validate void ConvToDec(char str[], int base, int new_base, int length) { double dec_frac =0; long dec_num =0; int dec_digit =0; for (int i=0; i<length && str[i]!='.'; i++) { dec_digit = str[i] - '0'; if (str[i]>='A') dec_digit -= ('A' - '9') -1; // Adjust so 'A' comes immediately after 9 dec_num = dec_digit + dec_num*base; } EvaluateWhole(dec_num, new_base); int index; if (str[i] == '.') { for (index = --length ; index > i; index--) { dec_digit = str[index] - '0'; if (str[index]>='A') dec_digit -= ('A' -'9') -1; // Adjust so 'A' comes immediately after 9 dec_frac = (dec_frac + dec_digit)/base; } EvaluateFract(dec_frac, new_base); } }//ConvToDec void EvaluateWhole(long DecWhole, int base) { char new_string[MAXSTRING] ; int i = 0, aski_val; while (DecWhole > 0) { aski_val = DecWhole % base + '0'; if (DecWhole%base > 9) aski_val += ('A' - '9') -1; // Adjust so 'A' comes immediately after 9 new_string[i] = aski_val ; DecWhole = DecWhole/base ; i++; } cout << "\nThe number converted is " ; for (int j=i-1; j>=0; j--) cout << new_string[j] ; }//Evaluate void EvaluateFract(double DecFrac, int base) { char FracDigit ; // 4 places int Digit ; cout << "." ; // decimal point for (int i=0; i<10; i++) // 6 decimal places { DecFrac = DecFrac * base ; // Bring out digit after point Digit = DecFrac ; DecFrac -= Digit; FracDigit = Digit + '0' ; if (base>10 && FracDigit>'9') FracDigit += ('A' - '9' - 1) ; cout << FracDigit ; } } |