C++單鏈表如何實(shí)現(xiàn)大數(shù)加法-創(chuàng)新互聯(lián)

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

創(chuàng)新互聯(lián)建站是一家專注于成都網(wǎng)站建設(shè)、網(wǎng)站制作與策劃設(shè)計(jì),惠濟(jì)網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:惠濟(jì)等地區(qū)?;轁?jì)做網(wǎng)站價(jià)格咨詢:18982081108

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;
}

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.muchs.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

文章題目:C++單鏈表如何實(shí)現(xiàn)大數(shù)加法-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)URL:http://www.muchs.cn/article36/dhjspg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、ChatGPT、域名注冊(cè)定制網(wǎng)站、商城網(wǎng)站網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司