Hey guys I have an assignment due in 3 days(Wednesday). The program is supposed to take a number from any base and be able to display it in any base as well.The methods of conversion between bases MUST BE RECURSIVE. I know there are some functions out there that let you do this easily, anyhow my professor is strictly asking for recursive methods. I have managed to make a class for each base (decimal, binary, octal, hexadecimal) and I'm storing the number in a string in order to validate it (which means that somehow I have to turn that string into an integer and I don't know how. I was trying to use the strtoll function to convert a string into a long long int but I don't know how to do this since the num doesn't exist at compile time so I guess I have to do some magic here that I still don't quite understand ). Now I need to print out all bases with that number using a recursive method. I want to start on the decimal number class, converting from decimal to: octal, hexadecimal and binary. Since binary is the easiest I want to precisely start here.
So after getting through the constructor I have a string but I need that value in an integer.
I'm asking for a way to convert the string into a long long int.
Enoizat, I appreciate your answer but I am asking for guidance, not estimates on how I emotionally feel towards the project.
Back to the subject at hand. I know how to convert a defined string to a long long int but I don't know how to do it here because the string is not defined at compile time (since it has to go through the constructor first). Can someone shine a light here? here's some more code. UNType is the base class for decType, hexType, binType, octType. I am working on decType. Any suggestions are very much appreciated.
//UNType.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#pragma once
#include "string"
#include "iostream"
#include "stdlib.h" //I think i'll need this for using stroll function
usingnamespace std;
class UNType
{
public:
string virtual toHex() = 0;
string virtual toDec() = 0;
string virtual toOct() = 0;
string virtual toBin() = 0;
protected:
string num;
};
//decType.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#pragma once
#include "UNType.h"
constint decMax_chars = 18;
class decType :
public UNType
{
public:
decType(string);
string toHex();
string toDec();
string toOct();
string toBin();
longlongint outNum;
char numCh[decMax_chars];
};
#include <iostream>
#include <cctype>
#include <string>
usingnamespace std;
//==========================================
// Integer to char conversion and vice versa
//==========================================
char symbol( int i )
{
if ( i < 10 ) return (char)( '0' + i );
elsereturn (char)( 'A' + i - 10 );
}
int lobmys( char c )
{
if ( isdigit( c ) ) return c - '0';
elsereturn 10 + toupper( c ) - 'A';
}
//===================
// Recursion routines
//===================
string intToBase( int n, int base ) { return n > 0 ? intToBase ( n / base , base ) + symbol( n % base ) : ""; }
int BaseToInt( string s, int base ) { return s != "" ? base * BaseToInt( s.substr( 0, s.size() - 1 ), base ) + lobmys( s[s.size()-1] ) : 0 ; }
//=========
// Checking
//=========
int main()
{
int n, base;
string s;
cout << "Convert a decimal integer to an arbitrary base\n";
cout << "Enter a positive integer: "; cin >> n ;
cout << "Enter a base (2-16): " ; cin >> base;
cout << n << " in base " << base << " is " << intToBase( n, base ) << endl;
cout << "\n\n";
cout << "Convert a string in an arbitrary base to a decimal integer\n";
cout << "Enter a string: " ; cin >> s;
cout << "Enter a base (2-16): " ; cin >> base;
cout << s << " in base " << base << " is " << BaseToInt( s, base ) << " in base 10" << endl;;
}
Convert a decimal integer to an arbitrary base
Enter a positive integer: 255
Enter a base (2-16): 16
255 in base 16 is FF
Convert a string in an arbitrary base to a decimal integer
Enter a string: FF
Enter a base (2-16): 16
FF in base 16 is 255 in base 10