编程实现一元多项式的加法运算。第一个一元多项式A;第二个多项式B。以(0,0作为输入结束)

时间: 2023-05-16 19:05:59 浏览: 35
好的,以下是一元多项式的加法运算的代码实现: ```python class Polynomial: def __init__(self): self.poly = {} def add_term(self, coeff, exp): if coeff != 0: self.poly[exp] = self.poly.get(exp, 0) + coeff def add_poly(self, other): result = Polynomial() for exp, coeff in self.poly.items(): result.add_term(coeff, exp) for exp, coeff in other.poly.items(): result.add_term(coeff, exp) return result def __str__(self): terms = [] for exp, coeff in sorted(self.poly.items(), reverse=True): if coeff == 0: continue if exp == 0: terms.append(str(coeff)) elif exp == 1: terms.append(f"{coeff}x") else: terms.append(f"{coeff}x^{exp}") if not terms: return "0" return " + ".join(terms) if __name__ == "__main__": A = Polynomial() B = Polynomial() while True: coeff, exp = map(int, input().split()) if coeff == 0 and exp == 0: break A.add_term(coeff, exp) while True: coeff, exp = map(int, input().split()) if coeff == 0 and exp == 0: break B.add_term(coeff, exp) C = A.add_poly(B) print(C) ``` 你可以输入多项式的系数和指数,以(0, 0)作为输入结束。比如,输入: ``` 3 2 -2 1 5 0 2 3 4 1 -1 0 0 0 ``` 输出: ``` 2x^3 + x^2 + 2x + 4 ``` 这个输出表示两个多项式相加的结果。

相关推荐

