PAT 3-04 一元多项式的乘法与加法运算(C语言实现)

题目描述:

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

输入格式说明:

输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式说明:

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

样例输入与输出:

序号 输入 输出
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
2
2 1 2 1 0
2 1 2 -1 0
1 4 -1 0
2 2
3
2 -1000 1000 1000 0
2 1000 1000 -1000 0
-1000000 2000 2000000 1000 -1000000 0
0 0
4
0
1 999 1000
0 0
999 1000

解答说明:

链表的每一个节点储存有系数和指数。
求和的时候如果指数相等,则将系数相加,注意系数和为0的情况。
求积的过程相当于多次多次求和的过程,调用求和函数累加即可。

源码:
#include<stdio.h>
#include<stdlib.h>

typedef struct node *ptrNode;
typedef ptrNode LinkList;  //头结点
typedef ptrNode Position;//中间节点
typedef int ElementType;
struct node{
	ElementType Coefficient;
	ElementType Exponent;
	Position next;
};

int IsEmpty(LinkList L)
{
	return L->next == NULL;
}

LinkList creatList(int n)
{
	LinkList head,r,p;
	int coe,exp;
	head = (struct node*)malloc(sizeof(struct node));    //生成新结点
	r = head;

	while(n--){
        scanf("%d%d",&coe,&exp);
		p = (struct node*)malloc(sizeof(struct node));
		p->Coefficient = coe;
        p->Exponent = exp;
		r->next = p;
		r = p;
	}
	r->next = NULL;
	return head;
}

LinkList add_List(LinkList a, LinkList b)
{
	Position ha, hb;
	LinkList c,r,p;
	int temp;

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

	c = (struct node*)malloc(sizeof(struct node));
	r = c;
	while((ha != NULL)&&(hb != NULL)){
		p = (struct node*)malloc(sizeof(struct node));
		if(ha->Exponent < hb->Exponent){
			p->Exponent = hb->Exponent;
			p->Coefficient = hb->Coefficient;
			hb = hb->next;
            r->next = p;
            r = p;
		}
		else if(ha->Exponent > hb->Exponent){
			p->Exponent = ha->Exponent;
			p->Coefficient = ha->Coefficient;
			ha = ha->next;
            r->next = p;
            r = p;
		}
		else{
            temp = ha->Coefficient + hb->Coefficient;
            if(temp != 0){
                p->Exponent = ha->Exponent;
                p->Coefficient = temp;
                r->next = p;
                r = p;
            }
            hb = hb->next;
            ha = ha->next;
        }
	}
	if(ha == NULL){
		while(hb != NULL){
			p = (struct node*)malloc(sizeof(struct node));
			p->Exponent = hb->Exponent;
			p->Coefficient = hb->Coefficient;
			hb = hb->next;
			r->next = p;
		    r = p;
		}
	}
	if(hb == NULL){
		while(ha != NULL){
			p = (struct node*)malloc(sizeof(struct node));
			p->Exponent = ha->Exponent;
			p->Coefficient = ha->Coefficient;
			ha = ha->next;
			r->next = p;
		    r = p;
		}
	}

	r->next = NULL;
	return c;
}


LinkList mul_List(LinkList a, LinkList b)
{
	Position ha, hb;
	LinkList c,tempC,r,p;
	ha = a->next;
    hb = b->next;
	//c = (struct node*)malloc(sizeof(struct node));
	//c->next = NULL;
    c = creatList(0);
	if(ha == NULL || hb == NULL){
        return c;
	}

	while(ha != NULL ){
        tempC = (struct node*)malloc(sizeof(struct node));
        r = tempC;
        hb = b->next;
        while(hb != NULL){
            p = (struct node*)malloc(sizeof(struct node));
            p->Exponent = ha->Exponent + hb->Exponent;
            p->Coefficient = ha->Coefficient*hb->Coefficient;
			hb = hb->next;
            r->next = p;
            r = p;
        }
        r->next = NULL;
        c = add_List(c,tempC);  //?????为什么不行?结果总是第一次加后的结果

//        tempHead = tempC->next;
//        tempC->next = NULL;
//        while(tempHead != NULL){
//            free(tempHead);
//            tempHead = tempHead->next;
//        }
        ha = ha->next;
	}
	return c;

}
void printList(LinkList L)
{
	LinkList hc;
	int flag = 0;

	hc = L->next;
	if(hc == NULL)
		printf("0 0");
	while(hc != NULL){
		if(flag)
			printf(" ");
		else
			flag = 1;
		printf("%d %d",hc->Coefficient,hc->Exponent);
		hc = hc->next;
	}
}

int main(void)
{
    int n1,n2;
	LinkList L1,L2,L3,L4;

    scanf("%d",&n1);
	L1 = creatList(n1);
	scanf("%d",&n2);
	L2 = creatList(n2);

	L3 = add_List(L1,L2);
    L4 = mul_List(L1,L2);
	printList(L4);
	printf("\n");
	printList(L3);

	return 0;
}









阅读更多
个人分类: PAT
想对作者说点什么? 我来说一句

多项式乘法(C语言

2009年06月14日 21KB 下载

c语言实现多项式乘法

2011年12月13日 257KB 下载

没有更多推荐了,返回首页

不良信息举报

PAT 3-04 一元多项式的乘法与加法运算(C语言实现)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