数据结构实现大整数运算,C语言版

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

struct Node{
    int val;
    Node* next;
};

struct LinkedList{
    Node* head;
    int len;
};

Node* createNode(int val){
    Node* node = new Node;
    node->val = val;
    node->next = nullptr;
    return node;
}
void insertNode(LinkedList& list, Node* node){
    if(list.head == nullptr){
        list.head = node;
    }else{
        Node* p = list.head;
        while(p->next != nullptr){
            p = p->next;
        }
        p->next = node;
    }
    list.len++;
}

void deleteList(LinkedList& list){
    Node* p = list.head;
    while(p != nullptr){
        Node* q = p->next;
        delete p;
        p = q;
    }
    list.len = 0;
    list.head = nullptr;
}
LinkedList add(LinkedList a, LinkedList b){
    LinkedList res = {nullptr, 0};
    int carry = 0;
    Node *p = a.head, *q = b.head;

    while(p != nullptr || q != nullptr){
        int x = p ? p->val : 0;
        int y = q ? q->val : 0;
        int sum = x + y + carry;
        carry = sum / 10;
        res.len++;
        insertNode(res, createNode(sum % 10));

        if(p) p = p->next;
        if(q) q = q->next;
    }
    if(carry){
        res.len++;
        insertNode(res, createNode(carry));
    }
    return res;
}
LinkedList sub(LinkedList a, LinkedList b){
    LinkedList res = {nullptr, 0};
    int borrow = 0;
    Node *p = a.head, *q = b.head;

    while(p){
        int x = p->val;
        int y = q ? q->val : 0;
        int diff = x - y - borrow;
        borrow = 0;
        if(diff < 0){
            diff += 10;
            borrow = 1;
        }
        res.len++;
        insertNode(res, createNode(diff));

        p = p->next;
        if(q) q = q->next;
    }
    while(res.len > 1 && res.head->val == 0){
        Node* tmp = res.head;
        res.head = res.head->next;
        delete tmp;
        res.len--;
    }
    return res;
}
LinkedList mul(LinkedList a, LinkedList b){
    LinkedList res = {nullptr, 0};
    Node *p = a.head, *q = b.head;

    vector<LinkedList> results;
    while(p){
        int carry = 0;
        LinkedList tmp = {nullptr, 0};
        for(int i = 0; i < a.len - tmp.len; i++){
            insertNode(tmp, createNode(0));
        }
        while(q){
            int prod = p->val * q->val + carry;
            carry = prod / 10;
            insertNode(tmp, createNode(prod % 10));
            q = q->next;
        }
        if(carry){
            insertNode(tmp, createNode(carry));
        }
        results.push_back(tmp);
        p = p->next;
        q = b.head;
    }
for(auto& r: results){
        LinkedList tmp = {nullptr, 0};
        for(int i = 0; i < results.size() - tmp.len; i++){
            insertNode(tmp, createNode(0));
        }
        Node *p = res.head, *q = r.head;

        int carry = 0;
        while(p || q){
            int x = p ? p->val : 0;
            int y = q ? q->val : 0;
            int sum = x + y + carry;
            carry = sum / 10;
            insertNode(tmp, createNode(sum % 10));

            if(p) p = p->next;
            if(q) q = q->next;
        }
        if(carry){
            insertNode(tmp, createNode(carry));
        }
        deleteList(res);
        res = tmp;
    }
    return res;
}
int compare(LinkedList a, LinkedList b){
    if(a.len > b.len){
        return 1;
    }else if(a.len < b.len){
        return -1;
    }else{
        Node *p = a.head, *q = b.head;
        while(p && q){
            if(p->val > q->val){
                return 1;
            }else if(p->val < q->val){
                return -1;
            }
            p = p->next;
            q = q->next;
        }
        return 0;
    }
}
void addOne(LinkedList& list){
    Node* p = list.head;
    int carry = 1;
    while(p){
        int sum = p->val + carry;
        carry = sum / 10;
        p->val = sum % 10;
        p = p->next;
    }
    if(carry){
        Node* node = createNode(carry);
        node->next = list.head;
        list.head = node;
        list.len++;
    }
}

LinkedList div(LinkedList a, LinkedList b){
    LinkedList res = {nullptr, 0};
    if(compare(a, b) < 0){
        insertNode(res, createNode(0));
        return res;
    }
LinkedList tmp = {nullptr, 0};
    Node* p = a.head;
    while(p){
        insertNode(tmp, createNode(p->val));
        p = p->next;

        while(compare(tmp, b) >= 0){
            addOne(res);
            tmp = sub(tmp, b);
        }
    }

    deleteList(tmp);
    return res;
}

LinkedList mod(LinkedList a, LinkedList b){
    if(compare(a, b) < 0){
        return a;
    }

    LinkedList res = {nullptr, 0};
    LinkedList tmp = {nullptr, 0};
    Node* p = a.head;
    while(p){
        insertNode(tmp, createNode(p->val));
        p = p->next;

        while(compare(tmp, b) >= 0){
            tmp = sub(tmp, b);
        }
    }
    if(tmp.len == 0){
        insertNode(res, createNode(0));
    }else{
        Node* q = tmp.head;
        while(q){
            insertNode(res, createNode(q->val));
            q = q->next;
        }
    }

    deleteList(tmp);
    return res;
}

LinkedList strToList(string s){
    LinkedList res = {nullptr, 0};
    for(int i = s.size() - 1; i >= 0; i--){
        insertNode(res, createNode(s[i] - '0'));
    }
    return res;
}
string listToStr(LinkedList list){
    string s;
    Node* p = list.head;
    while(p){
        s += (char)(p->val + '0');
        p = p->next;
    }
    reverse(s.begin(), s.end());
    return s;
}

int main(){
    while(true){
        cout << "*************************************************\n";
        cout << "大整数四则运算计算器★|请输入第1个长整数:";
        string s1;
        cin >> s1;
        LinkedList a = strToList(s1);

        cout << "请输入第2个长整数:";
        string s2;
        cin >> s2;
        LinkedList b = strToList(s2);
        cout << "*************************************************\n";
        cout << "请选择菜单(0-5):\n";
        cout << "0. 退出程序\n";
        cout << "1. 加法运算\n";
        cout << "2. 减法运算\n";
        cout << "3. 乘法运算\n";
        cout << "4. 除法运算\n";
        cout << "5. 取模运算\n";
        int choice;
        cin >> choice;

        LinkedList c, d, e, f, g;
        switch(choice){
            case 0:
                deleteList(a);
                deleteList(b);
                return 0;
            case 1:
                c = add(a, b);
                cout << listToStr(a) << " + " << listToStr(b) << " = " << listToStr(c) << endl;
                deleteList(c);
                break;
            case 2:
                d = sub(a, b);
                cout << listToStr(a) << " - " << listToStr(b) << " = " << listToStr(d) << endl;
                deleteList(d);
                break;
                case 3:
                e = mul(a, b);
                cout << listToStr(a) << " x " << listToStr(b) << " = " << listToStr(e) << endl;
                deleteList(e);
                break;
            case 4:
                f = div(a, b);
                cout << listToStr(a) << " / " << listToStr(b) << " = " << listToStr(f) << endl;
                deleteList(f);
                break;
            case 5:
                g = mod(a, b);
                cout << listToStr(a) << " % " << listToStr(b) << " = " << listToStr(g) << endl;
                deleteList(g);
                break;
            default:
                cout << "输入不合法,请重新选择!\n";
        }

        deleteList(a);
        deleteList(b);

        if(choice){
            cout << "※按任意键返回主菜单,继续其他类型运算...※\n";
            getchar();
            getchar();

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值