M
manoj1978
Hi All,
I wrote the following to print an integer in its string
representation for base -36 to 36.
Please comment on this code.
#include <iostream>
#include <string>
using std::abs;
using std::cout;
using std::endl;
using std::reverse;
using std::string;
char charTable[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
void printBase(int i, int base)
{
if ((abs(base) <= 1) || (abs(base) > 36) || (i == 0))
{
cout << "0" << endl;
return;
}
string str = "";
if ((base > 0) && (i < 0))
{
str += "-";
i = -i;
}
do {
if (i % base >= 0)
{
str += charTable[i % base];
i = i / base;
} else {
str += charTable[i % base - base];
i = i /base + 1;
}
} while(i);
reverse(str.begin(), str.end());
cout << str << endl;
}
int main()
{
for(int i = -55; i <= 55; i++)
{
cout << i << " = ";
printBase(i,-10);
}
}
Thanks and Regards,
manoj.
I wrote the following to print an integer in its string
representation for base -36 to 36.
Please comment on this code.
#include <iostream>
#include <string>
using std::abs;
using std::cout;
using std::endl;
using std::reverse;
using std::string;
char charTable[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
void printBase(int i, int base)
{
if ((abs(base) <= 1) || (abs(base) > 36) || (i == 0))
{
cout << "0" << endl;
return;
}
string str = "";
if ((base > 0) && (i < 0))
{
str += "-";
i = -i;
}
do {
if (i % base >= 0)
{
str += charTable[i % base];
i = i / base;
} else {
str += charTable[i % base - base];
i = i /base + 1;
}
} while(i);
reverse(str.begin(), str.end());
cout << str << endl;
}
int main()
{
for(int i = -55; i <= 55; i++)
{
cout << i << " = ";
printBase(i,-10);
}
}
Thanks and Regards,
manoj.