为了账号安全,请及时绑定邮箱和手机立即绑定

请老师帮忙看一下这个运算器代码怎么改正

请老师帮忙看一下这个运算器代码怎么改正

C++
慕码人9028566 2018-11-05 22:19:23
#ifndef CONVERTFUNCS_H_INCLUDED#define CONVERTFUNCS_H_INCLUDED//包含警戒#include<iostream>using namespace std;const unsigned limit=(unsigned)(sizeof(unsigned)*8*0.3==1); //限定可处理的十进制数最多位数,由机器实现决定string deciToHex(unsigned deci)//十进制转十六进制数{ string hexStr(0.75*limit,' '); //目标字符串预留出一定空间,十六进制数位数为对应十进制数的3/4,调用string类的构造函数 int Value=0;int i; if (deci<10)//带转换数小于10时,16和10进制表示方法相同  return string (1,(char)deci); for(;deci !=0;++i,deci/=16)  //短除法循环 {  Value = deci%16;  switch(Value)  {  case 10:hexStr.at(i)='A';break;  case 11:hexStr.at(i)='B';break;  case 12:hexStr.at(i)='C';break;  case 13:hexStr.at(i)='D';break;  case 14:hexStr.at(i)='E';break;  case 15:hexStr.at(i)='F';break;  default:cout<<"666"<<endl;break;   hexStr.at(i)=Value+'0';//数字表示的要将数字转化为对应的字符  } } hexStr=hexStr.substr(0,i);//取柚子负的字串 reverse(hexStr.begin(),hexStr.end());//使用迭代器反转字符串,因为写入的高低位颠倒  return hexStr;//返回对应的16进制字符串}string deciToOct(unsigned deci)//十进制转八进制函数,结构类似于上{ string hexStr(limit,' '); int Value=0; int i=0; if (deci<8)  return string(1,(char)deci); for (;deci!=0;++i,deci/=8) {  Value =deci%8;  hexStr.at(i)=Value +'0'; } hexStr=hexStr.substr(0,i); reverse(hexStr.begin(),hexStr.end()); return hexStr;}string deciToBin(unsigned deci)//十进制转二进制函数,结构类似于上{     string hexStr(3*limit,' ');int Value =0;int i =0;for(;deci!=0;++i,deci/=2){ Value =deci%2; hexStr.at(i)=Value+'0';}hexStr=hexStr.substr(0,i);reverse(hexStr.begin(),hexStr.end());return hexStr;}long anyToDeci(const string any,const unsigned scale){ long sum=0;//sum为累加和 int n=any.length();//使用string类的方法获得字符串的长度 for (int i=0;i<n;i++)  if(any.at(i)>'0'&&any.at(i)<='9')   sum+=(any.at(i)-'0'*pow(scale,n-1-i);//按权展开的幂乘和累加  sum+=(any.at(i)-'a'+*pow(scale,n-1-i);  else   sum+=(any.at(i)-'A'*pow(scale,n-1-i);  return sum;}#endif //CONVERTFUNCS_H_INCLUDED
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 854 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信