C++ 任意进制的转换

更新时间:2023-05-18 14:17:21 阅读: 评论:0

// NumSysTransform.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <string>
using namespace std;
bool TestNumStrIsOK(const int _n,const string &_strNum);/*测试_strNum是否为合法的_n进制*/
long TransNbaNumStrToDicmal(const int _n,const string &_strNum);/*将_n进制的字符创_strNum转化为十进制数返回*/
int  TransCharToDicmal(const char _aNum);/*讲一个字符转化为数值*/
string TransNBaNumToMBaNum(const int _srcN, const string& _strSrcNum,const int _desN);/*将_srcN进制的数_strSrcNum转化中_desN进制并返回*/
string DecimalToNbaNumStr(long _srcNum,const int _n);/*将十进制数转化为_n进制*/
char TransDigitNumToChar(const int _digitValue);/*将n进制中的一个位的数值转化为字符*/
bool TestNbaIsOK(const int _n);/*测试_n进制是不是合法(  2<=n<=36)*/
int main(int argc, char* argv[])
{
long dec;
int  baN,baM;
string strN,strM;
cout<<"请输入一个十进制数:"<<endl;
cin>>dec;
cout<<"请输入需要转化的进制数(N):"<<endl;
cin>>baN;
strN = DecimalToNbaNumStr(dec,baN);
cout<<"#"<<baN<<":"<<strN<<endl;
cout<<"请输入需要转化的进制数(M):"<<endl;
cin>>baM;
strM = DecimalToNbaNumStr(dec,baN);
cout<<"#"<<baN<<":"<<strM<<endl;
cout<<"将(N)进制转化成M进制:"<<endl;
strM = TransNBaNumToMBaNum(baN,strN,baM);
cout<<"#"<<baN<<":"<<strN<<"==>>"<<"#"<<baN<<":"<<strM<<endl;
return 0;
}
/
*测试_strNum是否为合法的_n进制*/
bool TestNumStrIsOK(const int _n,const string &_strNum)
{
/*标识*/
bool flag = true;
int  digitValue = 0;/*表示n进制中一个位的十进制大小*/
for (int i = 0 ; i < _strNum.length(); i++ )
{
if (('0' <= _strNum[i] && _strNum[i] <= '9') ||
('A' <= _strNum[i] && _strNum[i] <= 'Z'))
{
名著阅读
digitValue = TransCharToDicmal(_strNum[i]);;
}
belle knoxel
{
flag = fal;
break;
}
if (digitValue < _n)
{
continue;
}
el
{
flag = fal;
break;
}
}
return flag;
}
/*将_n进制的字符创_strNum转化为十进制数返回*/
long TransNbaNumStrToDicmal(const int _n,const string &_strNum)
{
long result = 0;
long nMult = 1;/*相当于实际值的1,10,100的进位标识*/
long    digitValue = 0;
if (TestNumStrIsOK(_n,_strNum))
sitv
{
/*将_strNum转化为十进制*/
for ( int i = _strNum.length() - 1 ; i >= 0; i -- )
{
digitValue = TransCharToDicmal(_strNum[i]);
result += digitValue * nMult;
nMult *= _n;
}
}
el
{
return 0;
}
return result;
}
/*将一个字符转化为数值*/
int  TransCharToDicmal(const char _aNum)
{
亲爱的英文单词
int  digitValue = 0;/*表示n进制中一个位的十进制大小*/
if ( '0' <=  _aNum && _aNum <= '9')
{
digitValue =  _aNum - '0';
}
el if ('A' <= _aNum &&_aNum <= 'Z')
{
digitValue = _aNum - 'A' + 10;
}
return
digitValue;
}
/*将_srcN进制的数_strSrcNum转化中_desN进制并返回*/
string TransNBaNumToMBaNum(const int _srcN, const string& _strSrcNum,const int _desN)
{日本人不知道的日语
string strResult;
if ( (_srcN < 2 || _srcN > 36) ||(_desN < 2 || _desN > 36))
{
return strResult;
}
long srcNum = 0;
if (TestNumStrIsOK(_srcN, _strSrcNum))
{
srcNum  = TransNbaNumStrToDicmal(_srcN,_strSrcNum);
}
return DecimalToNbaNumStr(srcNum,_desN);
}
maisie williams
/*将十进制数转化为_n进制*/
string DecimalToNbaNumStr(long _srcNum,const int _n)
{
string strResult;
if (!TestNbaIsOK(_n))
{
return strResult;
}
do
{
strResult += TransDigitNumToChar(_srcNum % _n);
_srcNum /= _n;
} while (_srcNum);
char temp;
int  leftIndex = 0;
int  rightIndex = 0;
/*倒序*/
for (int i = 0; i < strResult.length() / 2;i++)
{
leftIndex = i;
rightIndex = strResult.length() - i - 1;
temp = strResult[leftIndex];
strResult[leftIndex] = strResult[rightIndex];本科转学出国
strResult[rightIndex] = temp;
}
return strResult;
}
/*将n进制中的一个位的数值转化为字符*/
char  TransDigitNumToChar(const int _digitValue)
{
if (_digitValue >= 36)
{
return ' ';
}
if (_digitValue >= 0 && _digitValue <= 9)
{
return '0' + _digitValue;
}
if (_digitValue >= 10 && _digitValue <= 35)
{
return 'A' + (_digitValue - 10);
}
return ' ';
}
/*测试_n进制是不是合法(  2<=n<=36)*/
bool TestNbaIsOK(const int _n)
我搭车{
if (_n >=2 && _n <=36)
{
cnn chinareturn true;
}
postmessage
return fal;
}

本文发布于:2023-05-18 14:17:21,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/682360.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:进制   转化   字符   阅读   相当于   进位   知道
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图