📄 numberconverter.cpp
字号:
#*******************************************************
# MYCPLUS Sample Code - http://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 <iostream.h>
#include <conio.h>
#include <string.h>
#include <math.h>
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<length; i++)
{
if (str[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<length && str[i]!='.'; i++)
{
dec_digit = str[i] - '0';
if (str[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 ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -