Number Conversions

This is another one of my numerical analysis university assignments. It is an extension of binary to decimal conversion code. The program converts a number in any base to a number in any other base. The user inputs the original number, the base it was written in and the base it wants to convert to. It then outputs the number in the converted base.

/*******************************************************
*     MYCPLUS Sample Code - https://www.mycplus.com     *
*                                                     *
*   This code is made available as a service to our   *
*      visitors and is provided strictly for the      *
*               purpose of illustration.              *
*                                                     *
* Please direct all inquiries to saqib at mycplus.com *
*******************************************************/
#include 
#include 
#include 
#include 

int Validate(char[], int, int);     // returns if a valid number was typed in
void ConvToDec(char[], int, int, int);    // changes original number into a decimal
void EvaluateWhole(long, int);
void EvaluateFract(double, int);

const int MAXSTRING = 30 ;

void main()
{
  char old_string[MAXSTRING];
  int original_base, new_base ;

  clrscr();
  cout << "\nPlease enter the number you would like to convert"
   << " (max-30 characters): " ;
  cin.getline(old_string, MAXSTRING, '\n') ;
  cout << "\nPlease enter the base you were writing in(eg.2-binary): " ;
  cin >> original_base ;

  if (!Validate(old_string, original_base, strlen(old_string)))
  cout << "\nThe number you entered did not match with the base you specified!" ;
  else
  {
  cout << "\nPlease enter the base you want to convert to: " ;
  cin >> new_base ;
  ConvToDec(old_string, original_base, new_base, strlen(old_string));
  }
  getch();
}//Begin

int Validate(char str[], int base, int length)
{
  int aski_val ;

  aski_val = '0' + base ;
  if (base>10)
  aski_val += ('A' - '9') -1;      // Adjust so 'A' comes immediately after 9

  for (int i=0; i= aski_val)
  return 0;     // invalid
  }
  return 1;   // valid
}//Validate

void ConvToDec(char str[], int base, int new_base, int length)
{
  double dec_frac =0;
  long dec_num =0;
  int dec_digit =0;

  for (int i=0; i='A')
  dec_digit -= ('A' - '9') -1;   // Adjust so 'A' comes immediately after 9
  dec_num = dec_digit + dec_num*base;
  }

  EvaluateWhole(dec_num, new_base);

  int index;

  if (str[i] == '.')
  {
  for (index = --length ; index > i; index--)
  {
  dec_digit = str[index] - '0';
  if (str[index]>='A')
    dec_digit -= ('A' -'9') -1;  // Adjust so 'A' comes immediately after 9
  dec_frac = (dec_frac + dec_digit)/base;
  }
  EvaluateFract(dec_frac, new_base);
  }
}//ConvToDec

void EvaluateWhole(long DecWhole, int base)
{
  char new_string[MAXSTRING] ;
  int i = 0, aski_val;

  while (DecWhole > 0)
  {
  aski_val = DecWhole % base + '0';
  if (DecWhole%base > 9)
  aski_val += ('A' - '9') -1;   // Adjust so 'A' comes immediately after 9
  new_string[i] = aski_val ;
  DecWhole = DecWhole/base ;
  i++;
  }

  cout << "\nThe number converted is " ;
  for (int j=i-1; j>=0; j--)
  cout << new_string[j] ;
}//Evaluate

void EvaluateFract(double DecFrac, int base)
{
  char FracDigit ;  // 4 places
  int Digit ;

  cout << "." ;    // decimal point
  for (int i=0; i<10; i++)    // 6 decimal places
  {
  DecFrac = DecFrac * base ;    // Bring out digit after point
  Digit = DecFrac ;
  DecFrac -= Digit;

  FracDigit =  Digit + '0' ;
  if (base>10 && FracDigit>'9')
  FracDigit += ('A' - '9' - 1) ;
  cout << FracDigit ;
  }
}
M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post