C++单链表如何实现大数加法

发布时间:2020-07-21 14:03:17 作者:小猪
来源:亿速云 阅读:133

这篇文章主要为大家展示了C++单链表如何实现大数加法,内容简而易懂,希望大家可以学习一下,学习完之后肯定会有收获的,下面让小编带大家一起来看看吧。

Input Format

输入文件包括两行。

Output Format

输出文件包括一行。

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

以上就是关于C++单链表如何实现大数加法的内容,如果你们有学习到知识或者技能,可以把它分享出去让更多的人看到。

推荐阅读:
  1. 以c++的方式实现单链表
  2. 线性表--单链表(C++)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++ 单链表 c+

上一篇:为什么本地开发时使用CURL请求本地URL会卡死

下一篇:位置不可用无法访问E此卷不包含可识别文件系统

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》