user-0 提问: 将该文件代码改成 任意两个高次多项式的相加与相减

回复:
user: user-1
content: C++实现任意次元的多项式加法，以及输入输出重载，以及内存管理。Polynominal.h // // Created by Alone on 2021/9/23. //  #ifndef UNTITLED46_POLYNOMINAL_H #define UNTITLED46_POLYNOMINAL_H #include <iostream> //@Node结构体--作为表达式存储的对象 struct Node {     double factor;     int exp;     Node *next;      Node(double x, int y, Node *p = nullptr) : factor(x), exp(y), next(p) {}     //静态方法方便调用     static void swap(Node *a, Node *b) {//仅仅交换数据，不交换指针         std::swap(a->factor, b->factor);         std::swap(a->exp, b->exp);     } }; //@Polynominal类声明 class Polynominal {     Node *head;      friend std::istream &operator>>(std::istream &in, Polynominal &a);      friend std::ostream &operator<<(std::ostream &out, Polynominal &a);      friend std::ostream &handle_one(std::ostream &out, Node &node);  public:     Polynominal &operator+(Polynominal &b);      void Insert(double x, int y);      Polynominal() { //缺省构造         head = new Node(0, -1);//构建一个结点作为头结点，方便头插     }      Polynominal(double x, int y);//带参构造      ~Polynominal(); };  #endif //UNTITLED46_POLYNOMINAL_H  Polynominal.cpp // // Created by Alone on 2021/9/23. //  #include "Polynominal.h" //@Polynominal类定义 //@关键的Insert函数实现 void Polynominal::Insert(double x, int y) {     Node *t = new Node(x, y);     t->next = head->next; //头插法插入     head->next = t;     Node *pre = head;     while (t != nullptr && t->next != nullptr) {//进行偷天换日，把指数大的放在前面         if (t->exp < t->next->exp) {//指数大的放在前面             Node::swap(t, t->next);         } else if (t->exp == t->next->exp) {//指数相等时直接加和得到一个表达式             Node *tmp = t->next;             tmp->factor += t->factor; //相等时，只加上系数即可             delete t;             pre->next = tmp;             t = pre;             break;//这种直接找到相等的就处理完了         } else {             break;//如果比它大位置就已经是正确的了         }         pre = t;         t = t->next;     } } Polynominal::Polynominal(double x, int y) {     head = new Node(0, -1);//构建一个结点作为头结点，方便头插     Insert(x, y); } //@重载'+'运算符：利用Insert成员函数实现两个Polynominal类的相加 Polynominal &Polynominal::operator+(Polynominal &b) {     Node *t2 = b.head->next;     while (t2 != nullptr) {         Insert(t2->factor, t2->exp);         t2 = t2->next;     }     return *this; } //@输入操作重载 std::istream &operator>>(std::istream &in, Polynominal &a) {     double x;     int y;     while (1) {         std::cout << "input a term(coef[double],exp[int]):->Until the end of the input 0 to coef" << std::endl;         in >> x >> y;         if (x == 0)             break;         a.Insert(x, y);     }     std::cout << "The polynomial is established successfully!" << std::endl;     return in; } //@输出操作重载 // --直接重载前，先写一个单个式子处理函数，方便后面处理中间的符号 std::ostream &handle_one(std::ostream &out, Node &node) {     out << node.factor;     switch (node.exp) { //根据指数的不同情况进行打印         case 0:             break;         case 1:             out << "X";             break;         default:             out << "X^" << node.exp;             break;     }     return out; }  std::ostream &operator<<(std::ostream &out, Polynominal &a) {     out << "the polynominal is: ";     Node *t = a.head->next;     if (t == nullptr)         return out << "null";     handle_one(out, *t);     t = t->next;     while (t != nullptr) {         out << "+";         handle_one(out, *t);         t = t->next;     }     out << '\n';     return out; } //@对链表内存的析构 Polynominal::~Polynominal() {     Node *t = head;     while (t != nullptr) {         Node *cur = t;         t = t->next;         delete cur;     } }

user: user-1
content: 看看Insert函数的实现就行，减法和加法是一样的，乘法和除法需要作出较大的改变

