#include <iostream> // C++
#include <ostream> // std::endl
#include <string>
// ------------------------------------
class Manoj{
public: // ------------------------------ public
Manoj():
charTable("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
{} // Ctor
// ------------------------------------
void execute( int base, std:
stream &cout=std::cout){
cout<<"\n___[ base = "<<base<<" ]___"<<std::endl;
// ------------
for(int i(-55); i <= 55; ++i){
cout << i <<" = "<<CalcBase( i, base )<<std::endl;
} // for(i)
// ------------
cout<<"___[ Done ]___\n"<<std::endl;
} //execute(int,std:
stream&)
// ------------------------------------
private: // ------------------------------ private
std::string charTable;
// ------------------------------------
std::string CalcBase( int i, int base ){
using std::abs;
if((abs(base) <= 1) || (abs(base) > 36) || (i == 0)){
return "0";
}
std::string str("");
std::string negate("");
if((base > 0) && (i < 0)){
negate = "-";
i = -i;
}
do{
if(i % base >= 0){
str += charTable.at( i % base );
i = i / base;
}
else{
str += charTable.at( i % base - base );
i = i /base + 1;
}
} while(i);
std::reverse(str.begin(), str.end());
str = negate + str;
// Will this be ok? Try it
// if (str[0] == '-')
// std::reverse(str.begin()+1,str.end());
return str;
} //CalcBase(int,int)
// ------------------------------------
}; //class Manoj
// ------------------------------------
#include <fstream> // std::endl
int main(){
Manoj Man;
Man.execute( 10, std::cout);
Man.execute( 16 );
ofstream File("MyTest.txt");
if( !File ){
std::cerr<<"ofstream File FAILURE!!"<<std::endl;
return EXIT_FAILURE;
}
for(int i( -36 ); i <= 36; ++i){
Man.execute( i, File );
} // for(i)
return 0;
} // main()end
// ------------------------------------