通过单链表实现两个多项式的加运算、减运算、乘运算和求导运算。
输入样例说明:
4//表示总共有四组输入数据
3 3//每组第一行分别表示多项式A和B各有几项
1 5//表示A(x)第一项为x^5
3 3//表示A(x)第二项为3x^3,以此类推
1 1//
1 4//表示B(x)第一项为x^4,以此类推
2 2//
1 0//
'//表示两个多项式进行求导运算(加、减、乘运算只有一个结果,求导有两个结果)
···//余下三组输入数据
输入样例:
4
3 3
1 5
3 3
1 1
1 4
2 2
1 0
+
3 3
1 5
3 3
1 1
1 4
2 2
1 0
-
3 3
1 5
3 3
1 1
1 4
2 2
1 0
*
3 3
1 5
3 3
1 1
1 4
2 2
1 0
'
输出样例:
1x^5+1x^4+3x^3+2x^2+1x^1+1
1x^5-1x^4+3x^3-2x^2+1x^1-1
1x^9+5x^7+8x^5+5x^3+1x^1
5x^4+9x^2+1
4x^3+4x^1
#include <stdio.h>
#include <stdlib.h>
typedef struct LNode {
int coefficient;//系数[ˌkoʊɪˈfɪʃnt]
int exponent;//指数
struct LNode *next;
} LNode, *LinkList;
LinkList ListInit();
void ListTailInsert(LinkList head, int coe, int exp);
void ListShow(LinkList head);
void ListDestroy(LinkList *head);
void ListSortByExponent(LinkList head);
void Add(LinkList A, LinkList B);
void Sub(LinkList A, LinkList B);
void Mul(LinkList A, LinkList B);
void Der(LinkList A, LinkList B);
int main() {
int m;//输入数据的组数
scanf("%d%*c", &m);
while (m--) {
LinkList A = ListInit();//多项式A
LinkList B = ListInit();//多项式B
int termA, termB;//A、B的项数
char op;//运算操作
int coe, exp;//多项式每一项的系数和指数
scanf("%d %d%*c", &termA, &termB);
while (termA--) {
scanf("%d%*c", &coe);
scanf("%d%*c", &exp);
ListTailInsert(A, coe, exp);
}
while (termB--) {
scanf("%d%*c", &coe);
scanf("%d%*c", &exp);
ListTailInsert(B, coe, exp);
}
scanf("%c%*c", &op);
switch (op) {
case '+':
Add(A, B);
break;
case '-':
Sub(A, B);
break;
case '*':
Mul(A, B);
break;
case '\'':
Der(A, B);
break;
default:
printf("error");
break;
}
ListDestroy(&A);
ListDestroy(&B);
}
return 0;
}
LinkList ListInit() {
LinkList head = (LinkList) malloc(sizeof(LNode));
head->coefficient = head->exponent = 0;
head->next = NULL;
return head;
}
void ListTailInsert(LinkList head, int coe, int exp) {
int isFound = 0;//待插项的指数是否已经在多项式中出现过
for (LNode *cursor = head->next; cursor; cursor = cursor->next) {
if (cursor->exponent == exp) {
cursor->coefficient += coe;
isFound = 1;
break;
}
}
if (!isFound) {//未能找到对应项,只能在链表尾部新建
LNode *newNode = (LNode *) malloc(sizeof(LNode));
LNode *tail = head;
newNode->coefficient = coe;
newNode->exponent = exp;
newNode->next = NULL;
/*链接在尾部*/
while (tail->next) tail = tail->next;
tail->next = newNode;
}
}
void ListShow(LinkList head) {
int isFirst = 1;
for (LNode *cursor = head->next; cursor; cursor = cursor->next) {
if (cursor->coefficient) {
if (!isFirst && cursor->coefficient > 0) printf("+");
printf("%d", cursor->coefficient);
if (cursor->exponent) printf("x^%d", cursor->exponent);
isFirst = 0;
}
}
if (isFirst) printf("0");//表明没有任何一项被输出
printf("\n");
}
void ListDestroy(LinkList *head) {
LNode *tmp;
while (*head) {
tmp = *head;
*head = (*head)->next;
free(tmp);
}
*head = NULL;
}
void ListSortByExponent(LinkList head) {
LNode *right, *left, *tmp;
if (!head->next) return;
right = head->next->next;
head->next->next = NULL;
while (right) {
left = head;
while (left->next && left->next->exponent > right->exponent) left = left->next;
tmp = right->next;
right->next = left->next;
left->next = right;
right = tmp;
}
}
void Add(LinkList A, LinkList B) {
LNode *cursor;
for (cursor = B->next; cursor; cursor = cursor->next) {
ListTailInsert(A, cursor->coefficient, cursor->exponent);
}
ListSortByExponent(A);//将每项按照指数从高到低进行排序
ListShow(A);
}
void Sub(LinkList A, LinkList B) {
LNode *cursor;
for (cursor = B; cursor; cursor = cursor->next) cursor->coefficient *= -1;//A - B = A + (-B)
for (cursor = B->next; cursor; cursor = cursor->next) {
ListTailInsert(A, cursor->coefficient, cursor->exponent);
}
ListSortByExponent(A);//将每项按照指数从高到低进行排序
ListShow(A);
}
void Mul(LinkList A, LinkList B) {
LinkList C = ListInit();
LNode *curA, *curB;
for (curA = A->next; curA; curA = curA->next) {
for (curB = B->next; curB; curB = curB->next) {
ListTailInsert(C, curA->coefficient * curB->coefficient, curA->exponent + curB->exponent);
}
}
ListSortByExponent(C);
ListShow(C);
ListDestroy(&C);
}
void Der(LinkList A, LinkList B) {
LNode *curA, *curB;
for (curA = A; curA; curA = curA->next) {
curA->coefficient = curA->coefficient * curA->exponent;
curA->exponent -= 1;
}
for (curB = B; curB; curB = curB->next) {
curB->coefficient = curB->coefficient * curB->exponent;
curB->exponent -= 1;
}
ListSortByExponent(A);
ListSortByExponent(B);
ListShow(A);
ListShow(B);
}