以链表形式储存大整数并运算

//////Digital是每一位上的数,number类是存储大数的链表类(用字符串初始化),insert是从低到高位插入数据的成员函数,nplus是对number类对象进行运算的函数。
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

struct Digital
{
    int x;
    Digital*next;
};

class number
{
    Digital*head;
public:
    number() { head = NULL; }
    number(string&);
    void insert(int);
    void output();
    Digital*Gethead() { return head; }
};

number::number(string& x)
{
    reverse(x.begin(), x.end());
    for (unsigned int i = 0; i < x.length(); i++)
    {
        insert(x[i]-'0');
    }
}

void number::insert(int x)
{
    Digital*d=head;
    if (d==NULL)
    {
        d = new Digital;
        d->next = NULL;
        d->x = x;
        head = d;
    }
    else
    {
        while (d->next != NULL)
        {
            d = d->next;
        }
        d->next = new Digital;
        d->next->next = NULL;
        d->next->x = x;
    }
}

void number::output()
{
    Digital*d = head;
    string s="";
    string t = "";
    while (d->next != NULL)
    {
        t = char(d->x) + '0';
        s.insert(0,t);
        d = d->next;
    } 
    t = char(d->x) + '0';
    s.insert(0, t);
    for (unsigned int i = 0; i < s.length(); i++)
    {
        cout << s[i];
    }
    cout << endl;
}

number nplus(number&a, number&b)
{
    number c;
    Digital*x = a.Gethead();
    Digital*y = b.Gethead();
    int o = 0, p= 0;
    while (x->next!=NULL&&y->next!=NULL)
    {
        int t = x->x + y->x;
        o=t%10;
        c.insert(o+p);
        p = t / 10;
        x = x->next;
        y = y->next;
    }
    int t = x->x + y->x;
    o = t % 10;
    c.insert(o + p);
    p = t / 10;
    if (x->next!=NULL)
    {
        while (x->next != NULL)
        {
            int t = x->x;
            o = t;
            c.insert(o + p);
            p =0;
            x = x->next;
        }
    }
    else
    {
        while (y->next != NULL)
        {
            int t = y->x;
            o = t;
            c.insert(o + p);
            p = 0;
            y = y->next;
        }
    }
    return c;
}

int main()
{
    string s1="1145141919810";
    string s2 = "97";
    number n1(s1);
    number n2(s2);
    n1.output();
    n2.output();
    number n3 = nplus(n1,n2);
    n3.output();
    getchar();
    return 0;
}
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以为您解答。为了实现用链表储存大整数并进行运算,我们可以创建一个类来表示这个超大整数。以下是一个简单的 C++ 代码示例,用于实现这个类: ```cpp #include <iostream> #include <string> using namespace std; // 链表节点定义 struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(NULL) {} }; // 超大整数类定义 class BigInt { private: ListNode* head; // 链表头指针 public: BigInt() : head(NULL) {} // 构造函数,初始化链表头指针为空 BigInt(string str) : head(NULL) { // 构造函数,根据字符串初始化链表 for (int i = str.size() - 1; i >= 0; i--) { ListNode* node = new ListNode(str[i] - '0'); node->next = head; head = node; } } // 拷贝构造函数,用于拷贝一个 BigInt 对象 BigInt(const BigInt& other) : head(NULL) { ListNode* cur = other.head; while (cur != NULL) { ListNode* node = new ListNode(cur->val); node->next = head; head = node; cur = cur->next; } } // 析构函数,释放链表 ~BigInt() { ListNode* cur = head; while (cur != NULL) { ListNode* next = cur->next; delete cur; cur = next; } } // 重载加号运算符,实现两个 BigInt 对象的加法运算 BigInt operator+(const BigInt& other) const { BigInt sum; ListNode* p = head, * q = other.head; int carry = 0; while (p != NULL || q != NULL) { int x = (p != NULL) ? p->val : 0; int y = (q != NULL) ? q->val : 0; int s = x + y + carry; carry = s / 10; ListNode* node = new ListNode(s % 10); node->next = sum.head; sum.head = node; if (p != NULL) p = p->next; if (q != NULL) q = q->next; } if (carry > 0) { ListNode* node = new ListNode(carry); node->next = sum.head; sum.head = node; } return sum; } // 重载输出运算符,用于将 BigInt 对象输出到标准输出流中 friend ostream& operator<<(ostream& os, const BigInt& num) { ListNode* cur = num.head; while (cur != NULL) { os << cur->val; cur = cur->next; } return os; } }; int main() { BigInt a("12345678901234567890"); BigInt b("98765432109876543210"); BigInt c = a + b; cout << c << endl; return 0; } ``` 在这个示例中,我们创建了一个名为 `BigInt` 的类,用于表示超大整数。这个类的数据成员是一个链表头指针,每个节点包含一个整数。我们在类中实现了构造函数、拷贝构造函数、析构函数、重载加号运算符和输出运算符。 在 `operator+` 函数中,我们使用了链表的基本操作,遍历两个链表并逐位相加,得到一个新的链表表示结果。在 `operator<<` 函数中,我们使用了输出流操作符 `<<`,将链表中的整数逐位输出到标准输出流中。 最后,在 `main` 函数中,我们创建了两个超大整数,并进行了加法运算,将结果输出到标准输出流中。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值