Advertisement
MouseyN1

Base converter \w menu & steps (base-10 to base-x)

Oct 22nd, 2015
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. float getDecimalPart(float number){
  7.   int I = (int)number;
  8.   number -= (float) I;
  9.   return number;
  10. }
  11.  
  12. void convertNumberToBase(float initialNumber, int base, bool trace)
  13. {
  14.     int i, pos(0), integerPart;
  15.     char finalNumber[100];
  16.     char baseRange[37] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  17.     double dec(0);
  18.     if(getDecimalPart(initialNumber)) {
  19.         dec = getDecimalPart(initialNumber);
  20.     }
  21.     if(trace)
  22.     {
  23.         cout << "\n\nIn order to change a number's base from 10 to " << base << "\nwe need to compute repeated divisions of said number to the specified base.";
  24.         cout << "\nAt the same time, we need to keep the remainder of every division in memory.\nAfter the numerator becomes 0 (there are no more divisions to be computed)";
  25.         cout << "\nwe get the number in a different base by putting together every remainder\nin the opposite order of which they were computed.\n";
  26.     }
  27.     integerPart = (int)initialNumber;
  28.     while(integerPart)
  29.     {
  30.         if(trace) {
  31.             cout << "\nWe divide " << integerPart << " by " << base << ". Result: " << integerPart / base << ". Remainder: " << integerPart % base << " (" << baseRange[integerPart % base] << ')';
  32.         }
  33.         finalNumber[pos++] = baseRange[integerPart % base];
  34.         integerPart = integerPart / base;
  35.     }
  36.     finalNumber[pos] = '\0';
  37.     if(!dec) {
  38.         if(trace) cout << "\nThe program stops. We now take the remainders from end to start.";
  39.         cout << "\nThe converted number is: " << strrev(finalNumber);
  40.     }
  41.     else {
  42.         if(trace)
  43.         {
  44.             cout << "\n\nIn order to change the base of the decimal part, we multiply it with the base.\n";
  45.             cout << "If the result is greater than 0 we put the integer part aside and we continue\nmultiplying";
  46.             cout << " until the decimal part is 0.\n";
  47.             cout << "This time, we take the integer parts in the order that they were computed.\n\n";
  48.             cout << "WARNING! If the decimal part is periodical/infinite, the program will only show\n";
  49.             cout << "the first 10 digits.\n\n";
  50.         }
  51.         pos = 0;
  52.         char decimalPoints[15];
  53.         while(dec)
  54.         {
  55.             if(trace) cout << dec << " multiplied by " << base << " is " << dec * base << " (" << (int)(dec * base) << ")\n";
  56.             dec *= base;
  57.             if(dec > 0)
  58.                 decimalPoints[pos++] = baseRange[(int)dec];
  59.             dec -= (int)dec;
  60.             if(pos > 9) {
  61.                     cout << "The result after the decimal point might not be exact.\n\n";
  62.                     break;
  63.             }
  64.  
  65.         }
  66.         decimalPoints[pos] = '\0';
  67.         cout << "\nThe converted number is: " << strrev(finalNumber) << '.' << decimalPoints;
  68.     }
  69. }
  70.  
  71. int main()
  72. {
  73.     int menuOption;
  74.     float initNr;
  75.     int base;
  76.     bool trace = true;
  77.     cout << "Welcome to the base converter program!\n";
  78.     cout << "This program takes your base-10 input and converts it\n";
  79.     cout << "to another base (2 - 37).\nDecimal points are supported.\n\n";
  80.     do {
  81.         cout << "\n\n1. Enter number\n";
  82.         cout << "2. Display progress (default: yes) (current: ";
  83.         trace?cout << "yes)":cout << "no)";
  84.         cout << "\n3. Exit program\n";
  85.         cout << "Chosen option: ";
  86.         cin >> menuOption;
  87.         switch(menuOption)
  88.         {
  89.             case 1:
  90.                 cout << "\nEnter number to convert: ";
  91.                 cin >> initNr;
  92.                 cout << "Enter desired base: ";
  93.                 cin >> base;
  94.                 if(base < 2 || base > 37) {
  95.                     cout << "Out of bounds. Desired base must be within 2 and 37.";
  96.                     break;
  97.                 }
  98.                 else if(base == 10)
  99.                     cout << "The number is already in base 10.";
  100.             //    else cout << "The converted number is: ";
  101.                 convertNumberToBase(initNr, base, trace);
  102.                 break;
  103.             case 2:
  104.                 trace = !trace;
  105.                 if(!trace) cout << "Progress will no longer be displayed.";
  106.                 else cout << "Progress will now be displayed.";
  107.                 break;
  108.             case 3:
  109.                 return 0;
  110.                 break;
  111.             default: cout << "\nInvalid selection.";
  112.         }
  113.         }while(menuOption != 3);
  114.         return 0;
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement