//大数类
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
#define MAXN 9999
#define DLEN 4
class BigNum{
private:
int a[300];//DLEN digs for a position
int len;
public:
BigNum(){len = 1;memset(a,0,sizeof(a));}
BigNum(const int b);
BigNum(const BigNum & T);
bool Bigger(const BigNum &) const;
BigNum & operator=(const BigNum &);
BigNum & Add(const BigNum &);
BigNum & Sub(const BigNum &);
BigNum operator+(const BigNum &) const;
BigNum operator-(const BigNum &) const;
BigNum operator*(const BigNum &) const;
BigNum operator/(const int &) const;
void Print();
};
BigNum::BigNum(const int b)
{
int c,d = b;
len = 0;
memset(a,0,sizeof(a));
while(d > MAXN){
c = d - d / (MAXN + 1) * (MAXN + 1);
d = d / (MAXN + 1);
a[len++] = c;
}
a[len++] = d;
}
BigNum::BigNum(const BigNum & T) : len(T.len)
{
int i;
memset(a,0,sizeof(a));
for(i = 0 ; i < len ; i++)
a[i] = T.a[i];
}
bool BigNum::Bigger(const BigNum & T) const
{
int ln;
if(len > T.len) return true;
else if(len == T.len){
ln = len - 1;
while(a[ln] == T.a[ln] && ln >= 0) ln--;
if(ln >= 0 && a[ln] > T.a[ln]) return true;
else return false;
}
else return false;
}
BigNum & BigNum::operator=(const BigNum & n)
{
len = n.len;
memset(a,0,sizeof(a));
for(int i = 0 ; i < len ; i++)
a[i] = n.a[i];
return *this;
}
BigNum & BigNum::Add(const BigNum & T)
{
int i,big;
big = T.len > len ? T.len : len;
for(i = 0 ; i < big ; i++)
{
a[i] = a[i] + T.a[i];
if(a[i] > MAXN)
{
a[i + 1]++;
a[i] = a[i] - MAXN - 1;
}
}
if(a[big] != 0) len = big + 1;
else len = big;
return *this;
}
BigNum & BigNum::Sub(const BigNum & T)
{
int i,j,big;
big = T.len > len ? T.len : len;
for(i = 0 ; i < big ; i++){
if(a[i] < T.a[i]){
j = i + 1;
while(a[j] == 0) j++;
a[j--]--;
while(j > i) a[j--] += MAXN;
a[i] = a[i] + MAXN + 1 - T.a[i];
}
else a[i] -= T.a[i];
}
len = big;
while(a[len - 1] == 0 && len > 1) len--;
return *this;
}
BigNum BigNum::operator+(const BigNum & n) const
{
BigNum a = *this;
a.Add(n);
return a;
}
BigNum BigNum::operator-(const BigNum & T) const
{
BigNum b = *this;
b.Sub(T);
return b;
}
BigNum BigNum::operator*(const BigNum & T) const
{
BigNum ret;
int i,j,up;
int temp,temp1;
for(i = 0 ; i < len ; i++){
up = 0;
for(j = 0 ; j < T.len ; j++){
temp = a[i] * T.a[j] + ret.a[i + j] + up;
if(temp > MAXN){
temp1 = temp - temp / (MAXN + 1) * (MAXN + 1);
up = temp / (MAXN + 1);
ret.a[i + j] = temp1;
}
else {
up = 0;
ret.a[i + j] = temp;
}
}
if(up != 0)
ret.a[i + j] = up;
}
ret.len = i + j;
while(ret.a[ret.len - 1] == 0 && ret.len > 1) ret.len--;
return ret;
}
BigNum BigNum::operator/(const int & b) const
{
BigNum ret;
int i,down = 0;
for(i = len - 1 ; i >= 0 ; i--){
ret.a[i] = (a[i] + down * (MAXN + 1)) / b;
down = a[i] + down * (MAXN + 1) - ret.a[i] * b;
}
ret.len = len;
while(ret.a[ret.len - 1] == 0) ret.len--;
return ret;
}
void BigNum::Print()
{
int i;
cout << a[len - 1];
for(i = len - 2 ; i >= 0 ; i--){
cout.width(DLEN);
cout.fill('0');
cout << a[i];
}
cout << endl;
}
大数 加法:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BIT 1000
void add(char *re,char *op1,char *op2)
{
int i=0,cf=0,temp,m1,m2;
while(1)
{
if(*op1)m1=*op1-48,++op1;
else m1=0;
if(*op2)m2=*op2-48,++op2;
else m2=0;
temp=m1+m2+cf;
cf=temp/10;
re[i++]=temp%10+48;
if(!*op1&&!*op2)
{
if(cf)re[i++]=cf+48;
re[i]=0;
break;
}
}
}
void make(char *op1,char *op2)
{
char re[MAX_BIT];
int i=0;
add(re,strrev(op1),strrev(op2));
strrev(re);
while(*(re+i)=='0')i++;
printf("%s/n",re+i);
}
int main()
{
char op1[MAX_BIT],op2[MAX_BIT];
scanf("%s%s",op1,op2);
make(op1,op2);
system("PAUSE");
return 0;
}
//-------------------------------------------
// 大整数加法.
//
// 利用字符数组模拟实现大数加法运算
//
// 2006-11-30
//-------------------------------------------
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#define min( x ,y ) ( x>y )? y : x;
#define max( x ,y ) ( x>y )? x : y;
#define len 100
int main()
{
char s1[ len-1 ] ,s2[ len-1 ] ;
char s[ len ] ;
cout<<"Input a number:( <10^"<<len<<" )"<<endl;
cin>>s1;
cout<<"Input the other number:( <10^"<<len<<" )"<<endl;
cin>>s2;
int len1 = strlen( s1 );
int len2 = strlen( s2 );
int grad = 0;//进位标志
int minl = min( len1 , len2 );
int maxl = max( len1 , len2 );
int i ,j ;
for( i=0 ; i<minl ; i++ )//第一步:处理公共部分
{
s[ len-1-i ] = ( grad + s1 [ len1-1-i ] + s2[ len2-1-i ] - '0' - '0' )%10;
grad = ( grad + s1[ len1-1-i ] + s2[ len2-1-i ] - '0' - '0' )/10;
}
for( ; i<maxl ; i++ )//第二步:处理截断的部分
{
if( len1>len2 )
{
s[ len-1-i ] = ( grad + s1[ len1-1-i ] - '0' )%10;
grad = ( grad + s1[ len1-1-i ] - '0' )/10;
}
else
{
s[ len-1-i ] = (grad + s2[ len2-1-i ]- '0' )%10;
grad = ( grad + s2[ len2-1-i ] - '0' )/10;
}
}
while( grad&&i<len )//处理进位的情况
{
s[ len-1-i ] = grad%10;
grad = grad/10;
++i;
}
if( i>=len-1 )//若结果存不下,提示.
{
cout<<"The store is too short!/n";
exit(1);
}
cout<<"Reslut is:/n";//打印结果
for( j=i ; j>0 ; j-- )
cout<<int( s[ len-j ] );
cout<<endl;
return 1;
}