Polynomial multiplication and addition

Polynomial multiplication and addition

Exponentially descending input and output The
design function finds the product and sum of two univariate polynomials respectively.

Input format: The
input is divided into 2 lines. Each line gives the number of non-zero terms of the polynomial, and then enters the coefficient and exponent of a polynomial non-zero term in exponential descending mode (the absolute value is an integer not exceeding 1000). The numbers are separated by spaces.

Output format: The
output is divided into 2 lines, and the coefficients and exponents of the product polynomial and the non-zero term of the sum polynomial are output in exponential descending manner. The numbers are separated by spaces, but no extra spaces are allowed at the end. The zero polynomial should output 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;
}

Guess you like

Origin blog.csdn.net/qq_40602655/article/details/111026357