大整數加法(鏈表實現)

實現任意範圍的兩個整數的加法( 整數的範圍用 int 型的變量無法表示)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct node{
    int score;
    struct node *next;
}*link, Node;

void plusSuper(){
    char *a = (char*)malloc(50);
    char *b = (char*)malloc(50);
    char ch, ah;
    int sum = 0, flag;
    Node *p, *head, *head1, *newhead;
    head = head1 = newhead = NULL;
    while(gets(a)!=NULL && gets(b)!=NULL){
        while(*a != '\0'){
           p = (Node*)malloc(sizeof(Node));
           p->score = *a - 48;
           p->next = NULL;
           if(head == NULL){
                head = p;
           }
           else{
                p->next = head;
                head = p;
           }
           a++;
        }
        while(*b != '\0'){
           p = (Node*)malloc(sizeof(Node));
           p->score = *b - 48;
           p->next = NULL;
           if(head1 == NULL){
                head1 = p;
           }
           else{
                p->next = head1;
                head1 = p;
           }
           b++;
        }
        //plus
        flag = 0;//進位標誌
        while(head!=NULL && head1!=NULL){
            sum = head->score + head1->score + flag;
            if(newhead == NULL){
                newhead = head;
                head = head->next;
                newhead->next = NULL;
                if(sum > 9){
                    newhead->score = sum - 10;
                    flag = 1;
                }
                else{
                    newhead->score = sum;
                }
            }
            else{
                p = head->next;
                head->next = newhead;
                newhead = head;
                head = p;
                if(sum < 10){
                    newhead->score = sum;
                    flag = 0;
                }
                else{
                    newhead->score = sum - 10;
                    flag = 1;
                }
            }
            p = head1;
            head1 = head1->next;
            free(p);
        }

        if(head == NULL && head1 == NULL){
            if(flag == 1){
                p = (Node*)malloc(sizeof(Node));
                p->score = 1;
                p->next = newhead;
                newhead = p;
            }
        }
        if(head == NULL){
            head = head1;
        }
        while(head != NULL){
            sum = head->score + flag;
            p = head->next;
            head->next = newhead;
            newhead = head;
            head = p;
            if(sum > 9){
                newhead->score = sum - 10;
                flag = 1;
            }
            else{
                newhead->score = sum;
                flag = 0;
            }
        }
        //flag 的處理
        if(flag == 1){
            p = (Node*)malloc(sizeof(Node));
            p->score = 1;
            p->next = newhead;
            newhead = p;
        }
        //The output of result
        while(newhead != NULL){
            printf("%d", newhead->score);
            newhead = newhead->next;
        }
        printf("\n");
        head = head1 = newhead = NULL;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章