溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

C++單鏈表如何實(shí)現(xiàn)大數(shù)加法

發(fā)布時(shí)間:2020-07-21 14:03:17 來(lái)源:億速云 閱讀:141 作者:小豬 欄目:編程語(yǔ)言

這篇文章主要為大家展示了C++單鏈表如何實(shí)現(xiàn)大數(shù)加法,內(nèi)容簡(jiǎn)而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會(huì)有收獲的,下面讓小編帶大家一起來(lái)看看吧。

Input Format

輸入文件包括兩行。

  • 第一行包括一個(gè)正整數(shù),保證位數(shù)不超過(guò)1000000。
  • 第二行包括一個(gè)正整數(shù),保證位數(shù)不超過(guò)1000000。

Output Format

輸出文件包括一行。

  • 第一行包括一個(gè)正整數(shù)。

Sample Input

10558
22

Sample Output

10580

#include <iostream>

using namespace std;

class BigData {
  friend ostream &operator<<(ostream &os, const BigData &x);

  friend istream &operator>>(istream &is, BigData &x);

  friend BigData operator+(BigData a, BigData b);

private:
  struct node {
    int data;
    node *next;

    node(const short &x, node *n = NULL) {
      data = x;
      next = n;
    }
  };

  node *num;

  void clear();

public:
  BigData(node *p = NULL) {
    if (p == NULL) {
      num = new node(0);
    } else {
      num = p;
    };
  }

  BigData(const BigData &);

  ~BigData() {
    clear();
  }

  BigData &operator=(const BigData &);
};

BigData::BigData(const BigData &x) {
  num = new node(x.num->data);
  node *p = num, *q = x.num;
  while (q->next != NULL) {
    q = q->next;
    p->next = new node(q->data);
    p = p->next;
  }
}

void BigData::clear() {
  node *p = num, *q;
  while (p != NULL) {
    q = p;
    p = p->next;
    delete q;
  }
  num = NULL;
}

BigData operator+(BigData a, BigData b) {
  BigData tmp;
  BigData::node *p, *q, *end;
  int carry;
  tmp.num = end = new BigData::node(a.num->data + b.num->data);
  carry = tmp.num->data / 10;
  tmp.num->data %= 10;
  p = a.num->next;
  q = b.num->next;
  end = tmp.num;
  while (p != NULL && q != NULL) {
    end->next = new BigData::node(p->data + q->data + carry);
    end = end->next;
    carry = end->data / 10;
    end->data %= 10;
    p = p->next;
    q = q->next;
  }
  if (p == NULL)p = q;
  while (p != NULL) {
    end->next = new BigData::node(p->data + carry);
    end = end->next;
    carry = end->data / 10;
    end->data %= 10;
    p = p->next;
  }
  if (carry != 0) {
    end->next = new BigData::node(carry);
    return tmp;
  }
}

BigData &BigData::operator=(const BigData &x) {
  if (&x == this)return *this;
  clear();
  num = new node(x.num->data);
  node *p = num, *q = x.num;
  while (q->next != NULL) {
    q = q->next;
    p->next = new node(q->data);
    p = p->next;
  }
  return *this;
}

istream &operator>>(istream &is, BigData &x) {
  char ch;
  x.clear();
  while ((ch = is.get()) != '\n') {
    x.num = new BigData::node(ch - '0', x.num);
  }
  return is;
}

ostream &operator<<(ostream &os, const BigData &x) {
  string s;
  BigData::node *p = x.num;
  while (p != NULL) {
    s = char(p->data + '0') + s;
    p = p->next;
  }
  for (int i = 0; i < s.size(); ++i)os << s[i];
  return os;
}

int main() {
  BigData a, b, c;
  cin >> a >> b;
  c = a + b;
  cout << c;
}

以上就是關(guān)于C++單鏈表如何實(shí)現(xiàn)大數(shù)加法的內(nèi)容,如果你們有學(xué)習(xí)到知識(shí)或者技能,可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI