将两个多项式相加

题目链接:https://pintia.cn/problem-sets/434/problems/5803

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 typedef struct PolyNode* PtrToPolyNode;
  5 struct PolyNode {
  6     int Coef;
  7     int Expon;
  8     PtrToPolyNode Next;
  9 };
 10 
 11 typedef struct PolyNode* Polynomial;
 12 
 13 
 14 int Compare(int e1, int e2)
 15 {    //比较两项指数e1和e2,根据大、小、等三种情况分别返回1, -1, 0
 16     if (e1 > e2)
 17         return 1;
 18     else if (e1 < e2)
 19         return -1;
 20     else
 21         return 0;
 22 }
 23 
 24 void Attach(int coef, int expon, Polynomial *PtrRear)
 25 {
 26     Polynomial p = (Polynomial)malloc(sizeof(struct PolyNode));
 27     p -> Coef = coef;
 28     p->Expon = expon;
 29     p->Next = NULL;
 30     (*PtrRear)->Next = p;
 31     *PtrRear = p;
 32 }
 33 
 34 Polynomial PolyAdd(Polynomial P1, Polynomial P2)
 35 {
 36     Polynomial front, rear, temp;
 37     int sum;
 38     front = rear = (Polynomial)malloc(sizeof(struct PolyNode));
 39     while (P1 && P2)
 40     {
 41         switch (Compare(P1->Expon, P2->Expon))
 42         {
 43         case 1:
 44             Attach(P1->Coef, P1->Expon, &rear);
 45             P1 = P1->Next;
 46             break;
 47         case -1:
 48             Attach(P2->Coef, P2->Expon, &rear);
 49             P2 = P2->Next;
 50             break;
 51         case 0:
 52             sum = P1->Coef + P2->Coef;
 53             if (sum)
 54                 Attach(sum, P1->Expon, &rear);
 55             P1 = P1->Next;
 56             P2 = P2->Next;
 57             break;
 58         }
 59     }
 60     while (P1)
 61     {
 62         Attach(P1->Coef, P1->Expon, &rear);
 63         P1 = P1->Next;
 64     }
 65     while (P2)
 66     {
 67         Attach(P2->Coef, P2->Expon, &rear);
 68         P2 = P2->Next;
 69     }
 70     rear->Next = NULL;
 71     temp = front;
 72     front = front->Next;
 73     free(temp);
 74     return front;
 75 }
 76 
 77 Polynomial CreatePoly()
 78 {
 79     int n, coef, expon;
 80     Polynomial front, rear, temp;
 81     
 82     front = rear = (Polynomial)malloc(sizeof(struct PolyNode));
 83     scanf_s("%d", &n);
 84     while (n--)
 85     {
 86         scanf_s("%d %d", &coef, &expon);
 87         Attach(coef, expon, &rear);
 88     }
 89     rear->Next = NULL;
 90     temp = front;
 91     front = front->Next;
 92     free(temp);
 93     return front;
 94 }
 95 
 96 void printPoly(Polynomial P)
 97 {
 98     Polynomial cell;
 99     cell = P;
100     for (cell; cell; cell = cell->Next)
101     {
102         printf("%dX^%d ", cell->Coef, cell->Expon);
103     }
104 }
105 
106 int main()
107 {
108     Polynomial P1, P2, P3;
109     P1 = CreatePoly();
110     P2 = CreatePoly();
111     P3 = PolyAdd(P1, P2);
112     printPoly(P3);
113 
114 }

 

转载于:https://www.cnblogs.com/hi3254014978/p/9743562.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于两个多项式的相加,可以使用链表来表示多项式。每个链表节点表示一个项,包含指数和系数两个属性。首先,将两个多项式按照指数从大到小的顺序插入链表中,并合并同类项的系数。然后,遍历链表,将每个节点的指数和系数输出即可。 以下是一个示例的代码实现(使用Python语言): ```python # 定义链表节点类 class Node: def __init__(self, exp, coef): self.exp = exp # 指数 self.coef = coef # 系数 self.next = None # 下一个节点 # 多项式相加函数 def add_polynomials(poly1, poly2): dummy = Node(0, 0) # 虚拟头节点 curr = dummy # 当前节点 while poly1 and poly2: if poly1.exp > poly2.exp: curr.next = Node(poly1.exp, poly1.coef) poly1 = poly1.next elif poly1.exp < poly2.exp: curr.next = Node(poly2.exp, poly2.coef) poly2 = poly2.next else: # 合并同类项 coef_sum = poly1.coef + poly2.coef if coef_sum != 0: # 系数和非零才添加节点 curr.next = Node(poly1.exp, coef_sum) poly1 = poly1.next poly2 = poly2.next curr = curr.next # 处理剩余节点 if poly1: curr.next = poly1 if poly2: curr.next = poly2 return dummy.next # 打印多项式 def print_polynomial(poly): if not poly: print("0") return res = "" while poly: if poly.coef != 0: res += f"{poly.coef}x^{poly.exp} + " poly = poly.next res = res.rstrip(" + ") print(res) # 示例用法 # 创建多项式1:3x^4 + 2x^3 + 5x^1 + 1 poly1 = Node(4, 3) poly1.next = Node(3, 2) poly1.next.next = Node(1, 5) poly1.next.next.next = Node(0, 1) # 创建多项式2:4x^3 + 2x^2 - 1x^0 poly2 = Node(3, 4) poly2.next = Node(2, 2) poly2.next.next = Node(0, -1) # 相加并打印结果 result = add_polynomials(poly1, poly2) print_polynomial(result) ``` 输出结果为:3x^4 + 6x^3 + 2x^2 + 5x^1 - 1

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值