// filename : number_system.cpp
// author : ancktion
// Create date : 2008/05/17
//
// description :
// this code function is make for to change the numbers from one number_system to other number_system. Example: dec number system to hex number system
// Log:
//
// reversion : 0.21 2008/05/20 ancktion
// 修正进制转换的显示方式:
// 1.正数的转换后,显示前面总会有一个0元素开头,否则就不在前面显示0元素(注意:八进制的数可能也会在前面加个零)
// 这样可以明显区后那些是正数,那些是负数
// 2.添加了反码与原码的显示方式(在一些教程书中只介绍了二进制的等码),这里将沿伸到其他的编码
//
// reversion : 0.2 2008/05/18 ancktion
// extended those function support negative
//
// reversion : 0.1 2008/05/17 ancktion
// added function create
#include "stdafx.h"
#include "number_system.h"
#include <iostream> // include std::out and std::in
void chg_DecToBin( IO_IN const long int& nDec,
IO_OUT char** chBin/* = NULL*/,
IO_IN bool bGetCh/* = false*/,
IO_IN eNumView eView/* = eNumView_CPLCode*/,
IO_IN eNumView eCurCode/* = eNumView_CPLCode*/)
{
short int iCharCount = 0;
_chg_DecToBin((unsigned long int)_DecCodeChage(nDec,eView,eCurCode),
nDec<0 ? 1 : 0,
chBin,
bGetCh,
&iCharCount);
}
bool chg_DecToBin( IO_IN const char* cDec,
IO_OUT char** chBin/* = NULL*/,
IO_IN bool bGetCh/* = false*/,
IO_IN eNumView eView /*= eNumView_CPLCode*/,
IO_IN eNumView eCurCode/* = eNumView_CPLCode*/)
{
if( !_chek_StrIsNumSys(cDec))
return false;
long int nDec = 0;
nDec = _strToDec(cDec);
chg_DecToBin( nDec,chBin,bGetCh,eView,eCurCode);
return true;
}
//-------------------------------------------------------------------------
bool chg_NumSysToDec( IO_IN const char* cNumSys,
short int nSys,
IO_IN_OUT long int& nDec,
IO_OUT char** chDec/* = NULL*/,
IO_IN bool bGetCh /*= false*/,
IO_IN eNumView eGetCode/* = eNumView_CPLCode*/,
IO_IN eNumView eCurCode/* = eNumView_CPLCode*/)
{
if( !_chek_StrIsNumSys(cNumSys,nSys) )
return 0;
nDec = _strToDec(cNumSys,nSys);
nDec = _DecCodeChage(nDec,eGetCode,eCurCode);
if(bGetCh)
_chg_DecToStr(nDec,chDec);
return true;
}
//-------------------------------------------------------------------------
bool chg_DecToNumSys( IO_IN const long int& nDec,
IO_IN short int nSys,
IO_OUT char** chNumSys/* = NULL*/,
IO_IN bool bGetCh/* = false*/,
IO_IN eNumView eView/* = eNumView_CPLCode*/,
IO_IN eNumView eCurCode/* = eNumView_CPLCode*/)
{
short int iCharCount = 0;
_chg_DecToNumSys((unsigned long int)_DecCodeChage(nDec,eView,eCurCode),
nSys,
nDec < 0 ? 1 : 0,
chNumSys,
bGetCh,
&iCharCount);
return true;
}
bool chg_DecToNumSys( IO_IN const char* cDec,
IO_IN short int nSys,
IO_OUT char** chNumSys/* = NULL*/,
IO_IN bool bGetCh /*= false*/,
IO_IN eNumView eView /*= eNumView_CPLCode*/,
IO_IN eNumView eCurCode/* = eNumView_CPLCode*/)
{
if( !_chek_StrIsNumSys(cDec))
return false;
long int nDec = 0;
nDec = _strToDec(cDec);
return chg_DecToNumSys(nDec,nSys,chNumSys,bGetCh,eView,eCurCode);
}
//************************************************************************************************************************************
void _chg_DecToBin( IO_IN const unsigned long int& nDec,
IO_IN bool bNeg/* = 0*/,
IO_OUT char** chBin/* = NULL*/,
IO_IN bool bGetCh/* = false*/,
IO_IN short int* iCharCount/* = NULL*/,
IO_IN short int iCallLay/* = 0*/
)
{
if( 0 == nDec)
{
if( 0 == iCallLay)
{ // for input nDec is Zero
*iCharCount = iCallLay + 2;
std::cout << "0";
if( bGetCh )
*chBin = new char[ *iCharCount ];
(*chBin)[0] = '0';
(*chBin)[1] = '\0';
} // if( 0 == iCallLay)
else
{
if(bNeg == 1)
*iCharCount = iCallLay + 1;
else
*iCharCount = iCallLay + 2;
if( bGetCh )
*chBin = new char[ *iCharCount ];
if (bNeg != 1)
{
(*chBin)[0] = '0';
std::cout<<'0';
}
}
return;
} // if( 0 == nDec)
short int iRe = (short int)(nDec % 2);
_chg_DecToBin( nDec / 2,bNeg,chBin,bGetCh,iCharCount,iCallLay + 1);
std::cout << (char)('0' + iRe);
if(bGetCh)
(*chBin)[*iCharCount - iCallLay - 2] = (char)('0' + iRe);
if( 0 == iCallLay)
if(bGetCh)
(*chBin)[*iCharCount - 1] = '\0';
}
void _chg_DecToNumSys( IO_IN const unsigned long int& nDec,
IO_IN short int nSys,
IO_IN bool bNeg/* = 0*/,
IO_OUT char** chNumSys/* = NULL*/,
IO_IN bool bGetCh/* = false*/,
IO_IN short int* iCharCount/* = NULL*/,
IO_IN short int iCallLay/* = 0*/)
{
if( 0 == nDec)
{
if( 0 == iCallLay)
{ // for input nDec is Zero
*iCharCount = iCallLay + 2;
std::cout << "0";
if( bGetCh )
*chNumSys = new char[ *iCharCount ];
(*chNumSys)[0] = '0';
(*chNumSys)[1] = '\0';
} // if( 0 == iCallLay)
else
{
if (bNeg == 1)
*iCharCount = iCallLay + 1;
else
*iCharCount = iCallLay + 2;
if( bGetCh )
*chNumSys = new char[ *iCharCount ];
if (bNeg != 1)
{
(*chNumSys)[0] = '0';
std::cout<<'0';
}
}
return;
} // if( 0 == nDec)
short int iRe = (short int)(nDec % nSys);
_chg_DecToNumSys( nDec / nSys,nSys,bNeg,chNumSys,bGetCh,iCharCount,iCallLay + 1);
char c = ' ';
if(iRe < 10)
c = (char)('0' + iRe);
else
{
iRe = iRe - 10;
c = (char)('A' + iRe);
}
std::cout << c;
(*chNumSys)[*iCharCount - iCallLay - 2] = c;
if( 0 == iCallLay)
if(bGetCh)
(*chNumSys)[*iCharCount - 1] = '\0';
}
//把十进制整数转换为字符串,不使用STL
void _chg_DecToStr( IO_IN long int nDec,IO_OUT char** chDec/* = NULL*/)
{
if( 0 == nDec )
{
*chDec = new char[2];
(*chDec)[0] = '0';
(*chDec)[1] = '\0';
}
long int nReDec = 0;
short int iChCount = 1;
short int iSigned = nDec > 0 ? 1 : -1;
nDec = nDec > 0 ? nDec : -nDec;
while(nDec)
{
nReDec = nReDec * 10 + (nDec % 10);
nDec /= 10;
iChCount ++;
}
if(iSigned < 0)
iChCount ++;
*chDec = new char[iChCount];
iChCount = 0;
if(iSigned < 0)
{
(*chDec)[iChCount] = '-';
iChCount ++;
}
while(nReDec)
{
(*chDec)[iChCount] = (char)('0' + (nReDec % 10));
nReDec /= 10;
iChCount ++;
}
(*chDec)[iChCount] = '\0';
}
bool _chek_StrIsDec( IO_IN const char* cDec )
{
if( NULL == cDec )
return false;
short int iCount = 0;
while( cDec[iCount] != '\0' )
{
if(iCount == 0)
if(cDec[iCount] == '-' || cDec[iCount] == '+')
{
iCount ++;
continue;
}
if( cDec[iCount] > '9' || cDec[iCount] < '0')
return false;
iCount ++;
}
return true;
}
//调用时请确保使用的进制在16进制内
bool _chek_StrIsNumSys( IO_IN const char* cNumSys,IO_IN unsigned short int nSys/* = 10*/)
{
if( NULL == cNumSys )
return false;
short int iCount = 0;
while( cNumSys[iCount] != '\0' )
{
if(iCount == 0)
if(cNumSys[iCount] == '-' || cNumSys[iCount] == '+')
{
iCount ++;
continue;
}
if(cNumSys[iCount] < '0')
goto _error;
if( nSys <= 10 )
{
if( cNumSys[iCount] > '0' + nSys - 1)
goto _error;
}//if( nSys <= 10 )
else
{
if( (cNumSys[iCount] > '9' && cNumSys[iCount] < 'A' ) ||
(cNumSys[iCount] > 'A' + nSys - 10 - 1 && cNumSys[iCount] < 'a') ||
(cNumSys[iCount] > 'a' + nSys - 10 - 1)
)
goto _error;
}
iCount ++;
}
return true;
_error:
std::cout<<"字符串非法!不是指定进制数字符串!\n";
return false;
}
//nSys可以是2,8,10,16等数,但希望不能超过16进制,cDec是否合法,请在函数外检查
long int _strToDec( IO_IN const char* cDec,IO_IN unsigned short int nSys/* = 10*/)
{
long int iDec = 0;
short int iCount = 0;
short int iSigned = 1;
if(cDec[iCount] == '-')
数制互转换v0.21(支持补、原、反)
需积分: 9 83 浏览量
2008-05-20
16:54:37
上传
评论
收藏 4KB RAR 举报
ancktion
- 粉丝: 2
- 资源: 40
最新资源
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
- C语言实现直接插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序、归并排序、计数排序,并带图详解.zip
- 常用工具集参考用于图像等数据处理
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