大数加法
2017-03-15 23:12
176 查看
#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, 4000 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& e9ca 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;
}
其实函数部分实现的是两个正数相加,大正数减小正数,和正数大小比较三个函数,加减法用主函数或新写函数调用这三个函数同时运用分类讨论的思想即可解决问题。
相关文章推荐
- hduoj1002,A + B Problem II, 大数加法,经典
- 大数加法、减法、乘法、除法、求余、比较、乘方(代码简短(总计150)、易懂)
- 大数加法
- C语言 加法 大数加法
- 大数加法
- 大数的加法,减法,乘法
- 大数加法
- //九度OJ 教程63 大数处理之浮点数加法
- 大数加法
- Fibonacci Numbers 基于大数加法
- 杭电1002大数加法
- 大数乘法、大数加法实现
- hdu 4099 Revenge of Fibonacci(字典树+大数加法)
- Problem 56: 某种序列(大数加法+数列)
- 大数加法
- 大数加法--回文数
- 51nod 1005 大数加法
- 序列 大数加法
- 51nod--基础题大数加法
- Hdu1002 大数加法