#include <stdio.h> #define MAX_TERMS 100 typedef struct { float coef; // 系数 int exp; // 指数 } Term; void input_poly(Term poly[]); void add_poly(const Term poly1[], const Term poly2[], Term result[]); void print_poly(const Term poly[]); int main() { Term poly1[MAX_TERMS], poly2[MAX_TERMS], result[MAX_TERMS]; printf("请输入第一个多项式:\n"); input_poly(poly1); printf("请输入第二个多项式:\n"); input_poly(poly2); add_poly(poly1, poly2, result); printf("两个多项式相加的结果为:\n"); print_poly(result); return 0; } void input_poly(Term poly[]) { int i = 0; while (1) { printf("请输入第%d项的系数和指数:", i + 1); scanf("%f%d", &poly[i].coef, &poly[i].exp); if (poly[i].coef == 0 && poly[i].exp == 0) { break; } i++; } } void add_poly(const Term poly1[], const Term poly2[], Term result[]) { int i = 0, j = 0, k = 0; while (poly1[i].coef != 0 || poly1[i].exp != 0 || poly2[j].coef != 0 || poly2[j].exp != 0) { if (poly1[i].exp > poly2[j].exp) { result[k] = poly1[i]; i++; } else if (poly1[i].exp < poly2[j].exp) { result[k] = poly2[j]; j++; } else { result[k].coef = poly1[i].coef + poly2[j].coef; result[k].exp = poly1[i].exp; i++; j++; } k++; } result[k].coef = 0; result[k].exp = 0; } void print_poly(const Term poly[]) { int i = 0; while (poly[i].coef != 0 || poly[i].exp != 0) { if (i > 0 && poly[i].coef > 0) { printf("+"); } if (poly[i].coef != 1 && poly[i].coef != -1) { printf("%.2f", poly[i].coef); } else if (poly[i].coef == -1) { printf("-"); } if (poly[i].exp == 0) { printf("%.2f", poly[i].coef); } else if (poly[i].exp == 1) { printf("x"); } else { printf("x^%d", poly[i].exp); } i++; } printf("\n"); }
可以使用链表来存储一元多项式。 具体实现步骤如下: 1. 定义一个结构体来表示一项多项式,包括系数和指数两个成员变量。 2. 定义一个链表节点结构体,包括一项多项式和一个指向下一个节点的指针。 3. 定义一个函数来创建一项多项式的链表,输入系数和指数,返回一个链表头指针。 4. 定义一个函数来打印一元多项式。 5. 定义一个函数来实现一元多项式的加法运算,输入两个链表头指针,返回一个链表头指针。 6. 在主函数中,循环输入一元多项式的系数和指数,直到输入(0,0)为止。然后调用创建链表函数,分别创建两个一元多项式的链表。接着调用加法运算函数,计算并打印结果。 以下是示例代码: c #include <stdio.h> #include <stdlib.h> // 一项多项式结构体 struct PolyTerm { int coef; // 系数 int exp; // 指数 }; // 链表节点结构体 struct Node { struct PolyTerm term; // 一项多项式 struct Node* next; // 下一个节点的指针 }; // 创建一项多项式的链表,输入系数和指数,返回一个链表头指针 struct Node* createPoly(int coef, int exp) { struct Node* head = (struct Node*)malloc(sizeof(struct Node)); head->term.coef = coef; head->term.exp = exp; head->next = NULL; return head; } // 打印一元多项式 void printPoly(struct Node* head) { struct Node* p = head; while (p != NULL) { printf("%d*x^%d", p->term.coef, p->term.exp); p = p->next; if (p != NULL && p->term.coef > 0) { printf("+"); } } printf("\n"); } // 实现一元多项式的加法运算,输入两个链表头指针,返回一个链表头指针 struct Node* addPoly(struct Node* head1, struct Node* head2) { struct Node* p1 = head1; struct Node* p2 = head2; struct Node* head3 = (struct Node*)malloc(sizeof(struct Node)); struct Node* p3 = head3; while (p1 != NULL && p2 != NULL) { if (p1->term.exp > p2->term.exp) { p3->term = p1->term; p1 = p1->next; } else if (p1->term.exp < p2->term.exp) { p3->term = p2->term; p2 = p2->next; } else { p3->term.coef = p1->term.coef + p2->term.coef; if (p3->term.coef != 0) { p3->term.exp = p1->term.exp; p1 = p1->next; p2 = p2->next; } else { p1 = p1->next; p2 = p2->next; continue; } } p3->next = (struct Node*)malloc(sizeof(struct Node)); p3 = p3->next; p3->next = NULL; } while (p1 != NULL) { p3->term = p1->term; p1 = p1->next; p3->next = (struct Node*)malloc(sizeof(struct Node)); p3 = p3->next; p3->next = NULL; } while (p2 != NULL) { p3->term = p2->term; p2 = p2->next; p3->next = (struct Node*)malloc(sizeof(struct Node)); p3 = p3->next; p3->next = NULL; } p3 = head3; head3 = head3->next; free(p3); return head3; } int main() { struct Node* head1 = NULL; struct Node* head2 = NULL; struct Node* head3 = NULL; int coef, exp; printf("Input polynomial A:(coef, exp)\n"); scanf("%d%d", &coef, &exp); head1 = createPoly(coef, exp); while (coef != 0 || exp != 0) { scanf("%d%d", &coef, &exp); if (coef == 0 && exp == 0) { break; } struct Node* p = createPoly(coef, exp); p->next = head1->next; head1->next = p; } printf("Input polynomial B:(coef, exp)\n"); scanf("%d%d", &coef, &exp); head2 = createPoly(coef, exp); while (coef != 0 || exp != 0) { scanf("%d%d", &coef, &exp); if (coef == 0 && exp == 0) { break; } struct Node* p = createPoly(coef, exp); p->next = head2->next; head2->next = p; } printf("Polynomial A: "); printPoly(head1); printf("Polynomial B: "); printPoly(head2); head3 = addPoly(head1, head2); printf("Polynomial A+B: "); printPoly(head3); return 0; }
#include <stdio.h> #include <stdlib.h> typedef struct PolyNode *Polynomial; struct PolyNode { int coef; // 系数 int expon; // 指数 Polynomial next; // 指向下一个节点的指针 }; Polynomial ReadPoly(); // 读入多项式 Polynomial Add(Polynomial P1, Polynomial P2); // 多项式相加 void PrintPoly(Polynomial P); // 输出多项式 int main() { Polynomial P1, P2, PS; // 读入两个多项式 P1 = ReadPoly(); P2 = ReadPoly(); // 计算多项式相加 PS = Add(P1, P2); // 输出相加的结果 PrintPoly(PS); return 0; } Polynomial ReadPoly() { Polynomial P, Rear, t; int c, e; // 初始化多项式头节点 P = (Polynomial)malloc(sizeof(struct PolyNode)); P->next = NULL; Rear = P; // 读入每一项 scanf("%d %d", &c, &e); while (c != 0 || e != 0) { t = (Polynomial)malloc(sizeof(struct PolyNode)); t->coef = c; t->expon = e; t->next = NULL; Rear->next = t; Rear = t; scanf("%d %d", &c, &e); } return P; } Polynomial Add(Polynomial P1, Polynomial P2) { Polynomial t1, t2, Rear, t; int sum; // 初始化结果多项式头节点 t1 = P1->next; t2 = P2->next; t = (Polynomial)malloc(sizeof(struct PolyNode)); t->next = NULL; Rear = t; // 对两个多项式进行相加 while (t1 && t2) { if (t1->expon > t2->expon) { Rear->next = t1; Rear = t1; t1 = t1->next; } else if (t1->expon < t2->expon) { Rear->next = t2; Rear = t2; t2 = t2->next; } else { sum = t1->coef + t2->coef; if (sum != 0) { t->coef = sum; t->expon = t1->expon; Rear->next = t; Rear = t; } t1 = t1->next; t2 = t2->next; } } // 将未处理完的节点接到结果多项式的末尾 for (; t1; t1 = t1->next) { Rear->next = t1; Rear = t1; } for (; t2; t2 = t2->next) { Rear->next = t2; Rear = t2; } // 删除结果多项式头节点 Rear = t; t = t->next; free(Rear); return t; } void PrintPoly(Polynomial P) { if (!P) { printf("0 0\n"); return; } while (P) { printf("%d %d", P->coef, P->expon); P = P->next; if (P) { printf(" "); } else { printf("\n"); } } }
以下是一元多项式加法运算的C语言代码: #include <stdio.h> #include <stdlib.h> #define MAX_TERM 100 // 多项式的最大项数 typedef struct { float coef; // 系数 int expn; // 指数 } term; typedef struct { term data[MAX_TERM]; int len; } polynomial; void create_polynomial(polynomial *p) { // 创建多项式 printf("请输入多项式的项数:"); scanf("%d", &p->len); for (int i = 0; i < p->len; ++i) { printf("请输入第%d项的系数和指数:", i + 1); scanf("%f%d", &p->data[i].coef, &p->data[i].expn); } } void print_polynomial(polynomial p) { // 输出多项式 for (int i = 0; i < p.len; ++i) { if (p.data[i].coef > 0 && i > 0) { printf("+"); } printf("%.2fx^%d", p.data[i].coef, p.data[i].expn); } printf("\n"); } polynomial add_polynomial(polynomial p1, polynomial p2) { // 多项式加法 polynomial result = {0}; int i = 0, j = 0, k = 0; while (i < p1.len && j < p2.len) { if (p1.data[i].expn > p2.data[j].expn) { result.data[k++] = p1.data[i++]; } else if (p1.data[i].expn < p2.data[j].expn) { result.data[k++] = p2.data[j++]; } else { float sum = p1.data[i].coef + p2.data[j].coef; if (sum != 0) { result.data[k].coef = sum; result.data[k++].expn = p1.data[i].expn; } ++i; ++j; } } while (i < p1.len) { result.data[k++] = p1.data[i++]; } while (j < p2.len) { result.data[k++] = p2.data[j++]; } result.len = k; return result; } int main() { polynomial p1 = {0}, p2 = {0}, result = {0}; printf("请输入第一个多项式:\n"); create_polynomial(&p1); printf("请输入第二个多项式:\n"); create_polynomial(&p2); printf("第一个多项式为:"); print_polynomial(p1); printf("第二个多项式为:"); print_polynomial(p2); result = add_polynomial(p1, p2); printf("两个多项式相加的结果为:"); print_polynomial(result); return 0; } 在代码中,我们首先定义了一个term结构体表示多项式的一项,其中包括系数和指数两个成员变量。然后定义了一个polynomial结构体表示多项式,其中包括一个term类型的数组和一个表示多项式长度的len变量。 接着,我们实现了三个函数:create_polynomial用于创建多项式,print_polynomial用于输出多项式,add_polynomial用于实现多项式加法。 在main函数中,我们先分别创建了两个多项式p1和p2,然后输出它们,再调用add_polynomial函数求出它们的和result,并输出结果。
#include <stdio.h> #include <stdlib.h> typedef struct node { int coef; // 系数 int exp; // 指数 struct node* next; // 指向下一个节点的指针 } Node; Node* create_node(int coef, int exp) { Node* p = (Node*)malloc(sizeof(Node)); p->coef = coef; p->exp = exp; p->next = NULL; return p; } Node* create_poly() { Node* head = create_node(0, 0); // 创建一个头节点 Node* tail = head; // 尾指针指向头节点 int coef, exp; printf("请输入一元多项式的系数和指数(以0,0结束):\n"); scanf("%d,%d", &coef, &exp); while (coef != 0 || exp != 0) { // 以(0,0)作为输入结束 Node* p = create_node(coef, exp); tail->next = p; // 尾节点指向新节点 tail = p; // 尾指针指向新节点 scanf("%d,%d", &coef, &exp); } return head; } void print_poly(Node* head) { Node* p = head->next; while (p) { // 遍历链表 if (p->coef > 0 && p != head->next) { // 系数为正数需要输出“+” printf("+"); } printf("%d", p->coef); // 输出系数 if (p->exp > 1) { // 指数大于1需要输出“x^exp” printf("x^%d", p->exp); } else if (p->exp == 1) { // 指数为1只需要输出“x” printf("x"); } p = p->next; } printf("\n"); } Node* add_poly(Node* poly1, Node* poly2) { Node* head1 = poly1->next; Node* head2 = poly2->next; Node* head = create_node(0, 0); // 创建一个头节点 Node* tail = head; // 尾指针指向头节点 while (head1 && head2) { // 遍历两个链表 if (head1->exp > head2->exp) { // 如果poly1的指数大于poly2的指数 tail->next = create_node(head1->coef, head1->exp); // 将poly1的节点添加到结果链表中 head1 = head1->next; } else if (head1->exp < head2->exp) { // 如果poly1的指数小于poly2的指数 tail->next = create_node(head2->coef, head2->exp); // 将poly2的节点添加到结果链表中 head2 = head2->next; } else { // 如果poly1的指数等于poly2的指数 int coef = head1->coef + head2->coef; if (coef != 0) { // 系数不为0才添加节点 tail->next = create_node(coef, head1->exp); // 将系数相加后的节点添加到结果链表中 } head1 = head1->next; head2 = head2->next; } tail = tail->next; // 尾指针指向新节点 } // 将剩余的节点添加到结果链表中 while (head1) { tail->next = create_node(head1->coef, head1->exp); head1 = head1->next; tail = tail->next; } while (head2) { tail->next = create_node(head2->coef, head2->exp); head2 = head2->next; tail = tail->next; } return head; } int main() { printf("请输入第一个一元多项式:\n"); Node* poly1 = create_poly(); printf("请输入第二个一元多项式:\n"); Node* poly2 = create_poly(); printf("第一个多项式:"); print_poly(poly1); printf("第二个多项式:"); print_poly(poly2); Node* sum = add_poly(poly1, poly2); printf("多项式相加的结果:"); print_poly(sum); return 0; }
#include <stdio.h> #include <stdlib.h> #define MAXSIZE 100 // 定义一元多项式结构体 typedef struct { float coef; // 系数 int expn; // 指数 } ElemType; typedef struct { ElemType *elem; // 存储空间基地址 int length; // 当前长度 int listsize; // 分配的存储容量 } SqList; // 初始化线性表 void InitList(SqList *L) { L->elem = (ElemType *)malloc(MAXSIZE * sizeof(ElemType)); if (!L->elem) { exit(0); // 存储分配失败 } L->length = 0; L->listsize = MAXSIZE; } // 增加线性表长度 void IncreaseSize(SqList *L, int len) { ElemType *newbase; newbase = (ElemType *)realloc(L->elem, (L->listsize + len) * sizeof(ElemType)); if (!newbase) { exit(0); // 存储分配失败 } L->elem = newbase; L->listsize += len; } // 插入元素 void ListInsert(SqList *L, int i, ElemType e) { if (i < 1 || i > L->length + 1) { // i值不合法 exit(0); } if (L->length >= L->listsize) { // 当前存储空间已满,增加分配 IncreaseSize(L, MAXSIZE); } ElemType *q = &(L->elem[i - 1]); for (ElemType *p = &(L->elem[L->length - 1]); p >= q; --p) { *(p + 1) = *p; } *q = e; ++L->length; } // 一元多项式相加 void AddPolyn(SqList *La, SqList *Lb) { int i = 1, j = 1, k = 0; while (i <= La->length && j <= Lb->length) { if (La->elem[i - 1].expn == Lb->elem[j - 1].expn) { // 指数相等,系数相加 float sum = La->elem[i - 1].coef + Lb->elem[j - 1].coef; if (sum != 0) { ElemType e = {sum, La->elem[i - 1].expn}; ListInsert(La, ++k, e); } ++i; ++j; } else if (La->elem[i - 1].expn < Lb->elem[j - 1].expn) { // 将La中指数较小的赋值给结果多项式 ListInsert(La, ++k, La->elem[i - 1]); ++i; } else { // 将Lb中指数较小的赋值给结果多项式 ListInsert(La, ++k, Lb->elem[j - 1]); ++j; } } // 将La或Lb中剩余的元素添加到结果多项式中 while (i <= La->length) { ListInsert(La, ++k, La->elem[i - 1]); ++i; } while (j <= Lb->length) { ListInsert(La, ++k, Lb->elem[j - 1]); ++j; } } int main() { SqList La, Lb; InitList(&La); InitList(&Lb); printf("请输入多项式1的系数和指数,以(0,0)作为输入结束:\n"); float coef; int expn; scanf("%f,%d", &coef, &expn); while (coef != 0 || expn != 0) { ElemType e = {coef, expn}; ListInsert(&La, La.length + 1, e); scanf("%f,%d", &coef, &expn); } printf("请输入多项式2的系数和指数,以(0,0)作为输入结束:\n"); scanf("%f,%d", &coef, &expn); while (coef != 0 || expn != 0) { ElemType e = {coef, expn}; ListInsert(&Lb, Lb.length + 1, e); scanf("%f,%d", &coef, &expn); } AddPolyn(&La, &Lb); printf("多项式相加的结果为:\n"); for (int i = 0; i < La.length; ++i) { printf("%.1fX^%d", La.elem[i].coef, La.elem[i].expn); if (i != La.length - 1) { printf("+"); } } printf("\n"); return 0; }
#include <stdio.h> #include <stdlib.h> typedef struct PolyNode *Polynomial; struct PolyNode{ int coef; // 系数 int expon; // 指数 Polynomial link; // 下一项 }; void Attach(int c, int e, Polynomial *pRear){ Polynomial P; // 新建节点 P = (Polynomial)malloc(sizeof(struct PolyNode)); P->coef = c; P->expon = e; P->link = NULL; // 插入节点 (*pRear)->link = P; *pRear = P; } Polynomial PolyAdd(Polynomial P1, Polynomial P2){ Polynomial P, Rear, t1, t2; int sum; // 新建头节点 P = (Polynomial)malloc(sizeof(struct PolyNode)); Rear = P; t1 = P1->link; t2 = P2->link; while(t1 && t2){ if(t1->expon == t2->expon){ // 指数相等 sum = t1->coef + t2->coef; if(sum) // 系数不为0 Attach(sum, t1->expon, &Rear); t1 = t1->link; t2 = t2->link; } else if(t1->expon > t2->expon){ // P1中指数较大 Attach(t1->coef, t1->expon, &Rear); t1 = t1->link; } else{ // P2中指数较大 Attach(t2->coef, t2->expon, &Rear); t2 = t2->link; } } // 将未处理完的项接到结果多项式中 for(; t1; t1 = t1->link) Attach(t1->coef, t1->expon, &Rear); for(; t2; t2 = t2->link) Attach(t2->coef, t2->expon, &Rear); // 删除头节点 Rear->link = NULL; P = P->link; free(P1); free(P2); return P; } void PrintPoly(Polynomial P){ if(!P){ printf("0 0\n"); return; } while(P){ printf("%d %d", P->coef, P->expon); P = P->link; if(P) printf(" "); } printf("\n"); } int main(){ Polynomial P1, P2, PP, PS; // 新建多项式P1 P1 = (Polynomial)malloc(sizeof(struct PolyNode)); P1->link = NULL; Attach(5, 0, &P1); Attach(2, 1, &P1); Attach(-3, 2, &P1); // 新建多项式P2 P2 = (Polynomial)malloc(sizeof(struct PolyNode)); P2->link = NULL; Attach(7, 1, &P2); Attach(-2, 2, &P2); Attach(4, 4, &P2); // 输出多项式P1和P2 printf("P1: "); PrintPoly(P1); printf("P2: "); PrintPoly(P2); // 多项式相加 PP = PolyAdd(P1, P2); // 输出相加结果 printf("P1 + P2: "); PrintPoly(PP); return 0; }
一元多项式可以用链表来实现,每个节点表示一个项,包含系数和指数两个成员变量。加法运算就是将两个链表按照指数从小到大的顺序依次合并,如果两个节点的指数相同,则将它们的系数相加,否则将指数小的节点插入到结果链表中。 以下是 C++ 实现代码: c++ #include <iostream> using namespace std; struct Node { int coef; // 系数 int exp; // 指数 Node* next; Node(int c = 0, int e = 0, Node* n = nullptr) : coef(c), exp(e), next(n) {} }; void addPoly(Node* a, Node* b) { Node* head = new Node(); // 结果链表的头结点 Node* tail = head; // 结果链表的尾结点 while (a != nullptr && b != nullptr) { if (a->exp < b->exp) { tail->next = new Node(a->coef, a->exp); a = a->next; } else if (a->exp > b->exp) { tail->next = new Node(b->coef, b->exp); b = b->next; } else { int coefSum = a->coef + b->coef; if (coefSum != 0) { tail->next = new Node(coefSum, a->exp); } a = a->next; b = b->next; } tail = tail->next; } // 将剩余的结点插入到结果链表中 while (a != nullptr) { tail->next = new Node(a->coef, a->exp); a = a->next; tail = tail->next; } while (b != nullptr) { tail->next = new Node(b->coef, b->exp); b = b->next; tail = tail->next; } // 输出结果链表 Node* p = head->next; while (p != nullptr) { cout << p->coef << "x^" << p->exp << " + "; p = p->next; } cout << "0" << endl; } int main() { // 构造两个多项式:a = 3x^2 + 2x + 1,b = 4x^3 + 2x^2 + 5 Node* a = new Node(3, 2, new Node(2, 1, new Node(1, 0))); Node* b = new Node(4, 3, new Node(2, 2, new Node(5, 0))); addPoly(a, b); // 输出结果:4x^3 + 5x^2 + 2x + 1 return 0; }
#include<stdio.h> #include<stdlib.h> // 定义多项式结构体 typedef struct Polynomial{ int coef; // 系数 int expn; // 指数 struct Polynomial *next; // 指向下一项的指针 }Polynomial; // 创建多项式 Polynomial* createPoly(){ Polynomial *head = (Polynomial*)malloc(sizeof(Polynomial)); // 头节点 head->next = NULL; Polynomial *p = head; // 指针p指向头节点 int n; // 项数 printf("请输入多项式项数:"); scanf("%d", &n); for(int i=0; i<n; i++){ Polynomial *node = (Polynomial*)malloc(sizeof(Polynomial)); // 新建节点 printf("请输入第%d项的系数和指数:", i+1); scanf("%d%d", &node->coef, &node->expn); node->next = NULL; p->next = node; // 将新节点插入到链表尾部 p = node; } return head; } // 显示多项式 void displayPoly(Polynomial *poly){ Polynomial *p = poly->next; // 指针p指向第一个节点 while(p){ printf("%dX^%d", p->coef, p->expn); p = p->next; if(p) printf("+"); } printf("\n"); } // 多项式相加 Polynomial* addPoly(Polynomial *poly1, Polynomial *poly2){ Polynomial *p1 = poly1->next; // 指针p1指向第一个节点 Polynomial *p2 = poly2->next; // 指针p2指向第一个节点 Polynomial *head = (Polynomial*)malloc(sizeof(Polynomial)); // 头节点 head->next = NULL; Polynomial *p = head; // 指针p指向头节点 while(p1 && p2){ if(p1->expn == p2->expn){ // 指数相等,系数相加 Polynomial *node = (Polynomial*)malloc(sizeof(Polynomial)); node->coef = p1->coef + p2->coef; node->expn = p1->expn; node->next = NULL; p->next = node; p = node; p1 = p1->next; p2 = p2->next; } else if(p1->expn > p2->expn){ // 第一个多项式指数大于第二个多项式指数 Polynomial *node = (Polynomial*)malloc(sizeof(Polynomial)); node->coef = p1->coef; node->expn = p1->expn; node->next = NULL; p->next = node; p = node; p1 = p1->next; } else{ // 第一个多项式指数小于第二个多项式指数 Polynomial *node = (Polynomial*)malloc(sizeof(Polynomial)); node->coef = p2->coef; node->expn = p2->expn; node->next = NULL; p->next = node; p = node; p2 = p2->next; } } while(p1){ // 第一个多项式还有剩余项 Polynomial *node = (Polynomial*)malloc(sizeof(Polynomial)); node->coef = p1->coef; node->expn = p1->expn; node->next = NULL; p->next = node; p = node; p1 = p1->next; } while(p2){ // 第二个多项式还有剩余项 Polynomial *node = (Polynomial*)malloc(sizeof(Polynomial)); node->coef = p2->coef; node->expn = p2->expn; node->next = NULL; p->next = node; p = node; p2 = p2->next; } return head; } int main(){ printf("请输入第一个多项式:\n"); Polynomial *poly1 = createPoly(); // 创建第一个多项式 printf("第一个多项式为:"); displayPoly(poly1); // 显示第一个多项式 printf("请输入第二个多项式:\n"); Polynomial *poly2 = createPoly(); // 创建第二个多项式 printf("第二个多项式为:"); displayPoly(poly2); // 显示第二个多项式 Polynomial *result = addPoly(poly1, poly2); // 两个多项式相加 printf("两个多项式相加后的结果为:"); displayPoly(result); // 显示相加后的结果 return 0; }

