admin管理员组

文章数量:1794759

vs C++ 进制转换器设计

vs C++ 进制转换器设计

设计思路:

1. 文本框限制数字和字符输入,只接受有效数字和字符;

2. 字符串转数字、字符转数字及反向处理;

局限性:

本文只考虑了long 类型以内数据进制转换,不适用大数据

具体实现过程:

头文件:

//进制类型

enum NUMSYSTEM{ BIN, OCT, DEC, HEX };

 //从当前类型转换至numSys类型  void ConvertTo(NUMSYSTEM numSys);  //从任意进制到十进制的转换  void ToDecimal(CString& strNum ,NUMSYSTEM numSys);  //从十进制到任意进制的转换  void DecimalTo(CString& strNum ,NUMSYSTEM numSys);  //去除数字前面的0  void RemoveZeroBeforeNum(CString &strNum);  //求幂函数  long Power(int aNum, int bNum);  //数字到字符  int CharToNum(TCHAR ch);  //字符到数字  TCHAR NumToChar(int nNum);

实现:

//类型转换函数 void CCalculatorDlg::ConvertTo(NUMSYSTEM numSys) {  CString strNum;  GetDlgItemText(IDC_EDIT_NUM, strNum);   if (!strNum.IsEmpty())  {   RemoveZeroBeforeNum(strNum);   ToDecimal(strNum, m_numSys);   switch (numSys)   {   case BIN:    DecimalTo(strNum, BIN);    break;   case OCT:    DecimalTo(strNum, OCT);    break;   case HEX:    DecimalTo(strNum, HEX);    break;   default:    break;   }  }  RemoveZeroBeforeNum(strNum);  SetDlgItemText(IDC_EDIT_NUM, strNum); }

void CCalculatorDlg::ToDecimal(CString& strNum, NUMSYSTEM numSys) {  int nCount = strNum.GetLength();  int nBase = 0;  long nRet = 0;  switch (numSys)  {  case BIN:     nBase = 2;   break;  case OCT:   nBase = 8;   break;  case HEX:   nBase = 16;   break;  default:   break;  }  if (0 == nBase)return;  while (nCount>0)  {   nRet += CharToNum(strNum.GetAt(0))*Power(nBase, nCount - 1);   strNum.Delete(0);   nCount--;  }  strNum.Format(_T("%d"), nRet); }

void CCalculatorDlg::DecimalTo(CString& strNum, NUMSYSTEM numSys) {  long nNum = _ttoi(strNum);  strNum.Empty();  int nBase = 0;  int nRet = 0;//余数  switch (numSys)  {  case BIN:   nBase = 2;   break;  case OCT:   nBase = 8;   break;  case HEX:   nBase = 16;   break;  default:   break;  }  if (0 == nBase)return;  while (nNum>1)  {   nRet = nNum%nBase;   nNum = nNum / nBase;   strNum+=NumToChar(nRet);  }  strNum += NumToChar(nNum);  strNum.MakeReverse(); }

void CCalculatorDlg::RemoveZeroBeforeNum(CString &strNum) {  while (0==strNum.Find(_T("0")))  {   strNum.Delete(0);  } }

//求nNum的nCount次幂 long CCalculatorDlg::Power(int nNum, int nCount) {  long nRet = 1;  while (nCount > 0)  {   nRet *= nNum;   nCount--;  }  return nRet; } 

//类型转换函数 void CCalculatorDlg::ConvertTo(NUMSYSTEM numSys) {  CString strNum;  GetDlgItemText(IDC_EDIT_NUM, strNum);   if (!strNum.IsEmpty())  {   RemoveZeroBeforeNum(strNum);   ToDecimal(strNum, m_numSys);   switch (numSys)   {   case BIN:    DecimalTo(strNum, BIN);    break;   case OCT:    DecimalTo(strNum, OCT);    break;   case HEX:    DecimalTo(strNum, HEX);    break;   default:    break;   }  }  RemoveZeroBeforeNum(strNum);  SetDlgItemText(IDC_EDIT_NUM, strNum); }

//字符转数字 int CCalculatorDlg::CharToNum(TCHAR ch) {  switch (ch)  {  case '0':   return 0;  case '1':   return 1;  case '2':   return 2;  case '3':   return 3;  case '4':   return 4;  case '5':   return 5;  case '6':   return 6;  case '7':   return 7;  case '8':   return 8;  case '9':   return 9;  case 'a':  case 'A':   return 10;  case 'b':  case 'B':   return 11;  case 'c':  case 'C':   return 12;  case 'd':  case 'D':   return 13;  case 'e':  case 'E':   return 14;  case 'f':  case 'F':   return 15;  default:   return NULL;  } }

//数字转字符 TCHAR CCalculatorDlg::NumToChar(int nNum) {  switch (nNum)  {  case 0:   return '0';  case 1:   return '1';  case 2:   return '2';  case 3:   return '3';  case 4:   return '4';  case 5:   return '5';  case 6:   return '6';  case 7:   return '7';  case 8:   return '8';  case 9:   return '9';  case 10:   return 'A';  case 11:   return 'B';  case 12:   return 'C';  case 13:   return 'D';  case 14:   return 'E';  case 15:   return 'F';  default:   return NULL;  } }



本文标签: 转换器