首页 > 代码库 > 大数乘法

大数乘法

#include <iostream>#include <cstring>using namespace std;#define MAX 1000000struct 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 = http://www.mamicode.com/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 = http://www.mamicode.com/0;                p->next = NULL;                p1->next = p;            }            if(*ap == 0)                temp1 = temp;            else{                temp1 = p1->data + (*ap-48)*bit + temp;                ap++;            }            p1->data = http://www.mamicode.com/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, \n);    cin.getline(b, MAX, \n);    Mul(strrev(a), strrev(b));    return 0;}

 

大数乘法