[C++]第七次作业:实现一个大整数类BigInt

/*
作业七
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;
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值