c++ 大数的加减乘除

来源:互联网 发布:男朋友是男神体验知乎 编辑:程序博客网 时间:2024/04/27 21:25


bigdigialoperation.h

#ifndef BIGDIGITALOPERATION_H#define BIGDIGITALOPERATION_H#include<iostream>#include<string>using namespace std;class BigDigitalOperation{ public:  /*比较大小:1则前者大,0相等,-1后者大*/  static int cmp(const string&s1,const string &s2);  /*把字符行s转化为int型数组*/  static void convert(int *a,const string &s);  /*相减res=s1-s2*/ static  void sub(const string &s1,const string &s2,string &res);  /*相乘res=s1*s2*/  static void multiplication(const string&s1,const string &s2,string &res);  /*相除 s1/s2=res 余数为mod*/  static void divide(const string &s1,const string &s2,string &res,string &mod);  /*大数转化为对应的二进制,res为s1的二进制表示*/  static void  toBin(const string&s1, string &res);  /*计算res=(s1^s2)%s3*/  static void quitCalculate(const string &s1,const string &s2,const string &s3,string &res);  static string toValue(const std::string & key);  static string toKey(const std::string& value);  static void mod(const string &s1,const string &s2,string &mod);};#endif 


bigdigialoperation.cpp

#include"bigdigialoperation.h"#include<map> int BigDigitalOperation::cmp(const string & s1,const string & s2){  if(s1.size()>s2.size())    return 1;  else if(s1.size()<s2.size())    return -1;  else     {      for(int i=0;i<s1.size();++i)        {          if(s1[i]>s2[i])return 1;          if(s1[i]<s2[i])return -1;        }    }  return 0;}void BigDigitalOperation::convert(int*a,const string&s){  int pos=0;  int length=s.length();  for(int i=length-1;i>=0;--i)    {      a[pos++]=s[i]-'0';    }} void BigDigitalOperation::sub( const string &s1,const string &s2,string &res){  if(cmp(s1,s2)==-1)    return;//只能是大数s1-小数s2  int L1=s1.length();  int L2=s2.length();  int min_length=L1>L2?L2:L1;  int max_length=L1>L2?L1:L2;  int *s1_tmp = new int [s1.length()+1];  int*s2_tmp= new int[s2.length()+1];  int *res_tmp=new int[max_length+1];  for(int i=0;i<max_length+1;++i)res_tmp[i]=0;  convert(s1_tmp,s1);  convert(s2_tmp,s2);  for(int i=0;i<max_length;++i)    {      if(i<min_length)        {          if(s1_tmp[i]<s2_tmp[i])            {              s1_tmp[i]+=10;              s1_tmp[i+1]-=1;            }          res_tmp[i]=s1_tmp[i]-s2_tmp[i];        }      else         {          if(s1_tmp[i]<0)            {              s1_tmp[i]+=10;              s1_tmp[i+1]-=1;            }          res_tmp[i]=s1_tmp[i];        }    }  for(int i=max_length-1;i>=0;--i)    if(res_tmp[i]!=0)      {         while(i>=0)          {            res.insert(res.end(),res_tmp[i--]+'0');          }      }  if(res.size()==0)    res.insert(res.end(),'0');}void BigDigitalOperation::multiplication(const string&s1,const string &s2,string&res){  int L1=s1.length();  int L2=s2.length();  int *s1_tmp=new int[L1+1];  int *s2_tmp=new int[L2+1];  int *res_tmp=new int[L1+L2+1];  for(int i=0;i<L1+L2+1;++i)res_tmp[i]=0;  int pos=0;  for(int i=L1-1;i>=0;--i){    s1_tmp[pos++]=s1[i]-'0';  }  pos=0;  for(int i=L2-1;i>=0;--i){    s2_tmp[pos++]=s2[i]-'0';  }  int c=L1+L2;  for(int i=0;i<L1;++i){    for(int j=0;j<L2;++j){      res_tmp[i+j]+=s1_tmp[i]*s2_tmp[j];    }     for(int k=0;k<c;++k){      if(res_tmp[k]>9){        res_tmp[k+1]+=res_tmp[k]/10;        res_tmp[k]%=10;        if(res_tmp[c]!=0){c++;}      }     }   }   for(int i=c-1;i>=0;--i){    if(res_tmp[i]!=0)      while(i>=0)        {          res.insert(res.end(),res_tmp[i]+'0');          --i;        }  }  delete s1_tmp;  delete s2_tmp;  delete res_tmp;}void BigDigitalOperation::divide(const string & s1,const string &s2,string & res,string &mod){  string res_tmp;  string mod_tmp;  string tmp;  string m_tmp;//s2*k==m_tmp;  int begin=0,end=0;int i;  while(true)    {      i=begin;if(i>=s1.size())break;      while(cmp(mod_tmp,s2)==-1)        {          if(!(mod_tmp.size()==0 && s1[i]=='0'))            mod_tmp.insert(mod_tmp.end(),s1[i]);          i++;          if(i>=s1.size())break;        }      if(begin!=0)        for(int j=begin+1;j<i;++j)          res_tmp.insert(res_tmp.end(),'0');      begin=i;      //----------------------------------------      string k="0";      while(cmp(mod_tmp,m_tmp)==1)        {          m_tmp.clear();k[0]++;          multiplication(s2,k,m_tmp);        }      if(cmp(mod_tmp,m_tmp)==-1) k[0]--;      m_tmp.clear();      multiplication(s2,k,m_tmp);      sub(mod_tmp,m_tmp,tmp);      if(tmp.size()==1 && tmp[0]=='0')tmp.clear();      res_tmp.insert(res_tmp.end(),k[0]);      //---------------------------------------------------------      m_tmp.clear();      mod_tmp.clear();      mod_tmp=tmp;      tmp.clear();    }  res=res_tmp;  if(mod_tmp.size()==0)    mod_tmp.insert(mod_tmp.end(),'0');  mod=mod_tmp;}void BigDigitalOperation::toBin(const string&s1, string &res){const string key="2";string s1_tmp=s1;string mod;while(cmp(s1_tmp,key)!=-1){string tmp;//商divide(s1_tmp,key,tmp,mod);res.insert(res.end(),mod[0]);mod.clear();s1_tmp.clear();s1_tmp=tmp; }res.insert(res.end(),s1_tmp[0]); }void BigDigitalOperation::quitCalculate(const string&s1,const string & s2,const string&s3,string &res){  string s2_bin;  toBin(s2,s2_bin);  string ss;  string mod;  string d="1",tmp;  for(int i=s2_bin.size()-1;i>=0;--i)    {      multiplication(d,d,tmp);      divide(tmp,s3,ss,mod);      d.clear();      d=mod;      mod.clear();      tmp.clear();      if(s2_bin[i]=='1')        {          multiplication(d,s1,tmp);          divide(tmp,s3,ss,mod);          d.clear();          d=mod;          mod.clear();          tmp.clear();        }    }  res=d;  }std::string  BigDigitalOperation::toValue(const std::string & key){std::map<std::string,std::string>mp;//mp["00"]=" ";mp["01"]="1";mp["02"]="2";mp["03"]="3";mp["04"]="4";//mp["05"]="5";mp["06"]="6";mp["07"]="7";mp["08"]="8";mp["09"]="9";mp["10"]="a";mp["11"]="b";mp["12"]="c";mp["13"]="d";mp["14"]="e";mp["15"]="f";mp["16"]="g";mp["17"]="h";mp["18"]="i";mp["19"]="j";mp["20"]="k";mp["21"]="l";mp["22"]="m";mp["23"]="n";mp["24"]="o";mp["25"]="p";mp["26"]="q";mp["27"]="r";mp["28"]="s";mp["29"]="t";mp["30"]="u";mp["31"]="v";mp["32"]="w";mp["33"]="x";mp["34"]="y";mp["35"]="z";mp["36"]="A";mp["37"]="B";mp["38"]="C";mp["39"]="D";mp["40"]="E";mp["41"]="F";mp["42"]="G";mp["43"]="H";mp["44"]="I";mp["45"]="J";mp["46"]="K";mp["47"]="L";mp["48"]="M";mp["49"]="N";mp["50"]="O";mp["51"]="P";mp["52"]="Q";mp["53"]="R";mp["54"]="S";mp["55"]="T";mp["56"]="U";mp["57"]="V";mp["58"]="W";mp["59"]="X";mp["60"]="Y";mp["61"]="Z";mp["62"]="0";mp["63"]="1";mp["64"]="2";mp["65"]="3";mp["66"]="4";mp["67"]="5";mp["68"]="6";mp["69"]="7";mp["70"]="8";mp["71"]="9";return mp[key];}std::string BigDigitalOperation::toKey(const std::string& value){std::map<std::string, std::string>mp;mp["10"]="a";mp["11"]="b";mp["12"]="c";mp["13"]="d";mp["14"]="e";mp["15"]="f";mp["16"]="g";mp["17"]="h";mp["18"]="i";mp["19"]="j";mp["20"]="k";mp["21"]="l";mp["22"]="m";mp["23"]="n";mp["24"]="o";mp["25"]="p";mp["26"]="q";mp["27"]="r";mp["28"]="s";mp["29"]="t";mp["30"]="u";mp["31"]="v";mp["32"]="w";mp["33"]="x";mp["34"]="y";mp["35"]="z";mp["36"]="A";mp["37"]="B";mp["38"]="C";mp["39"]="D";mp["40"]="E";mp["41"]="F";mp["42"]="G";mp["43"]="H";mp["44"]="I";mp["45"]="J";mp["46"]="K";mp["47"]="L";mp["48"]="M";mp["49"]="N";mp["50"]="O";mp["51"]="P";mp["52"]="Q";mp["53"]="R";mp["54"]="S";mp["55"]="T";mp["56"]="U";mp["57"]="V";mp["58"]="W";mp["59"]="X";mp["60"]="Y";mp["61"]="Z";mp["62"]="0";mp["63"]="1";mp["64"]="2";mp["65"]="3";mp["66"]="4";mp["67"]="5";mp["68"]="6";mp["69"]="7";mp["70"]="8";mp["71"]="9";std::map<std::string,std::string>::iterator it;for(it=mp.begin();it!=mp.end();++it){if(value==it->second)return it->first;}return "";}void BigDigitalOperation::mod(const string &s1,const string &s2,string &mod){std::string tmp;divide(s1,s2,tmp,mod);}


原创粉丝点击