两个一元多项式求乘积。polyn1和polyn2分别表示两个一元多项式,是带头结点单链表。求积后的一元多项式使用polyn3来表示。输入每对数据分别表示系数和指数,均为0表示输入结束(系数为0不做处理,指数重复或升序不做处理)。
图中第2行、第4行为键盘输入。
思路:
1. insert函数用来对head3做插入,判断有无指数,保持指数先大后小,系数相加是否为0。用q->next做判断来方便尾插。
2.第二个函数做乘法
#define _CRT_SECURE_NO_WARNINGS
//带头结点链表,多项式求积
#include <stdio.h>
#include <stdlib.h>
typedef struct _node
{
int coefficient; //系数
int exponent; //指数
struct _node* next;
} Node, * pNode, * Link;
int initialize(Link* head); //创建头结点,初始化无元素的带头结点的空链表
int create(Link head);
int insert(Link head, int coef, int expo);
int polynomial_mul(Link head1, Link head2, Link head3); //求积后的数据放置在head3链表中
void output(Link head);
void destroy(Link head);
int main(void)
{
Link polyn1, polyn2, polyn3;
if (initialize(&polyn1) <= 0)
{
return 0;
}
if (initialize(&polyn2) <= 0)
{
destroy(polyn1);
return 0;
}
if (initialize(&polyn3) <= 0)
{
destroy(polyn1);
destroy(polyn2);
return 0;
}
printf("输入第1个多项式的系数和指数(都为0表示结束):\n");
if (create(polyn1) < 0)
{
return 0;
}
printf("输入第2个多项式的系数和指数(都为0表示结束):\n");
if (create(polyn2) < 0)
{
return 0;
}
printf("第1个多项式为:\n");
output(polyn1);
printf("第2个多项式为:\n");
output(polyn2);
if (polynomial_mul(polyn1, polyn2, polyn3) > 0)
{
printf("Output:\n两个多项式求积结果为:\n");
output(polyn3);
}
destroy(polyn1);
destroy(polyn2);
destroy(polyn3);
return 0;
}
int initialize(Link* head)
{
*head = (pNode)malloc(sizeof(Node));
if (*head == NULL)
{
return -1;
}
(*head)->next = NULL;
return 1;
}
int create(Link head)
{
int coef; //系数
int expo; //指数
pNode p, q;
if (head == NULL)
{
return -1;
}
q = head;
scanf("%d%d", &coef, &expo);
while (coef != 0 || expo != 0)
{
if (coef != 0) //系数不为0,才存储到链表中
{
if (q == head) //第1个数据结点
{
p = (pNode)malloc(sizeof(Node));
if (p == NULL)
{
destroy(head);
return -1;
}
p->coefficient = coef;
p->exponent = expo;
q->next = p;
q = p;
}
else
{
if (q->exponent > expo) //链表中指数按降序存放
{
p = (pNode)malloc(sizeof(Node));
if (p == NULL)
{
destroy(head);
return -1;
}
p->coefficient = coef;
p->exponent = expo;
q->next = p;
q = p;
}
}
//不符合要求的输入也不存入
}
scanf("%d%d", &coef, &expo);
}
q->next = NULL;
return 1;
}
void output(Link head)
{
pNode p;
if (head == NULL)
{
return;
}
p = head->next;
if (p == NULL) //空链表,相当于只有0值,系数为0的结点不存于链表中
{
printf("0\n");
return;
}
while (p->next != NULL)
{
if (p != head->next)
{
if (p->coefficient > 0)
{
printf("+ %d", p->coefficient);
}
else
{
printf("- %d", -p->coefficient);
}
}
else
{
printf("%d", p->coefficient);
}
printf("*x^%d ", p->exponent);
p = p->next;
}
if (p->exponent != 0)
{
if (p->coefficient > 0)
{
printf("+ %d", p->coefficient);
}
else
{
printf("- %d", -p->coefficient);
}
printf("*x^%d", p->exponent);
}
else
{
if (p->coefficient > 0)
{
printf("+ %d", p->coefficient);
}
else
{
printf("- %d", -p->coefficient);
}
}
printf("\n");
}
void destroy(Link head)
{
pNode p;
while (head != NULL)
{
p = head->next;
free(head);
head = p;
}
}
/******start******/
int insert(Link head, int coef, int expo) {
Link p, q,t=NULL;
p = (Link)malloc(sizeof(Node));
q = head->next;
if (p == NULL) {
return -1;
}
p->next = NULL;
p->coefficient = coef;
p->exponent = expo;
if (head->next == NULL) {//只有头结点
head->next = p;
return 1;
}
while ( q->next != NULL && (q->next->exponent > expo)) {
q = q->next;//注意while里的顺序
}
if (q->next == NULL||q->next->exponent<expo) {//做尾插
p->next = q->next;
q->next = p;
}
else if (q->next->exponent == expo) {//不插入
q->next->coefficient += coef;//考虑sum==0情况
if (q->next->next==NULL&& q->next->coefficient == 0) {//q后面只有一个结点
q->next = NULL;
}
else if (q->next->coefficient==0) {//q后面有至少两个节点
t = q->next;
q->next = q->next->next;
}
free(p);
free(t);
}
return 1;
}
int polynomial_mul(Link head1, Link head2, Link head3) {
//return -1;
int i, j, tp1, tp2;
Link p, q;
p = head1->next;
q = head2->next;
while (p != NULL) {
q = head2->next;
while (q != NULL) {
tp1 = p->coefficient * q->coefficient;
tp2 = p->exponent + q->exponent;//指数+
insert(head3, tp1, tp2);
q = q->next;
}
p = p->next;
}
return 1;
}
/******end******/