大数乘法

#include <iostream>
#include <cstring>
using namespace std;
#define MAX 1000000
struct Node{
    int data;
    Node* next;
};

void output(Node* head){
    if(!head->next && !head->data) return;
    output(head->next);
    cout << head->data;
}

void Mul(char *a, char *b){
    char *ap = a, *bp = b;
    Node *head = new Node;
    head->data = 0;
    head->next = NULL;
    Node *p, *q=head, *p1;
    int temp=0, temp1, bit;
    while(*bp){
        p = q->next;
        p1 = q;
        bit = *bp-48;
        while(*ap || temp){
            if(!p){
                p = new Node;
                p->data = 0;
                p->next = NULL;
                p1->next = p;
            }
            if(*ap == 0)
                temp1 = temp;
            else{
                temp1 = p1->data + (*ap-48)*bit + temp;
                ap++;
            }
            p1->data = temp1 % 10;
            temp = temp1 / 10;
            p1 = p;
            p = p->next;
        }
        ap = a;
        bp++;
        q = q->next;
    }
    p = head;
    output(p);
    cout << endl;
    while(head){
        p = head->next;
        delete head;
        head = p;
    }
}
int main(){
    char a[MAX], b[MAX];
    cin.getline(a, MAX, '
');
    cin.getline(b, MAX, '
');
    Mul(strrev(a), strrev(b));
    return 0;
}
原文地址:https://www.cnblogs.com/yingl/p/5839267.html