zoukankan      html  css  js  c++  java
  • 将两个多项式相加

    题目链接: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 }
  • 相关阅读:
    xtoi (Hex to Integer) C function Nanoseconds Network
    Learning Scrapy | 王晨的博客
    Facebook搜索项目多名工程师均来自Google
    Beyond the C++ Standard Library: An Introduction to Boost: Björn Karlsson: 9780321133540: Amazon.com: Books
    归并排序 详解
    NewsFeed 3.0 发布,移植到 Python 3 开源中国 OSChina.NET
    对于拷贝构造函数和赋值构造函数的理解
    python 的os.fork()
    Install C++ Boost on Ubuntu
    石川的blog ,注意
  • 原文地址:https://www.cnblogs.com/hi3254014978/p/9743562.html
Copyright © 2011-2022 走看看