user: user-2
content: 仅供参考： //链表实现一元多项式的加法减法乘法 #include <stdio.h> #include <stdlib.h> typedef struct node {     float coef;   //系数     int expn;     //指数     struct node *next; } PolyNode;      //多项式节点 polynomial node typedef PolyNode* Polynomial; Polynomial createPolynomial() {  //创建多项式     PolyNode *p, *q, *head = (PolyNode *)malloc(sizeof(PolyNode));   //头节点     head->next = NULL;     float coef;     int expn;     printf("输入该多项式每一项的系数和指数，每项一行，输入0 0结束！\n");     while (scanf("%f %d", &coef, &expn) && coef) {   // 默认，按指数递减排列         if (head->next) {             p = head;             while (p->next && expn < p->next->expn)                 p = p->next;             if (p->next) {                 if (expn == p->next->expn) { //有相同指数的直接把系数加到原多项式                     p->next->coef += coef;                     if (p->next->coef > -0.000001 && p->next->coef < 0.000001) { //若是相加后系数为0，则舍弃该节点                         q = p->next;                         p->next = q->next;                         free(q);                     }                 } else {                     q = (PolyNode*)malloc(sizeof(PolyNode));                     q->coef = coef;                     q->expn = expn;                     q->next = p->next;                     p->next = q;                 }             } else {                 p->next = (PolyNode*)malloc(sizeof(PolyNode));                 p = p->next;                 p->coef = coef;                 p->expn = expn;                 p->next = NULL;             }         } else {             head->next = (PolyNode*)malloc(sizeof(PolyNode));             head->next->coef = coef;             head->next->expn = expn;             head->next->next = NULL;         }     }     return head; } Polynomial multiply(Polynomial poly, float coef, int expn) {  //多项式与指定单项式相乘，该单项式为 coefx^expn     PolyNode *p, *q, *Poly = (PolyNode*)malloc(sizeof(PolyNode));     p = Poly;     q = poly->next;     while (q) {         p->next = (PolyNode*)malloc(sizeof(PolyNode));         p = p->next;         p->coef = (q->coef*coef);         p->expn = (q->expn + expn);         q = q->next;     }     p->next = NULL;     return Poly; } void add(Polynomial poly1, Polynomial poly2) {  //把 poly2 加到 poly1 上     PolyNode *p, *q, *r;     r = poly1;     p = poly1->next;  //指向第一个节点     q = poly2->next;     poly2->next = NULL;     while (p && q) {         if (p->expn > q->expn) {             r->next = p;             p = p->next;             r = r->next;         } else if (p->expn < q->expn) {             r->next = q;             q = q->next;             r = r->next;         } else {             PolyNode *t;             p->coef += q->coef;             if (!(p->coef > -0.000001 && p->coef < 0.000001)) //系数不为0             {                 r->next = p;                 r = r->next;                 p = p->next;             } else {                 t = p;                 p = p->next;                 free(t);             }             t = q;             q = q->next;             free(t);         }     }     if (p)         r->next = p;     if (q)         r->next = q; } Polynomial polySubtract(Polynomial poly1, Polynomial poly2) {  //多项式减法 poly1-poly2形成一个新的多项式     //把poly2的系数取相反数，形成一个新的多项式     Polynomial poly = (PolyNode*)malloc(sizeof(PolyNode)); //构造头节点     PolyNode *p, *q;     p = poly;     q = poly2->next;     while (q) {         p->next = (PolyNode*)malloc(sizeof(PolyNode));         p = p->next;         p->coef = -(q->coef);  //系数取反         p->expn = q->expn;         q = q->next;     }     p->next = NULL;     add(poly, poly1);  //利用加法     return poly; } Polynomial polyAdd(Polynomial poly1, Polynomial poly2) { //多项式相加 poly1+poly2形成一个新的多项式     Polynomial poly = (PolyNode*)malloc(sizeof(PolyNode));  //和多项式的头节点     poly->next = NULL;     PolyNode *p, *q, *r;     r = poly;     p = poly1->next;     q = poly2->next;     while (p&&q) {         if (p->expn > q->expn) {             r->next = (PolyNode*)malloc(sizeof(PolyNode));             r = r->next;             r->coef = p->coef;             r->expn = p->expn;             p = p->next;         } else if (p->expn < q->expn) {             r->next = (PolyNode*)malloc(sizeof(PolyNode));             r = r->next;             r->coef = q->coef;             r->expn = q->expn;             q = q->next;         } else {             float m = p->coef + q->coef;             if (!(m > -0.000001 && m < 0.000001)) {                 r->next = (PolyNode*)malloc(sizeof(PolyNode));                 r = r->next;                 r->coef = m;                 r->expn = p->expn;             }             q = q->next;             p = p->next;         }     }     while (p) {         r->next = (PolyNode*)malloc(sizeof(PolyNode));         r = r->next;         r->coef = p->coef;         r->expn = p->expn;         p = p->next;     }     while (q) {         r->next = (PolyNode*)malloc(sizeof(PolyNode));         r = r->next;         r->coef = q->coef;         r->expn = q->expn;         q = q->next;     }     r->next = NULL;     return poly; } Polynomial polyMultiply(Polynomial poly1, Polynomial poly2) {  //多项式相乘     Polynomial poly = (PolyNode*)malloc(sizeof(PolyNode));  //创建多项式和的头节点     poly->next = NULL;     PolyNode *p;     p = poly2->next;     while (p) {         add(poly, multiply(poly1, p->coef, p->expn));         p = p->next;     }     return poly; } void printPoly(Polynomial poly) {  //打印多项式     if (poly && poly->next) {         PolyNode *p = poly->next;  //p指向第一个节点         while (p->next) {             printf("%gx^%d", p->coef, p->expn);             p = p->next;             if (p && (p->coef > 0))                 printf("+");         }         if (p->expn == 0)             printf("%g", p->coef);   //打印常数项         else             printf("%gx^%d", p->coef, p->expn);         printf("\n");     } else {         printf("0\n");     } } void freePoly(Polynomial poly) {  //释放内存     if (poly && poly->next) {         PolyNode *p, *q;         p = poly;         while (p) {             q = p->next;             free(p);             p = q;         }     }     poly = NULL; } int main() {     printf("用链表实现多项式的加减法\n");     Polynomial poly1, poly2, poly3;     printf("创建多项式一\n");     poly1 = createPolynomial();     printf("多项式一：\n");     printPoly(poly1);     printf("创建多项式二\n");     poly2 = createPolynomial();     printf("多项式二：\n");     printPoly(poly2);     printf("两多项式相加，和为：\n");     poly3 = polyAdd(poly1, poly2);     printPoly(poly3);     freePoly(poly3);     printf("两个多项式相乘，积为：\n");     poly3 = polyMultiply(poly1, poly2);     printPoly(poly3);     freePoly(poly3);     printf("两多项式相减，差为：\n");     poly3 = polySubtract(poly1, poly2);     printPoly(poly3);     freePoly(poly1);     freePoly(poly2);     freePoly(poly3);     system("pause");     return 0; }