/*
作业七
1. 不同计算机系统中所能表示的证书的范围不同。如在一个32位的机器上,
一个long类型的整数范围是-231~231-1。在某些应用中,需要处理比这个范围大得多的整数。
不同的大整数的长度(数字位数)可能差别较大,请基于STL库中的list容器(双向链表)实现一个大整数类BigInt.
*/
#include "BigInt.h"
void main()
{
BigInt a = "993729874";
BigInt b = "783943748";
BigInt c = a + b;
c.Print(); //1777673622
BigInt d = b - a; //-209786126
d.Print();
}
//BigInt.cpp
#include "BigInt.h"
BigInt operator+ (BigInt a, BigInt b)
{
BigInt c;
IntIterList iter_a = a.MinInt.begin() , iter_end_a = a.MinInt.end();
IntIterList iter_b = b.MinInt.begin() , iter_end_b = b.MinInt.end();
int temp=0;
int remainder = 0;
while ( (iter_a != iter_end_a) || (iter_b != iter_end_b) )
{
if(iter_a != iter_end_a) temp += *iter_a;
if(iter_b != iter_end_b) temp += *iter_b;
remainder = temp%1000;
temp = temp/1000;
c.MinInt.push_back(remainder);
c._Size += 3;
c._MinIntSize++;
if(iter_a != iter_end_a) iter_a++;
if(iter_b != iter_end_b) iter_b++;
}
if ( temp !=0 )
{
c._MinIntSize++;
c.MinInt.push_back(temp);
while (temp != 0)
{
c._Size++;
temp /= 10 ;
}
}
return c;
}
BigInt operator- (BigInt a,BigInt b)
{
BigInt c;
int flag = 1; //a>=b就1,否则是0
IntIterList iter_a = a.MinInt.begin() , iter_end_a = a.MinInt.end();
IntIterList iter_b = b.MinInt.begin() , iter_end_b = b.MinInt.end();
// 判断a和b谁大?
if (a.Size() > b.Size()) ;
else if (a.Size() < b.Size()) flag = 0;
else
{
while ( (iter_end_a != iter_a) || (iter_end_b != iter_b) )
{
--iter_end_b;
--iter_end_a;
if (*iter_end_a > *iter_end_b) break;
if (*iter_end_a < *iter_end_b)
{
flag = 0;
break;
}
}
}
iter_end_a = a.MinInt.end();
iter_end_b = b.MinInt.end();
int temp=0;
int remainder = 0;
while ( (iter_a != a.MinInt.end()) || (iter_b != b.MinInt.end()) )
{
if (flag)
{
if(iter_a != iter_end_a) temp += *iter_a;
if(iter_b != iter_end_b) temp -= *iter_b;
}
else
{
if(iter_a != iter_end_a) temp -= *iter_a;
if(iter_b != iter_end_b) temp += *iter_b;
}
remainder = temp%1000;
temp = temp/1000;
if ( remainder < 0 )
{
remainder = remainder +1000; //若是负数,则搞成正的
temp--;
}
c.MinInt.push_back(remainder);
c._Size += 3;
c._MinIntSize++;
if(iter_a != iter_end_a) iter_a++;
if(iter_b != iter_end_b) iter_b++;
}
if (flag==0) c.ChangeMark();
return c;
}
void BigInt::ChangeMark()
{
int temp = MinInt.back();
temp = -temp;
MinInt.pop_back();
MinInt.push_back(temp);
}
.//BigInt.h
#include <iostream>
#include <iomanip>
#include <string>
#include <list>
using namespace std;
typedef list<int>::iterator IntIterList;
class BigInt {
public:
BigInt():_Size(0),_MinIntSize(0){};
BigInt(const BigInt& other):MinInt(other.MinInt),_MinIntSize(other._MinIntSize),_Size(other._Size){};
BigInt(string);
int Size() {return _Size;}
friend BigInt operator+ (BigInt,BigInt);
friend BigInt operator- (BigInt,BigInt);
int MinIntSize() {return _MinIntSize;}
void Print();
void ChangeMark();
private:
list<int> MinInt;
int _MinIntSize;
int _Size;
};
inline void BigInt::Print()
{
for ( IntIterList iter = MinInt.end(); iter!= MinInt.begin();)
{
iter--;
if (iter==MinInt.begin())
cout << setw(3) << setfill('0') << *iter; //为了保证显示的正确,非首位必须强制显示“0”
else
cout << *iter;
}
cout << endl;
}
inline BigInt::BigInt(string str):_Size(0)
{
_Size = str.size();
_MinIntSize = _Size / 3 ;
int temp = 0;
for (int i=_Size-1 ; i >= 0; )
{
for (int j=1; j <= 100; j = j*10 )
if( i >= 0 )
{
temp += (str[i]-48)*j;
i--;
}
MinInt.push_back (temp);
temp = 0;
}
}