Harryviu
September 17th, 2012, 09:56 PM
#include <iostream>
using namespace std;
//PROTOTYPES
void decimal2Base(int num, int base, char *result, int offset = 0);
int main()
{
** //LOCAL DECLARATIONS
** int num;
** int base;
** char result[20];
** while (true)
** {
***** cout << "Enter number: ";
***** cin >> num;
***** if (num < 0) break;
***** cout << "Enter base: ";
***** cin >> base;
***** if (base > 36 || base < 2) break;
***** decimal2Base(num, base, result);
***** cout << result << endl;
***** cout << endl;
** }
** cout << "\nEnd - Press enter to exit . . . ";
** cin.sync();
** cin.get();
** return 0;
}
//---------------------------------------------------------
// FUNCTION DEFINITIONS
//---------------------------------------------------------
void decimal2Base(int num, int base, char * result, int offset)
{
** if (base > 36 || base < 2)
***** result = "NaN";
** else
** {
***** int digitVal = num % base;
***** *result = (digitVal < 10 ? digitVal + '0' : digitVal - 10 + 'A');
***** if (num >= base)
******** decimal2Base(num / base, base, result + 1, offset + 1);
***** else
***** {
******** result[1] = '\0';
******** result -= offset;
******** //get result's length
******** int len = 0;
******** for (char *ptr = result; *ptr; ptr++)
*********** len++;
******** //copy result to a temporary string
******** char *temp = new char[len + 1];
******** for (char *ptr = result, *tempPtr = temp; *ptr; ptr++, tempPtr++)
*********** *tempPtr = *ptr;
******** //copy back to result in reversed order
******** for (int i = len - 1; i >= 0; i--)
*********** result[len - 1 - i] = temp[i];
******** //free memory of the temporary string
******** delete [] temp;
***** }
** }
}
using namespace std;
//PROTOTYPES
void decimal2Base(int num, int base, char *result, int offset = 0);
int main()
{
** //LOCAL DECLARATIONS
** int num;
** int base;
** char result[20];
** while (true)
** {
***** cout << "Enter number: ";
***** cin >> num;
***** if (num < 0) break;
***** cout << "Enter base: ";
***** cin >> base;
***** if (base > 36 || base < 2) break;
***** decimal2Base(num, base, result);
***** cout << result << endl;
***** cout << endl;
** }
** cout << "\nEnd - Press enter to exit . . . ";
** cin.sync();
** cin.get();
** return 0;
}
//---------------------------------------------------------
// FUNCTION DEFINITIONS
//---------------------------------------------------------
void decimal2Base(int num, int base, char * result, int offset)
{
** if (base > 36 || base < 2)
***** result = "NaN";
** else
** {
***** int digitVal = num % base;
***** *result = (digitVal < 10 ? digitVal + '0' : digitVal - 10 + 'A');
***** if (num >= base)
******** decimal2Base(num / base, base, result + 1, offset + 1);
***** else
***** {
******** result[1] = '\0';
******** result -= offset;
******** //get result's length
******** int len = 0;
******** for (char *ptr = result; *ptr; ptr++)
*********** len++;
******** //copy result to a temporary string
******** char *temp = new char[len + 1];
******** for (char *ptr = result, *tempPtr = temp; *ptr; ptr++, tempPtr++)
*********** *tempPtr = *ptr;
******** //copy back to result in reversed order
******** for (int i = len - 1; i >= 0; i--)
*********** result[len - 1 - i] = temp[i];
******** //free memory of the temporary string
******** delete [] temp;
***** }
** }
}