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

来源:互联网 发布:c语言对比字符串 编辑:程序博客网 时间:2024/04/29 14:24

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

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 win7系统不带usb驱动怎么办 手机网页上的pdf打不开怎么办 网页下载pdf后缀是.do怎么办 ps界面太小怎么办win10 ps软件打不开程序错误怎么办 ps打开后 未响应怎么办 ps图层无法解锁怎么办 ie8浏览器电脑不能用怎么办 系统要ie6.0才能打开怎么办 2g手机内存不够怎么办 2g运行内存不够怎么办 手机运行内存2g不够怎么办 手机无法加载程序秒退怎么办 电脑账户密码忘记了怎么办 玩绝地求生卡顿怎么办 地下城总运行时间错误怎么办 逆战更新太慢怎么办 win7我的电脑没了怎么办 剑灵启动游戏慢怎么办 网页页面结束进程也关不掉怎么办 开机就启动微信怎么办 微信突然无法启动怎么办 微信发送太频繁怎么办 微信在电脑上打不开文件怎么办 微信照片电脑上打不开怎么办 换一部手机微信怎么办 微信支付宝停止运行怎么办 剑三重制版卡顿怎么办 剑三客户端更新不动了怎么办 安装包安装失败怎么办有内存 qq飞车换手机了怎么办 qq飞车求婚失败戒指怎么办 改脸型皮肤会下垂怎么办 情侣关系弄僵了怎么办 用微信交话费没有到账怎么办 微信交错话费了怎么办 微信缴费交错了怎么办 微信支付被投诉怎么办 微信q币充值错误怎么办 微信充值流量充错了怎么办 微信延迟到账怎么办