链表实现大整数 加减法 一个节点保存四位数字(C++实现)

可用来熟悉数据结构与C++语言内容。

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;
class node {
public:
    node() {
        ch = "";
        next = nullptr;
    }
    node(string a) {
        ch = a;
        next = nullptr;
    }
    node(int a) {
        ch = to_string(a);
        while(ch.size() < 4) {
            ch = '0' + ch;
        }
        next = nullptr;
    }
    node(node *tmp) {
        ch = tmp->ch;
        next = nullptr;
    }

    string get() {
        return ch;
    }

public:
    string ch;
    node *next;
};

class List {
public:
    List() : head(new node),level(0),negative(false) {}
    List(string s) {
        level = 0;
        negative = false;
        if(s[0] == '-') {
            negative = true;
            s = s.substr(1);
        }
        head = new node;
        node* tmp = head;
        int n = s.size() % 4;
        if(n != 0) {
            string sub = s.substr(0,n);
            tmp->next = new node(sub);
            tmp = tmp->next;
            s = s.substr(n);
            level++;
        }
        while(!s.empty()) {
            string sub = s.substr(0,4);
            tmp->next = new node(sub);
            tmp = tmp->next;
            s = s.substr(4);
            level++;
        }
    }
 
    List(const List &t) {
        this->head = new node;
        node *tmp = head->next;
        node *my = this->head;
        this->level = t.level;
        this->negative = t.negative;
        while(tmp != nullptr) {
            my->next = new node(tmp);
            my = my->next;
            tmp = tmp->next;
        }
    }
    friend List operator+(const List& t,const List& a) {
        List ans;

        vector<int>pt(t.level);
        vector<int>pa(a.level);

        node* tmp = t.head->next;
        int k = 0;
        while(tmp != nullptr) {
            const char* ts = tmp->ch.data();
            pt[k++] = atoi(ts);
            tmp = tmp->next;
        }
        k = 0;
        tmp = a.head->next;
        while(tmp != nullptr) {
            const char* ts = tmp->ch.data();
            pa[k++] = atoi(ts);
            tmp = tmp->next;
        }

        int i = pt.size() - 1;
        int j = pa.size() - 1;
        tmp = ans.head->next;
        while(i >= 0 && j >= 0) {
            int u = pt[i] + pa[j] + carry;
            carry = 0;
            if(u > 10000) carry = 1;
            node *t = new node(u);
            ans.head->next = t;
            t->next = tmp;
            tmp = t;
            i--;
            j--;
        }

        while(i >= 0) {
            int u = carry + pt[i];
            carry = 0;
            if(u > 10000) carry = 1;
            node *t = new node(u);
            ans.head->next = t;
            t->next = tmp;
            tmp = t;
            i--;
        }

        while(j >= 0) {
            int u = carry + pa[j];
            carry = 0;
            if(u > 10000) carry = 1;
            node *t = new node(u);
            ans.head->next = t;
            t->next = tmp;
            tmp = t;
            j--;            
        }

        if(carry) {
            node *t = new node(carry);
            ans.head->next = t;
            t->next = tmp;
            tmp = t;            
            carry = 0;
        }

        return ans;
    }
    
    friend List operator-(const List &t,const List &a) {

        List ans; 
        vector<int>pt(t.level);
        vector<int>pa(a.level);

        node* tmp = t.head->next;
        int k = 0;
        while(tmp != nullptr) {
            const char* ts = tmp->ch.data();
            pt[k++] = atoi(ts);
            tmp = tmp->next;
        }
        k = 0;
        tmp = a.head->next;
        while(tmp != nullptr) {
            const char* ts = tmp->ch.data();
            pa[k++] = atoi(ts);
            tmp = tmp->next;
        }
 
        //smaller - bigger
        if(t.level < a.level) {
            vector<int>tmp(pt);
            pt = pa;
            pa = tmp;
            ans.negative = true;
        }

        if(t.level == a.level) {
            int k = 0;
            while(k < t.level) {
                if(pt[k] < pa[k]) break;
                k++;
            }
            if(k != t.level) {
                vector<int>tmp(pt);
                pt = pa;
                pa = tmp;
                ans.negative = true;
            }
        }
        
        int i = pt.size() - 1;
        int j = pa.size() - 1;

        while(i >= 0 && j >= 0) {
            int u = pt[i] - pa[j] - carry;
            carry = 0;
            string p = "";
            if(u < 0) {
                u += 10000;
                carry = 1;
                p = to_string(u);
                while(p.size() < 4) {
                    p = '0' + p;
                }
            }else {
                p = to_string(u);
                while(p.size() < 4) {
                    p = '0' + p;
                }                
            }
            node *t = new node(p);
            ans.head->next = t;
            t->next = tmp;
            tmp = t;
            i--;
            j--;
        }
        while(i >= 0) {
            string p = "";
            if(carry) {
                pt[i] -= carry;
                carry = 0;
            }
            p = to_string(pt[i]);
            while(p.size() < 4) {
                p = '0' + p;
            }  
            node *t = new node(p);
            ans.head->next = t;
            t->next = tmp;
            tmp = t;
            i--;            
        }
        while(j >= 0) {
            string p = "";
            if(carry) {
                pa[j] -= carry;
                carry = 0;
            }
            p = to_string(pa[j]);
            while(p.size() < 4) {
                p = '0' + p;
            }  
            node *t = new node(p);
            ans.head->next = t;
            t->next = tmp;
            tmp = t;
            j--;              
        }
        return ans;
    }
    void print() {
        if(negative == true) cout<<'-';
        node *tmp = head->next;
        string ans = "";
        while(tmp != nullptr) {

            if(tmp == head->next) {
                const char* ts = tmp->ch.data();
                ans = ans + ts;
                // int u = atoi(ts);
                // if(u != 0)
                //     cout<<u;
            }else {
                ans = ans + tmp->ch;
                //cout<<tmp->ch;
            }
            
            tmp = tmp->next;
        }
        while(ans[0] == '0') ans = ans.substr(1);

        cout<<ans<<endl;
    }

public:
    static int carry;
    node *head;
    int level;
    bool negative;
};
int List::carry = 0;
int main()
{
    string a,b;
    while(cin>>a>>b) {
        List t1(a),t2(b);
        List ans = t1 + t2;
        List ans2 = t1 - t2;
        t1.print();
        t2.print();
        ans.print();
        ans2.print();

    }
    system("pause");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值