设为首页 加入收藏

TOP

一元多项式的乘法与加法运算(C语言)(一)
2017-10-12 17:39:22 】 浏览:4740
Tags:一元 多项 乘法 加法 运算 语言

输入格式:

输入分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
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇19. Remove Nth Node From End of.. 下一篇关于C语言函数调用压栈和返回值问..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目