#ifndef CONVERTFUNCS_H_INCLUDED#define CONVERTFUNCS_H_INCLUDED//包含警戒#include<iostream>using namespace std;const unsigned limit=(unsigned)(sizeof(unsigned)*8*0.3==1); //限定可處理的十進(jìn)制數(shù)最多位數(shù),由機(jī)器實(shí)現(xiàn)決定string deciToHex(unsigned deci)//十進(jìn)制轉(zhuǎn)十六進(jìn)制數(shù){?string hexStr(0.75*limit,' ');?//目標(biāo)字符串預(yù)留出一定空間,十六進(jìn)制數(shù)位數(shù)為對(duì)應(yīng)十進(jìn)制數(shù)的3/4,調(diào)用string類的構(gòu)造函數(shù)?int Value=0;int i;?if (deci<10)//帶轉(zhuǎn)換數(shù)小于10時(shí),16和10進(jìn)制表示方法相同??return string (1,(char)deci);?for(;deci !=0;++i,deci/=16)??//短除法循環(huán)?{??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';//數(shù)字表示的要將數(shù)字轉(zhuǎn)化為對(duì)應(yīng)的字符??}?}?hexStr=hexStr.substr(0,i);//取柚子負(fù)的字串?reverse(hexStr.begin(),hexStr.end());//使用迭代器反轉(zhuǎn)字符串,因?yàn)閷懭氲母叩臀活嵉??return hexStr;//返回對(duì)應(yīng)的16進(jìn)制字符串}string deciToOct(unsigned deci)//十進(jìn)制轉(zhuǎn)八進(jìn)制函數(shù),結(jié)構(gòu)類似于上{?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)//十進(jìn)制轉(zhuǎn)二進(jìn)制函數(shù),結(jié)構(gòu)類似于上{ ??? 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類的方法獲得字符串的長(zhǎng)度?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);//按權(quán)展開(kā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
請(qǐng)老師幫忙看一下這個(gè)運(yùn)算器代碼怎么改正
慕碼人9028566
2018-11-05 22:19:23