设有两个一元多项式:
多项式项的系数为实数,指数为整数,设计实现一元多项式的下列操作:
- 多项式链表建立:以(系数,指数)(例如(3,0)(-1,1))方式输入项建立多项式,返回所建立的链表的头指针;在输入一个多项式时如果遇到两项指数相同,可以选择舍弃、覆盖或者系数相加操作;
- 多项式排序:将所建立的多项式按指数非递减(从小到大)进行排序(也可以在建立时保证有序,创建一个有序链表);3,3 2,2 4,4 1,0
- 多项式相加:实现两个多项式相加操作。操作生成一个新的多项式,原有的两个多项式不变,可以返回生成的多项式的头指针;
- 多项式相减:实现两个多项式相减操作。操作生成一个新的多项式,原有的两个多项式不变,可以返回生成的多项式的头指针;
- 多项式的输出:按照
格式输出多项式;
- 主函数通过调用多项式链表建立函数,通过文件读取或者控制台输入两个多项式并分别输出;输出排序后的两个多项式;调用多项式相加函数实现多项式相加、相减操作,输出操作结果。
/* 多项式用单链表表示 */
typedef struct Pnode
{
int deg;
float coff;
struct Pnode *next;
} PolyNode;
PolyNode * ListInitiate_poly();
int PolyListDelete(PolyNode *head, int i, PolyNode *x);
int PolyListInsert(PolyNode *head, int i, PolyNode x);
int PolyListLength(PolyNode *head);
int PolyListGet(PolyNode *head, int i, PolyNode *x);
void PolyDestroy(PolyNode *head);
PolyNode* ListInitiate_poly()//链表初始化
{
PolyNode * head = (PolyNode *)malloc(sizeof(PolyNode));
head->next = NULL;
return head;
}
int PolyListLength(PolyNode *head) //多项式的项数
{
PolyNode *p = head;
int size = 0;
while (p->next != NULL)
{
p = p->next;
size++;
}
return size;
}
int PolyListInsert(PolyNode *head, int i, PolyNode x) //在第i个位置上插入一项x
{
PolyNode *p, *q; int j;
p = head; j = -1;
while (p->next != NULL && j < i - 1)
{
p = p->next; j++;
}
if (j != i - 1)
{
printf("插入位置参数错!"); return 0;
}
q = (PolyNode *)malloc(sizeof(PolyNode));
q->coff = x.coff;
q->deg = x.deg;
q->next = p->next; p->next = q;
return 1;
}
int PolyListDelete(PolyNode *head, int i, PolyNode *x)//从单链表删除1项
{
PolyNode *p, *s; int j;
p = head; j = -1;
while (p->next != NULL && p->next->next != NULL && j < i - 1)
{
p = p->next; j++;
}
if (j != i - 1)
{
printf("删除位置参数错!"); return 0;
}
s = p->next;
PolyNode tmp;
tmp.coff = s->coff;
tmp.deg = s->deg;
*s = tmp;
p->next = p->next->next;
free(s);
return 1;
}
int PolyListGet(PolyNode *head, int i, PolyNode *x) //取得第i项
{
PolyNode *p;
int j;
p = head; j = -1;
while (p->next != NULL && j < i)
{
p = p->next; j++;
}
if (j != i)
{
printf("取元素位置参数错!"); return 0;
}
PolyNode tmp;
tmp.coff = p->coff;
tmp.deg = p->deg;
*x = tmp;
return 1;
}
void PolyDestroy(PolyNode *head)
{
PolyNode *p, *p1;
p = head;
while (p != NULL)
{
p1 = p; p = p->next;
free(p1);
}
head = NULL;
}
void PolyLinListSort(PolyNode *head) //按照次数排序
{
PolyNode *curr, *pre, *p, *q;
p = head->next; head->next = NULL;
while (p != NULL)
{
curr = head->next; pre = head;
while (curr != NULL && curr->deg <= p->deg)
{
pre = curr;
curr = curr->next;
}
q = p; p = p->next;
q->next = pre->next; pre->next = q;
}
}
PolyNode * PolyADD(PolyNode *head, PolyNode *head2)//二个多项式相加
{
PolyNode *nhead = ListInitiate_poly();
PolyNode *p=head->next, *q=head2->next;
int i = 0, j=0, k=0;
while (p != NULL && q != NULL)
{
if (p->deg < q->deg)
{
PolyNode x;
PolyListGet(head, j++, &x);
PolyListInsert(nhead, i++, x);
p = p->next;
}
else if (p->deg > q->deg)
{
PolyNode x;
PolyListGet(head2, k++, &x);
PolyListInsert(nhead, i++, x);
q = q->next;
}
else
{
PolyNode x, y, z;
PolyListGet(head, j, &x);
PolyListGet(head2,k, &y);
z.coff = x.coff + y.coff;
if (z.coff == 0) { j++; k++; q = q->next; p = p->next; continue; }
z.deg = x.deg;
PolyListInsert(nhead, i++, z);
q = q->next; j++;
p = p->next; k++;
}
}
while (p != NULL)
{
PolyNode x;
PolyListGet(head, j++, &x);
PolyListInsert(nhead, i++, x);
p = p->next;
}
while (q != NULL)
{
PolyNode x;
PolyListGet(head2, k++, &x);
PolyListInsert(nhead, i++, x);
q = q->next;
}
return nhead;
}
PolyNode * PolySUB(PolyNode *head, PolyNode *head2)//二个多项式相减
{
PolyNode *nhead = ListInitiate_poly();
PolyNode *p = head->next, *q = head2->next;
int i = 0, j = 0, k = 0;
while (p != NULL && q != NULL)
{
if (p->deg < q->deg)
{
PolyNode x;
PolyListGet(head, j++, &x);
PolyListInsert(nhead, i++, x);
p = p->next;
}
else if (p->deg > q->deg)
{
PolyNode x;
PolyListGet(head2, k++, &x);
x.coff *= -1;
PolyListInsert(nhead, i++, x);
q = q->next;
}
else //次数相同的
{
PolyNode x, y, z;
PolyListGet(head, j, &x);
PolyListGet(head2, k, &y);
z.coff = x.coff - y.coff;
if (z.coff == 0) { j++; k++; q = q->next; p = p->next; continue; }
//系数=0, 就不要添加
z.deg = x.deg;
PolyListInsert(nhead, i++, z);
q = q->next; j++;
p = p->next; k++;
}
}
while (p != NULL)
{
PolyNode x;
PolyListGet(head, j++, &x);
PolyListInsert(nhead, i++, x);
p = p->next;
}
while (q != NULL)
{
PolyNode x;
PolyListGet(head2, k++, &x);
PolyListInsert(nhead, i++, x);
q = q->next;
}
return nhead;
}
//下面是测试用例,用插入法比较简单明了的创建了单链表,完成多项式的表示和计算。
void test_poly()
{
PolyNode * head1 = ListInitiate_poly();
PolyNode * head2 = ListInitiate_poly();
PolyNode x1, x2, x3, x4;
x1.coff = 1; x1.deg = 4;
x2.coff = 2; x2.deg = 3;
x3.coff = 3; x3.deg = 2;
x4.coff = 4; x4.deg = 1;
PolyListInsert(head1, 0, x1);
PolyListInsert(head1, 1, x2);
PolyListInsert(head1, 2, x3);
PolyListInsert(head1, 3, x4);
x1.coff = -1; x1.deg = 4;
x2.coff = -4; x2.deg = 3;
x3.coff = 2; x3.deg = 2;
x4.coff = 3; x4.deg = 1;
PolyListInsert(head2, 0, x1);
PolyListInsert(head2, 1, x2);
PolyListInsert(head2, 2, x3);
PolyListInsert(head2, 3, x4);
PolyLinListSort2(head1);
PolyLinListSort(head2);
PolyNode* head3 = PolyADD(head1, head2);
PolyNode* head4 = PolySUB(head1, head2);
}