user-0 提问: 一元多项式计算器。乘法问题

回复:
user: user-1
content: 修改如下：  #include<stdio.h> #include<stdlib.h> #include<conio.h> typedef struct Polynomial { 	int coef;//系数 	int expn;//指数 	struct Polynomial *next; }*Poly, Node; Poly Creat() { 	Node *head, *rear, *s; 	int e; 	int c; 	head = (Poly)malloc(sizeof(Node));  //头节点 	rear = head;//尾插法 	printf("请输入一个一元多项式，形如（系数c，指数e，c=0退出）\n"); 	scanf("%d,%d", &c, &e); 	while (c != 0)//c=0则结束 	{ 		s = (Poly)malloc(sizeof(Node)); 		s->coef = c; 		s->expn = e; 		rear->next = s; 		rear = s; 		scanf("%d,%d", &c, &e); 	} 	rear->next = NULL; 	return (head); } void show(Poly p) { 	Poly q = p->next; 	int flag = 1; 	if (!q) 	{ 		putchar('0'); 		printf("\n"); 		return; 	} 	while (q) 	{ 		if (q->coef > 0 && flag != 1) 			putchar('+'); 		if (q->coef != 1 && q->coef != -1) 		{ 			printf("%d", q->coef); 			if (q->expn == 1) 				putchar('X'); 			else if (q->expn) 				printf("X^%d", q->expn); 		} 		else 		{ 			if (q->coef == 1) 			{ 				if (!q->expn) 					putchar('1'); 				else if (q->expn == 1) 					putchar('X'); 				else 					printf("X^%d", q->expn); 			} 			if (q->coef == -1) 			{ 				if (!q->expn) 					printf("-1"); 				else if (q->expn == 1) 					printf("-X"); 				else 					printf("-X^%d", q->expn); 			} 		} 		q = q->next; 		flag++; 	} 	printf("\n"); } Poly Add(Poly pa, Poly pb) { 	Poly qa = pa->next; 	Poly qb = pb->next; 	Poly headc, pc, temp; 	pc = (Poly)malloc(sizeof(Node)); 	pc->next = NULL; 	headc = pc; 	while (qa != NULL && qb != NULL) 	{ 		temp = (Poly)malloc(sizeof(Node)); 		if (qa->expn < qb->expn) 		{ 			temp->expn = qa->expn; 			temp->coef = qa->coef; 			qa = qa->next; 		} 		else if (qa->expn == qb->expn) 		{ 			temp->expn = qa->expn; 			temp->coef = qa->coef + qb->coef; 			qa = qa->next; 			qb = qb->next; 		} 		else 		{ 			temp->expn = qb->expn; 			temp->coef = qb->coef; 			qb = qb->next; 		} 		if (temp->coef != 0) 		{ 			//temp->next = pc->next; 			temp->next = NULL;   //更直接 			pc->next = temp; 			pc = temp; 		} 		else 			free(temp); 	} 	while (qa != NULL) 	{ 		temp = (Poly)malloc(sizeof(Node)); 		temp->expn = qa->expn; 		temp->coef = qa->coef; 		qa = qa->next; 		//temp->next = pc->next; 		temp->next = NULL;   //更直接 		pc->next = temp; 		pc = temp; 	} 	while (qb != NULL) 	{ 		temp = (Poly)malloc(sizeof(Node)); 		temp->expn = qb->expn; 		temp->coef = qb->coef; 		qb = qb->next; 		//temp->next = pc->next; 		temp->next = NULL;   //更直接 		pc->next = temp; 		pc = temp; 	} 	return headc; } Poly diff(Poly pa, Poly pb)  //减法 { 	Poly qb = pb->next; 	Poly pd = NULL; 	//zx 以下两句没用 	//pd = (Poly)malloc(sizeof(Node)); 	//pd->next = NULL; 	while (qb) 	{ 		qb->coef = -qb->coef; 		qb = qb->next; 	} 	pd = Add(pa, pb); 	//zx 恢复pb中的指数 	for (qb = pb->next; qb != NULL; qb = qb->next) 		qb->coef *= -1;//存疑 	return pd; } Poly Cheng(Poly pa, Poly pb) { 	Poly qa = pa->next; 	Poly qb = pb->next; 	Poly head, pe, temp; 	Poly sum = NULL; 	head = (Poly)malloc(sizeof(Node)); 	head->next = NULL; 	//sum = (Poly)malloc(sizeof(Node)); 	//sum->next = NULL; 	//pe->next = NULL; 	//head = pe; 	if (qa != NULL && qb != NULL) 	{ 		for (qa = pa->next; qa != NULL; qa = qa->next) 		{ 			sum = (Poly)malloc(sizeof(Node)); 			sum->next = NULL; 			pe = sum; 			for (qb = pb->next; qb != NULL; qb = qb->next) 			{ 				temp = (Poly)malloc(sizeof(Node)); 				temp->coef = qa->coef * qb->coef; 				temp->expn = qa->expn + qb->expn; 				//temp->next = pe->next; 				temp->next = NULL;   //更直接 				pe->next = temp; 				pe = temp; 				//free(temp); 				//show(heade);//ERROR,此处会出现问题。使用单步调试来看，heade里面的值是乱码。程序意外终止。若注释掉本句，下一处ERROR也会发生。 				//printf("qb=qb->next\n"); 			} 			head = Add(head, sum);//ERROR，想利用整数求和的办法。sum+=a;但是总是终止。 		} 		//return sum; 	} 	//else 	//{ 	//	head->next = NULL; 	//	return head; 	//} 	return head; } int main() { 	Poly qc; 	Poly qd; 	Poly qe; 	Node *p, *q; 	p = Creat(); 	show(p); 	q = Creat(); 	show(q);  	qc = Add(p, q); 	printf("加法："); 	show(qc);  	qd = diff(p, q); 	printf("减法："); 	show(qd);  	qe = Cheng(p, q); 	printf("乘法："); 	show(qe); 	return 0; } //请输入一个一元多项式，形如（系数c，指数e，c = 0退出） //1, 1 //2, 2 //0, 0 //X + 2X ^ 2 //请输入一个一元多项式，形如（系数c，指数e，c = 0退出） //2, 1 //3, 2 //0, 0 //2X + 3X ^ 2 //加法：3X + 5X ^ 2 //减法： - X - X ^ 2 //乘法：2X ^ 2 + 7X ^ 3 + 6X ^ 4  缺陷： 1.有内存泄露。 2.输入多项式时，必须按指数从小到大输入。