最新推荐

Android 开发视频播放器源码代码逻辑清晰.zip

Android 开发视频播放器源码代码逻辑清晰

经典织构分析软件textool-欧拉角与米勒指数相互转换.zip

经典织构分析软件textool-欧拉角与米勒指数相互转换

Java 开发项目申报系统源码ssh框架+数据库.zip

Java 开发项目申报系统源码ssh框架+数据库

Java 开发教学管理项目源码+数据库项目可运行无报错,代码清晰适合新手.rar

Java 开发教学管理项目源码+数据库项目可运行无报错,代码清晰适合新手

ChatGPT技术在人工智能创作中的应用.docx

ChatGPT技术在人工智能创作中的应用

基于at89c51单片机的-智能开关设计毕业论文设计.doc

基于at89c51单片机的-智能开关设计毕业论文设计.doc

"蒙彼利埃大学与CNRS联合开发细胞内穿透载体用于靶向catphepsin D抑制剂"

由蒙彼利埃大学提供用于靶向catphepsin D抑制剂的细胞内穿透载体的开发在和CNRS研究单位- UMR 5247(马克斯·穆塞隆生物分子研究专长:分子工程由Clément Sanchez提供于2016年5月26日在评审团面前进行了辩护让·吉隆波尔多大学ARNA实验室CNRS- INSERM教授报告员塞巴斯蒂安·帕波特教授,CNRS-普瓦捷大学普瓦捷介质和材料化学研究所报告员帕斯卡尔·拉斯特洛教授,CNRS-审查员让·马丁内斯蒙彼利埃大学Max Mousseron生物分子研究所CNRS教授审查员文森特·利索夫斯基蒙彼利埃大学Max Mousseron生物分子研究所CNRS教授论文主任让-弗朗索瓦·赫尔南德斯CNRS研究总监-蒙彼利埃大学Max Mousseron生物分子研究论文共同主任由蒙彼利埃大学提供用于靶向catphepsin D抑制剂的细胞内穿透载体的开发在和CNRS研究单位- UMR 5247(马克斯·穆塞隆生物分子研究专长:分子工程由Clément Sanchez提供�

设计一个程序有一个字符串包含n个字符 写一个函数 将此字符串中从第m个字符开始的全部字符复制成为另一个字符串 用指针c语言

以下是用指针实现将字符串中从第m个字符开始的全部字符复制成为另一个字符串的C语言程序: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> void copyString(char *a, char *b, int n, int m); int main() { int n, m; char *a, *b; printf("请输入字符串长度n:"); scanf("%d", &n); a = (char*)malloc(n * sizeof(char)); b =

基于C#多机联合绘图软件的实现-毕业设计论文.doc

基于C#多机联合绘图软件的实现-毕业设计论文.doc

4G车载网络中无线电资源的智能管理

4G车载网络中无线电资源的智能管理汽车网络从4G到5G的5G智能无线电资源管理巴黎萨克雷大学博士论文第580号博士学院博士专业:网络、信息与通信研究单位:巴黎萨克雷大学,UVSQ,LI PARAD,78180,法国伊夫林省圣昆廷参考:凡尔赛大学-伊夫林省圣昆廷论文于11月30日在巴黎萨克雷发表并答辩2021年,由玛丽亚姆·阿卢奇·马迪陪审团组成Pascal Lorenz总裁上阿尔萨斯大学大学教授Mohamed Yacine Ghamri-Doudane拉罗谢尔大学报告员和审查员教授Rami Langar报告员和审查员马恩河谷大学Oyunchimeg SHAGDARVEDECOM研发(HDR)团队负责人审查员论文方向Samir TOHME博士生导师巴黎萨克雷大学名誉教授UVSQ/LI- PARADKALLEL KHEMIRI共同监督巴黎萨克雷UVSQ/大卫Guy Pujolle受邀索邦大学Tara Yahiya邀请巴黎萨克雷大学/LISN高级讲师(HDR)博士论文NNT:2021UPASG061谢谢你首先,我要感谢我的论文导师M.萨米