浙大PAT:一元多項式的加法與乘法

PAT:一元多項式的加法與乘法

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

typedef struct Node{
    int xs;
    int zs;
    struct Node* next;
}Node,*List;

void Init(List *L){
    *L = (List)malloc(sizeof(Node));
    (*L)->xs = 0;
    (*L)->zs = 0;
    (*L)->next = NULL;
}
void Read(List L){
    int n;
    scanf("%d",&n);
    while(n--){
        Node* N = (Node*)malloc(sizeof(Node));
        scanf("%d %d",&N->xs,&N->zs);
        N->next = NULL;
        L->next = N;
        L = L->next;
    }
}

void PrintfList(List L){
    L = L->next;
    if(L==NULL){
    	printf("0 0");
    	return;
	}
    printf("%d %d",L->xs,L->zs);
    L = L->next;
    while(L){
        printf(" %d %d",L->xs,L->zs);
        L = L->next;
    }
}

void copy(List L1,List L2){     //拷貝一份L1給L2
    Node* p1 = L1->next;
    Node* pL = L2;
    while(p1){  
        Node* N = (Node*)malloc(sizeof(Node));
        N->xs = p1->xs;
        N->zs = p1->zs;
        N->next = NULL;
        pL->next = N;
        pL = pL->next;
        p1 = p1->next;
    }
    pL->next = NULL;
}

List SUM(List L1,List L2){		//將L2加到L1上 
    List L3 = L1;
    Node* N;
    Node* p1 = L1->next;
    Node* p2 = L2->next;
    L3->next = NULL;
    N = L3;
    while(p1&&p2){
        if(p1->zs>p2->zs){
            N->next = p1;
            p1 = p1->next;
            N = N->next;
            N->next = NULL;
        }else if(p1->zs<p2->zs){
        	Node* p = (Node*)malloc(sizeof(Node));		//複製當前p2結點 
        	p->xs = p2->xs;
        	p->zs = p2->zs;
        	p->next = NULL; 
        	
            N->next = p;
            p2 = p2->next;
            N = N->next;
        }
        else{
            p1->xs += p2->xs;
            if(p1->xs!=0){
            	N->next = p1;
            	N = N->next;
			}
			p1 = p1->next;
			p2 = p2->next;
			N->next = NULL;
        }
    }
    if(p1)  N->next = p1;
    else if(p2){
    	Node* p = (Node*)malloc(sizeof(Node));		//複製當前p2結點 
        	p->xs = p2->xs;
        	p->zs = p2->zs;
        	p->next = NULL;
        	
            N->next = p;
            N = N->next;
            
            copy(p2,N);
	} 
    return L3;
}

List MUL(List L1,List L2){

    List sum;
    Init(&sum);

    Node* p1 = L1->next;
    Node* p2 = L2->next;

    List L3;
    Init(&L3);

    copy(L1,L3);
    Node* p3 = L3->next;

    if(p1==NULL||p2==NULL)  return sum;

    while(p2){
        while(p1){
            p3->xs = p1->xs*p2->xs;
            p3->zs = p1->zs+p2->zs;
            p1 = p1->next;
            p3 = p3->next;
        }
        p3 = L3->next;
        p1 = L1->next;
        sum = SUM(sum,L3);
        p2 = p2->next;
    }

    return sum;
}

int main(){
	freopen("1.txt","r",stdin);
    List L1,L2,sum,mul;
    
    Init(&L1);
    Init(&L2);
    Init(&sum);
    Init(&mul);
    
    Read(L1);
    Read(L2);
    copy(L1,sum);
    
    sum = SUM(sum,L2);
    mul = MUL(L1,L2);
    
    PrintfList(mul);
    printf("\n");
    PrintfList(sum);
    
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章