多项式乘法和加法

多项式乘法和加法

指数递降输入与输出
设计函数分别求两个一元多项式的乘积与和。

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

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

#include <stdio.h>
#include <stdlib.h>
typedef struct node *pnode;
struct node {
    
    
	int coef, expon;
	pnode next;
};
typedef pnode list;
void attach(int c, int e, pnode *p) {
    
                    //插入一个项在队尾
	
	if (c != 0) {
    
        //零项不插入,输入会输入零项后续除不掉
		pnode temp = (pnode)malloc(sizeof(struct node));
		temp->coef = c;
		temp->expon = e;
		temp->next = NULL;
		(*p)->next = temp;
		(*p) = temp;
	}
}
list read() {
    
    
	int N;
	scanf("%d",&N);
	int c, e;
	list p = (list)malloc(sizeof(struct node));
	p->next = NULL;
	list t = p;
	list temp;
	for (int i = 0; i < N; i++) {
    
    
		scanf("%d %d", &c, &e);
		attach(c, e, &t);
	}
	temp = p;
	p = p->next;
	free(temp);
	return  p;
}
list addp(list p1, list p2) {
    
    
	list a1, a2,a;
	a1 = p1;
	a2 = p2;
	list p = (list)malloc(sizeof(struct node));
	p->next = NULL;
	a = p;
	while (a1 && a2) {
    
    
		if (a1->expon < a2->expon) {
    
    
			attach(a2->coef, a2->expon, &a);
			a2 = a2->next;
		}
		else if (a1->expon> a2->expon) {
    
    
			attach(a1->coef, a1->expon, &a);
			a1= a1->next;
		}
		else {
    
    
			int m = a1->coef + a2->coef;
			if (m != 0)   attach(m, a1->expon, &a);
			a1 = a1->next;
			a2 = a2->next;
		}			
		}
	while (a1) {
    
    
		attach(a1->coef, a1->expon, &a);
		a1 = a1->next;
	}
	while (a2) {
    
    
		attach(a2->coef, a2->expon, &a);
		a2 = a2->next;
	}
	list temp = p;
	p = p->next;
	free(temp);
	return p;
}
list multp(list p1, list p2) {
    
    
	list t1, t2,temp,t;
	int c, e, m;
	if (!p1 || !p2) return NULL;
	t1 = p1;
	t2 = p2;
	list p = (list)malloc(sizeof(struct node));
	p->next = NULL;
	list rear = p;
	while (t2) {
    
            //先创立一个链表储存p1第一项乘t2所有项得到的多项式
		attach(t1->coef*t2->coef, t1->expon + t2->expon, &rear);
		t2 = t2->next;
	}
	t1 = t1->next;
	while (t1) {
    
        //p2每一项乘p1每一项得到的多项式相加
		t2 = p2; rear = p;     //每次循环,t2回到p2第一项
		while (t2) {
    
    
			c = t1->coef*t2->coef;
			e = t1->expon + t2->expon;
			while (rear->next != NULL && rear->next->expon > e) rear = rear->next;  //,每一项的指数在指数递降的P中扫描,得到他应该插在那一项(rear->next)的前面,即rear的后面
			if (rear->next!=NULL&&e == rear->next->expon) {
    
        //合并同类项并且next不能为空,否则将空值与int比较会出问题!!
				m = c + rear->next->coef;
				if (m == 0) {
    
    
					temp = rear->next;
					rear->next = temp->next;
					free(temp);
				}
				else {
    
    
					rear->next->coef = m;
				}
			}
			else {
    
    
				t = (pnode)malloc(sizeof(struct node));
				t->coef = c;
				t->expon = e;
				t->next = rear->next;
				rear->next = t;
				rear = rear->next;
			}
			t2 = t2->next;
		}
		t1 = t1->next;
	}
	t1 = p; p = p->next; free(t1);
	return p;
}
void printp(list p) {
    
    
	pnode t = p;
	int flag = 0;
	if (t == NULL) printf("0 0");
	while (t) {
    
    
		if (!flag) flag=1;
		else printf(" ");   //第二次开始在每项前打印空格
		printf("%d %d", t->coef, t->expon);
		t = t->next;
	}
	printf("\n");
}
int main() {
    
    
	list p1, p2, p3,p4;
	p1 = read();	
	p2 = read();
p4 = multp(p1, p2);
	printp(p4);
	p3 = addp(p1, p2);
    printp(p3);
		
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40602655/article/details/111026357
今日推荐