user: user-2
content: 把这一行注释掉，再试试 free(temp);

user: user-0
content: 引用 4 楼 zhangxiangDavaid 的回复:[quote=引用 3 楼 a653560991 的回复:] [quote=引用 2 楼 zhangxiangDavaid 的回复:] 修改如下： 缺陷： 1.有内存泄露。 2.输入多项式时，必须按指数从小到大输入。  多谢你啦！但是我还想咨询一个问题。 给sum分配的内存不需要free掉吗？[/quote] 执行完 head = Add(head, sum); 最好free掉，这就是其中一处的内存泄露的地方。[/quote] 好的我知道啦~多谢你啦。

user: user-1
content: 引用 3 楼 a653560991 的回复:[quote=引用 2 楼 zhangxiangDavaid 的回复:] 修改如下： 缺陷： 1.有内存泄露。 2.输入多项式时，必须按指数从小到大输入。  多谢你啦！但是我还想咨询一个问题。 给sum分配的内存不需要free掉吗？[/quote] 执行完 head = Add(head, sum); 最好free掉，这就是其中一处的内存泄露的地方。

user: user-0
content: 引用 2 楼 zhangxiangDavaid 的回复:修改如下： 缺陷： 1.有内存泄露。 2.输入多项式时，必须按指数从小到大输入。  多谢你啦！但是我还想咨询一个问题。 给sum分配的内存不需要free掉吗？