大数加法

来源:互联网 发布:淘宝开店身份证泄露 编辑:程序博客网 时间:2024/06/14 02:22
#include<cstdio>
#include<cstring>
#include<vector>
#include<iostream>
#include <sstream>
#include <fstream>
using namespace std;
struct BigInteger {
  static const int BASE = 100000000;
  static const int WIDTH = 8;
  vector<int> s;
  BigInteger(long long num = 0) { *this = num; } // 构造函数
  BigInteger(const string& str){*this=str;}
  BigInteger operator = (long long num) { // 赋值运算符
    this->s.clear();
    do {
      this->s.push_back(num % BASE);
      num /= BASE;
    } while(num > 0);
    return *this;
  }
  BigInteger operator = (string const& str) { // 赋值运算符
    s.clear();
    int x, len = (str.length() - 1) / WIDTH + 1;
    for(int i = 0; i < len; i++) {
      int end = str.length() - i*WIDTH;
      int start = max(0, end - WIDTH);
      sscanf(str.substr(start, end-start).c_str(), "%d", &x);
      s.push_back(x);
    }
    return *this;
  }
  BigInteger operator + (const BigInteger& b) const {
    BigInteger c;
    c.s.clear();
    for(int i = 0, g = 0; ; i++) {
      if(g == 0 && i >= s.size() && i >= b.s.size()) break;
      int x = g;
      if(i < s.size()) x += s[i];
      if(i < b.s.size()) x += b.s[i];
      c.s.push_back(x % BASE);
      g = x / BASE;
    }
    return c;
  }
  BigInteger operator +=(const BigInteger x)
  {
      *this=*this+x;
      return x;
  }
  //该大整数类只有正数。
  bool operator >(const BigInteger& b) const{
      if(this->s.size()>b.s.size())
          return 1;
      else if(this->s.size()==b.s.size())
      {
          int size=s.size();
          for(int i=size-1;i>=0;i--)
          {
             if(s[i]>b.s[i])
                 return 1;
             else 
                 return 0;
          }
          return 0;
      }
      else
          return 0;
  }
  //只做正数大减小的减法
  BigInteger operator -(const BigInteger& b) const
  {
      BigInteger c;
      c.s.clear();
      for(int i = 0, g = 0; ; i++) {
        if(g == 0 && i >= s.size() && i >= b.s.size()) break;
        //int x = g*BASE;
        int x=0;
        if(i < s.size()&&g==0) x += s[i];
        else if(i<s.size()&&g==1) x+=s[i]-1;
        if(i < b.s.size()) x -= b.s[i];
        g=0;
        if(x<0)
        {
            g=1;
            x+=g*BASE;
        }
        c.s.push_back(x);
      }
      if(c.s.back()==0)
          c.s.pop_back();
      return c;
  }
};
ostream& operator << (ostream &out, const BigInteger& x) {
  out << x.s.back();
  for(int i = x.s.size()-2; i >= 0; i--) {
    char buf[20];
    sprintf(buf, "%08d", x.s[i]);
    for(int j = 0; j < strlen(buf); j++) out << buf[j];
  }
  return out;
}
istream& operator >> (istream &in, BigInteger& x) {
  string s;
  if(!(in >> s)) return in;
  x = s;
  return in;
}
#include<set>
#include<map>
set<BigInteger> s;
map<BigInteger, int> m;
int main() {
  string A,B;
  cin>>A;
  cin>>B;
  if(A[0]!='-'&&B[0]!='-')
      cout<<BigInteger(A)+BigInteger(B);
  else if(A[0]=='-'&&B[0]=='-')
  {
      string a,b;
      for(int i=1;i<A.length();i++)
      {
          a.push_back(A[i]);
      }
      for(int i=1;i<B.length();i++)
      {
          b.push_back(B[i]);
      }
      BigInteger sum;
      sum=BigInteger(a)+BigInteger(b);
      stringstream out;
      string output;
      out<<"-";
      out << sum.s.back();
      for(int i = sum.s.size()-2; i >= 0; i--) {
        char buf[20];
        sprintf(buf, "%08d", sum.s[i]);
        for(int j = 0; j < strlen(buf); j++) out << buf[j];
      }
      out>>output;
      cout<<output;
  }
  else if(A[0]!='-'&&B[0]=='-')
  {
      string a,b;
      for(int i=0;i<A.length();i++)
      {
          a.push_back(A[i]);
      }
      for(int i=1;i<B.length();i++)
      {
          b.push_back(B[i]);
      }
      BigInteger At(a),Bt(b);
      if(At>Bt)
          cout<<At-Bt;
      else
          cout<<"-"<<Bt-At;
  }
  else
  {
      string temp=A; A=B; B=temp;
      string a,b;
      for(int i=0;i<A.length();i++)
      {
          a.push_back(A[i]);
      }
      for(int i=1;i<B.length();i++)
      {
          b.push_back(B[i]);
      }
      BigInteger At(a),Bt(b);
      if(At>Bt)
          cout<<At-Bt;
      else
      {
          cout<<"-"<<Bt-At;
          ofstream outfile;
          outfile.open("123.txt");
          outfile<<"-"<<Bt-At<<endl;
          outfile.close();
      }
  }
  return 0;

}


其实函数部分实现的是两个正数相加,大正数减小正数,和正数大小比较三个函数,加减法用主函数或新写函数调用这三个函数同时运用分类讨论的思想即可解决问题。

                                             
0 0
原创粉丝点击