Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- float getDecimalPart(float number){
- int I = (int)number;
- number -= (float) I;
- return number;
- }
- void convertNumberToBase(float initialNumber, int base, bool trace)
- {
- int i, pos(0), integerPart;
- char finalNumber[100];
- char baseRange[37] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- double dec(0);
- if(getDecimalPart(initialNumber)) {
- dec = getDecimalPart(initialNumber);
- }
- if(trace)
- {
- 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.";
- 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)";
- cout << "\nwe get the number in a different base by putting together every remainder\nin the opposite order of which they were computed.\n";
- }
- integerPart = (int)initialNumber;
- while(integerPart)
- {
- if(trace) {
- cout << "\nWe divide " << integerPart << " by " << base << ". Result: " << integerPart / base << ". Remainder: " << integerPart % base << " (" << baseRange[integerPart % base] << ')';
- }
- finalNumber[pos++] = baseRange[integerPart % base];
- integerPart = integerPart / base;
- }
- finalNumber[pos] = '\0';
- if(!dec) {
- if(trace) cout << "\nThe program stops. We now take the remainders from end to start.";
- cout << "\nThe converted number is: " << strrev(finalNumber);
- }
- else {
- if(trace)
- {
- cout << "\n\nIn order to change the base of the decimal part, we multiply it with the base.\n";
- cout << "If the result is greater than 0 we put the integer part aside and we continue\nmultiplying";
- cout << " until the decimal part is 0.\n";
- cout << "This time, we take the integer parts in the order that they were computed.\n\n";
- cout << "WARNING! If the decimal part is periodical/infinite, the program will only show\n";
- cout << "the first 10 digits.\n\n";
- }
- pos = 0;
- char decimalPoints[15];
- while(dec)
- {
- if(trace) cout << dec << " multiplied by " << base << " is " << dec * base << " (" << (int)(dec * base) << ")\n";
- dec *= base;
- if(dec > 0)
- decimalPoints[pos++] = baseRange[(int)dec];
- dec -= (int)dec;
- if(pos > 9) {
- cout << "The result after the decimal point might not be exact.\n\n";
- break;
- }
- }
- decimalPoints[pos] = '\0';
- cout << "\nThe converted number is: " << strrev(finalNumber) << '.' << decimalPoints;
- }
- }
- int main()
- {
- int menuOption;
- float initNr;
- int base;
- bool trace = true;
- cout << "Welcome to the base converter program!\n";
- cout << "This program takes your base-10 input and converts it\n";
- cout << "to another base (2 - 37).\nDecimal points are supported.\n\n";
- do {
- cout << "\n\n1. Enter number\n";
- cout << "2. Display progress (default: yes) (current: ";
- trace?cout << "yes)":cout << "no)";
- cout << "\n3. Exit program\n";
- cout << "Chosen option: ";
- cin >> menuOption;
- switch(menuOption)
- {
- case 1:
- cout << "\nEnter number to convert: ";
- cin >> initNr;
- cout << "Enter desired base: ";
- cin >> base;
- if(base < 2 || base > 37) {
- cout << "Out of bounds. Desired base must be within 2 and 37.";
- break;
- }
- else if(base == 10)
- cout << "The number is already in base 10.";
- // else cout << "The converted number is: ";
- convertNumberToBase(initNr, base, trace);
- break;
- case 2:
- trace = !trace;
- if(!trace) cout << "Progress will no longer be displayed.";
- else cout << "Progress will now be displayed.";
- break;
- case 3:
- return 0;
- break;
- default: cout << "\nInvalid selection.";
- }
- }while(menuOption != 3);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement