程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 一元多項式的乘法與加法運算(C語言),多項式乘法

一元多項式的乘法與加法運算(C語言),多項式乘法

編輯:關於C語言

一元多項式的乘法與加法運算(C語言),多項式乘法


輸入格式:

輸入分2行,每行分別先給出多項式非零項的個數,再以指數遞降方式輸入一個多項式非零項系數和指數(絕對值均為不超過1000的整數)。數字間以空格分隔。

輸出格式:

輸出分2行,分別以指數遞降方式輸出乘積多項式以及和多項式非零項的系數和指數。數字間以空格分隔,但結尾不能有多余空格。零多項式應輸出0 0

輸入樣例:

4 3 4 -5 2  6 1  -2 0
3 5 20  -7 4  3 1

輸出樣例:

15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0

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

typedef struct polyNode{
    int coef;
    int exp;
    struct polyNode *next;
}polyNode, *polyList;

void DestroyList(polyList L);

void printList(polyList L);

polyList creatList(int n);

polyList add(polyList a, polyList b);

polyList mul(polyList a, polyList b);

int main()
{
    int n1, n2, i;
    polyList a, b, L1, L2;

    scanf("%d", &n1);
    a = creatList(n1);
    scanf("%d", &n2);
    b = creatList(n2);

    L1 = mul(a, b);
    L2 = add(a, b);
    
    
    printList(L1);
    printf("\n");
    printList(L2);
    DestroyList(L1);
    DestroyList(L2);
    //system("pause");
    return 0;
}

void DestroyList(polyList L)
{
    polyNode * tmp;
    while (L)
    {
        tmp = L->next;
        free(L);
        L = tmp;
    }
}

polyList creatList(int n)
{
    polyNode *head, *r, *p;
    int coef, exp;
    head = (polyNode *)malloc(sizeof(polyNode));
    r = head;

    while (n--)
    {
        scanf("%d%d", &coef, &exp);
        p = (polyNode *)malloc(sizeof(polyNode));
        p->coef = coef;
        p->exp = exp;

        r->next = p;
        r = p;
    }
    r->next = NULL;
    return head;
}

polyList add(polyList a, polyList b)
{
    polyNode *ha, *hb, *p, *r, *h;
    int temp;

    ha = a->next;
    hb = b->next;

    h = (polyNode *)malloc(sizeof(polyNode));
    r = h;

    while (ha != NULL && hb != NULL)
    {
        p = (polyNode *)malloc(sizeof(polyNode));
        if (ha->exp < hb->exp)
        {
            p->exp = hb->exp;
            p->coef = hb->coef;
            hb = hb->next;

            r->next = p;
            r = p;
        }
        else if (ha->exp > hb->exp)
        {
            p->exp = ha->exp;
            p->coef = ha->coef;
            ha = ha->next;

            r->next = p;
            r = p;
        }
        else
        {
            temp = ha->coef + hb->coef;
            if (temp != 0)
            {
                p->exp = ha->exp;
                p->coef = temp;
        
                r->next = p;
                r = p;
            }
            ha = ha->next;
            hb = hb->next;
        }
    }
    while ( hb != NULL )
    {
        p = (polyNode *)malloc(sizeof(polyNode));
        p->exp = hb->exp;
        p->coef = hb->coef;
        hb = hb->next;
        r->next = p;
        r = p;
    }
    while ( ha != NULL )
    {
        p = (polyNode *)malloc(sizeof(polyNode));
        p->exp = ha->exp;
        p->coef = ha->coef;
        ha = ha->next;
        r->next = p;
        r = p;
    }
    r->next = NULL;
    DestroyList(a);
    DestroyList(b);
    return h;
}

polyList mul(polyList a, polyList b)
{
    polyNode *ha, *hb , *r, *p;
    polyList c, tempc;

    ha = a->next;
    hb = b->next;
    
    c = (polyNode *)malloc(sizeof(polyNode));
    c->next = NULL;
    
    if (ha == NULL || hb == NULL)
    {
        return c;
    }

    while (ha != NULL)
    {
        tempc = (polyNode *)malloc(sizeof(polyNode));
        r = tempc;
        hb = b->next;
        while (hb != NULL)
        {
            p = (polyNode *)malloc(sizeof(polyNode));
            p->exp = ha->exp + hb->exp;
            p->coef = ha->coef * hb->coef;
            
            hb = hb->next;
            r->next = p;
            r = p;
        }
        r->next = NULL;
        c = add(c,tempc);
        ha = ha->next;
    }
    return c;
}

void printList(polyList L)
{
    polyNode * x;
    x = L->next;
    if (x == NULL)
    {
        printf("0 0");
    }
    while (x != NULL)
    {
        if (x->next == NULL)
        {
            printf("%d %d", x->coef, x->exp);
        }
        else
            printf("%d %d ", x->coef, x->exp);
        x = x->next;
    }
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved