>

设计函数分别求两个一元多项式的乘积与和,数

- 编辑:澳门博发娱乐官网 -

设计函数分别求两个一元多项式的乘积与和,数

设计函数分别求两个一元多项式的乘积与和。

输出样例:

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>

typedef struct PolyNode *Polynomial;
struct PolyNode {
    int coef;
    int expon;
    Polynomial link; 
};

void Attach(int c,int e,Polynomial *pRear) {
    Polynomial P;
    P=(Polynomial)malloc(sizeof(struct PolyNode));
    P->coef=c;
    P->expon=e;
    P->link=NULL;
    (*pRear)->link=P; /*P指向的新结点插入到当前尾项的后面*/
    *pRear=P;   /*pRear指针移动到 P结点*/
}

int Compare(int a,int b){
    int c;
    if (a>b) c=1;
    else if (a<b) c=-1;
    else c=0;
    return c;
}

Polynomial ReadPoly(){
    Polynomial P,Rear,t;
    int c,e,N;

    scanf("%d",&N);
    P=(Polynomial)malloc(sizeof(struct PolyNode));/*链表头空结点*/
    P->link=NULL;
    Rear=P;
    while(N--){
        scanf("%d %d",&c,&e);
        Attach(c,e,&Rear);   /*将当前项插入多项式尾部*/
    } 
    t=P;P=P->link;free(t); /*删除临时生成的头结点*/
    return P;
}

Polynomial Add(Polynomial P1,Polynomial P2){
    Polynomial front,rear,temp;
    int sum;
    rear=(Polynomial)malloc(sizeof(struct PolyNode));
    front=rear;//front 用来记录链表头结点
    while(P1&&P2){

        switch(Compare(P1->expon,P2->expon)){
            case 1:
                Attach(P1->coef,P1->expon,&rear);
                P1=P1->link;
                break;
            case -1:
                Attach(P2->coef,P2->expon,&rear);
                P2=P2->link;
                break;
            case 0:
                sum=P1->coef+P2->coef;
                if (sum){
                    Attach(sum,P1->expon,&rear);
                }
                P1=P1->link;
                P2=P2->link;
                break;

        }
    }
    for(;P1;P1=P1->link) Attach(P1->coef,P1->expon,&rear);
    for(;P2;P2=P2->link) Attach(P2->coef,P2->expon,&rear);
    rear->link=NULL;
    temp=front;
    front=front->link;
    free(temp);
    return front;
} 

Polynomial Mult(Polynomial P1,Polynomial P2){
    Polynomial P,Rear,t1,t2,t;
    int c,e;
    if (!P1||!P2) return NULL;//判断如果P1或者P2有一个为NULL,则结果为NULL; 

    t1=P1;t2=P2;
    P=(Polynomial)malloc(sizeof(struct PolyNode));
    P->link=NULL;
    Rear=P;
    while(t2){
        Attach(t1->coef*t2->coef,t1->expon+t2->expon,&Rear);
        t2=t2->link;
    } 
    t1=t1->link;//t1已经乘好,后移 
    while (t1){
        t2=P2;Rear=P;
        while(t2){
            e=t1->expon+t2->expon;
            c=t1->coef*t2->coef;
            while(Rear->link && Rear->link->expon>e)
            Rear=Rear->link;//当相乘得到的 e小于多项式系数时,Rear指针往后移 
            if(Rear->link && Rear->link->expon==e){
                if(Rear->link->coef+c)
                    Rear->link->coef+=c;//如果多项式系数的和不为 0,则用新的系数赋值 
                else{
                    t=Rear->link;
                    Rear->link=t->link;
                    free(t);//如果多项式的系数为0,则 Rear指针指向下一个项,这个结点释放 
                }
            }
            else{
                t=(Polynomial)malloc(sizeof(struct PolyNode));
                t->coef=c;t->expon=e;
                t->link=Rear->link;
                Rear->link=t;Rear=Rear->link;//如果多项式没有到最后并且目前没有这一项,则分配内存空间,新建结点,并将结点插入链表,Rear指针指到新结点 t 
            } 
        t2=t2->link;     
        }
    t1=t1->link;    
    }
t2=P;P=P->link;free(t2);
return P;
}

void PrintPoly(Polynomial P){
    int flag=0;  //用于辅助调整输出格式

    if(!P) {printf("0 0n");return;}

    while (P){
        if(!flag) flag=1;
        else printf(" ");
        printf("%d %d",P->coef,P->expon);
        P=P->link;
    }
    printf("n");
} 

int main()
{
    Polynomial P1,P2,PP,PS;
    P1=ReadPoly();
    P2=ReadPoly();
    PP=Mult(P1,P2);
    PrintPoly(PP);
    PS=Add(P1,P2);
    PrintPoly(PS);

    return 0;
}

 

 

设计函数分别求两个一元多项式的乘积与和。 输入格式: 输入分2行,每行分别先给...

输入样例:

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

输入样例:

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;
    }
}

 

输入格式: 输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个...

输入样例:

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

输出格式:

输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0

输出格式:

输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0

输出格式:

输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0

输出格式:

输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0

本文由胜博发-编程发布,转载请注明来源:设计函数分别求两个一元多项式的乘积与和,数