一元n次多项式定义如下:
其中Ai为实数,i为不小于0的整数。在完成“一元n次多项式输入输出”题目的基础上实现一元n次多项式的乘法。要求使用链表实现上述运算。
输入格式:
有两个一元n次多项式,格式分别为:
f(X)=3X2+ X+1
g(X)=−2X2−X-1
其中系数为实数,指数为不小于0的整数,则输入分为2行,第1行为第1个一元n次多项式,第1个一元n次多项式按照第1项系数,指数 第2项系数,指数 .... 的格式输入,系数和指数以“,”分割,各项的系数和指数之间以空格分割,输入的一元n次多项式不要求按指数有序排列,最后以 0,0(即系数=0,指数=0)表示结束。第2行为第2个一元n次多项式,输入格式与第1个一元n次多项式相同。对上面的两个一元n次多项式:
输入样例:
3,2 1,1 1,0 0,0
-2,2 -1,1 -1,0 0,0
输出格式:
输出分为以下3行:第1行输出第1个一元n次多项式,第2行输出第2个一元n次多项式,第3行输出两个一元n次多项式的乘积。输出要求一元n次多项式的高次项在前,低次项在后,即按指数由大到小排列,实数保留小数点后面1位数,一元n次多项式为f(X)=0时,输出为f(X)=0.0。对上面两个一元n次多项式的输出为:
输出样例:
f(X)=3.0X^2+X+1.0
g(X)=-2.0X^2-X-1.0
f(X)*g(X)=-6.0X^4-5.0X^3-6.0X^2-2.0X-1.0
#include <stdio.h>
#include <stdlib.h>typedef struct PolynomialNode //创建一个结构体
{double coefficient; //多项式系数int exponent; //多项式指数struct PolynomialNode *next;//指针
} PolynomialNode;PolynomialNode* createNode(double coefficient, int exponent)
{ //为链表分配内存空间 PolynomialNode *newNode = (PolynomialNode*)malloc(sizeof(PolynomialNode));newNode->coefficient = coefficient;newNode->exponent = exponent;newNode->next = NULL;return newNode;
}void insertNode(PolynomialNode** head, double coefficient, int exponent)
{PolynomialNode* newNode = createNode(coefficient, exponent);if (*head == NULL || exponent > (*head)->exponent) //链表为空||新的指数>头节点指数{newNode->next = *head; //新节点插入链表头部*head = newNode; //新节点的next指针指向头节点,新节点更新为头节点} else { //定义一个指向PolynomialNode类型的指针current,并将其初始化为指向链表的第一个节点PolynomialNode* current = *head;while (current->next!= NULL && current->next->exponent > exponent) {current = current->next; //遍历}if (current->exponent == exponent) //当前项与某项指数相同{current->coefficient += coefficient;//合并同类项free(newNode);} else {newNode->next = current->next; //插入到合适的位置current->next = newNode;}}
}void printPolynomial(PolynomialNode* head, const char* name)
{if (head == NULL) {printf("%s=0.0\n", name);return;}PolynomialNode* current = head;printf("%s=", name);int isFirstTerm = 1;while (current!= NULL) {if (current->coefficient == 0) {current = current->next;continue;}if (!isFirstTerm) {if (current->coefficient > 0) {printf("+");} else {printf("");}}if (current->coefficient == 1 && current->exponent == 1) {printf("X");} else if (current->coefficient == -1 && current->exponent == 1) {printf("-X");} else if (current->coefficient == 1 && current->exponent > 1) {printf("X^%d", current->exponent);} else if (current->coefficient == -1 && current->exponent > 1) {printf("-X^%d", current->exponent);} else {if (current->exponent > 1) {printf("%.1fX^%d", current->coefficient, current->exponent);} else if (current->exponent == 1) {printf("%.1fX", current->coefficient);} else {printf("%.1f", current->coefficient);}}isFirstTerm = 0;current = current->next;}printf("\n");
}void freePolynomial(PolynomialNode* head)
{PolynomialNode* current = head; while (current!= NULL) { //为了在释放当前节点的内存后,能够继续遍历链表PolynomialNode* next = current->next;free(current);current = next; //将current指针更新为指向刚才保存的下一个节点}
}PolynomialNode* multiplyPolynomials(PolynomialNode* p1, PolynomialNode* p2)
{PolynomialNode* result = NULL;PolynomialNode* current1 = p1;while (current1!= NULL) //***核心运算逻辑***(双层遍历){ //只要current1不为NULL,就表示还有项需要处理PolynomialNode* current2 = p2;while (current2!= NULL) { //只要current2不为NULL,就表示还有项需要处理double newCoefficient = current1->coefficient * current2->coefficient;int newExponent = current1->exponent + current2->exponent;insertNode(&result, newCoefficient, newExponent);current2 = current2->next;}current1 = current1->next;}PolynomialNode* temp = result;while (temp!= NULL && temp->next!= NULL) {if (temp->exponent == temp->next->exponent) //结果的合并同类项{temp->coefficient += temp->next->coefficient;PolynomialNode* toFree = temp->next;temp->next = temp->next->next;free(toFree);} else {temp = temp->next; //移动指针到下一项,继续检查下一对相邻项}}return result;
}int main()
{PolynomialNode* poly1 = NULL;PolynomialNode* poly2 = NULL;double coefficient;int exponent;// 输入第一个多项式while (scanf("%lf,%d", &coefficient, &exponent) && (coefficient!= 0 || exponent!= 0)) {if (exponent < 0) {printf("Exponents must be non-negative. Please enter valid input.\n");return 1;}insertNode(&poly1, coefficient, exponent);}// 输入第二个多项式while (scanf("%lf,%d", &coefficient, &exponent) && (coefficient!= 0 || exponent!= 0)) {if (exponent < 0) {printf("Exponents must be non-negative. Please enter valid input.\n");return 1;}insertNode(&poly2, coefficient, exponent);}// 输出第一个多项式printPolynomial(poly1, "f(X)");// 输出第二个多项式printPolynomial(poly2, "g(X)");// 计算并输出乘积PolynomialNode* product = multiplyPolynomials(poly1, poly2);printPolynomial(product, "f(X)*g(X)");// 释放内存freePolynomial(poly1);freePolynomial(poly2);freePolynomial(product);return 0;
}