练习44

 
  1. /*****************************************************************************************
  2.   44. (一元多项式加法) 实现两个整系数一元多项式的加法。例如, 对于多项式
  3.  5*X^6+4*X^3-7*X^4+1 与多项式 50*X^2+4*X, 运算结果为:
  4.  5*X^6-7*X^4+4*X^3+50*X^2+4*X+1。
  5.    程序要求:键盘输入多项式的各项系数及指数,每项系数及指数为一组数据(系
  6.  数及指数之一可为零),以'0,0'结束一个多项式的输入,结果按降幂排列,同类
  7.  项要合并(指数最大不超过30)。
  8.    上例第一式的输入为:    5,6
  9.                            4,3
  10.                           -7,4
  11.                            1,0
  12.                            0,0
  13.   输出结果应为:5*x^6-7*x^4+4*x^3+50*x^2+4*x+1.
  14.   *****************************************************************************************/
  15. #include <stdio.h>
  16. #include <malloc.h>
  17. typedef struct tagNode
  18. {
  19.     int c;
  20.     int p;
  21.     tagNode *next;
  22. } Node;
  23. FILE *fp;
  24. void sort(Node *l)
  25. {
  26.     Node *cur,*max,*pos;
  27.     for(pos=l; pos; pos=pos->next)
  28.     {
  29.         max = pos;
  30.         for(cur=pos->next; cur; cur=cur->next)
  31.         {
  32.             if(cur->p > max->p)
  33.                 max = cur;
  34.         }
  35.         if(max != pos)
  36.         {
  37.             int c,p;
  38.             c = max->c;
  39.             p = max->p;
  40.             max->c = pos->c;
  41.             max->p = pos->p;
  42.             pos->c = c;
  43.             pos->p = p;
  44.         }
  45.     }
  46. }
  47. Node* CreateLink()
  48. {
  49.     int c,p;
  50.     Node *head = NULL;
  51.     Node *tail = NULL;
  52.     Node *temp = NULL;
  53.     
  54.     while(1)
  55.     {
  56.         fscanf(fp,"%d",&c);
  57.         fscanf(fp,"%c",&p);
  58.         fscanf(fp,"%d",&p);
  59.         if(c==0 && p==0)
  60.             break;
  61.         temp = (Node*)malloc(sizeof(Node));
  62.         temp->c = c;
  63.         temp->p = p;
  64.         temp->next = NULL;
  65.         if(!head)
  66.         {
  67.             tail = head = temp;
  68.         }
  69.         else 
  70.         {
  71.             tail->next = temp;
  72.             tail = tail->next;
  73.         }
  74.     }
  75.     return head;
  76. }
  77. Node* AddLink(Node *la, Node *lb)
  78. {
  79.     Node *head=NULL,*tail=NULL,*temp;
  80.     Node *s,*t;
  81.     s = la;
  82.     t = lb;
  83.     while(s && t)
  84.     {
  85.         temp = (Node*)malloc(sizeof(Node));
  86.         temp->next = NULL;
  87.         if(s->p == t->p)
  88.         {
  89.             temp->c = s->c + t->c;
  90.             temp->p = s->p;
  91.             s = s->next;
  92.             t = t->next;
  93.         }
  94.         else if(s->p > t->p)
  95.         {
  96.             temp->c = s->c;
  97.             temp->p = s->p;
  98.             s = s->next;
  99.         }
  100.         else 
  101.         {
  102.             temp->c = t->c;
  103.             temp->p = t->p;
  104.             t = t->next;
  105.         }
  106.         if(!head)
  107.         {
  108.             head = tail = temp;
  109.         }
  110.         else 
  111.         {
  112.             tail->next = temp;
  113.             tail = tail->next;
  114.         }
  115.     }
  116.     if(!t)
  117.         s = t;
  118.     while(s)
  119.     {
  120.         temp = (Node*)malloc(sizeof(Node));
  121.         temp->c = s->c;
  122.         temp->p = s->p;
  123.         s = s->next;
  124.         if(!head)
  125.         {
  126.             head = tail = temp;
  127.         }
  128.         else 
  129.         {
  130.             tail->next = temp;
  131.             tail = tail->next;
  132.         }
  133.     }
  134.     return head;
  135. }
  136. void PrintLink(Node* l)
  137. {
  138.     Node* cur = l;
  139.     while(cur)
  140.     {
  141.         if(cur->c > 0 && cur != l)
  142.             printf("+"); 
  143.         if(cur->p != 0)
  144.         {
  145.             if(cur->p != 1)
  146.             {
  147.                 if(cur->c != 1)
  148.                 {
  149.                     if(cur->c != -1)
  150.                         printf("%dX^%d",cur->c,cur->p);
  151.                     else printf("-X^%d",cur->p);
  152.                 }
  153.                 else
  154.                     printf("X^%d",cur->p);
  155.             }
  156.             else
  157.             {
  158.                 if(cur->c != 1)
  159.                 {
  160.                     if(cur->c != -1)
  161.                         printf("%dX",cur->c);
  162.                     else printf("-X");
  163.                 }
  164.                 else
  165.                     printf("X");
  166.             }
  167.         }
  168.         else printf("%d",cur->c);
  169.         cur = cur->next;
  170.     }
  171.     printf("/n");
  172. }
  173. void main()
  174. {
  175.     Node *la,*lb,*lc;
  176.     fp = fopen("data.txt","r");
  177.     if(!fp) return
  178.     la = CreateLink();
  179.     sort(la);
  180.     PrintLink(la);
  181.     lb = CreateLink();
  182.     sort(lb);
  183.     PrintLink(lb);
  184.     lc = AddLink(la,lb);
  185.     sort(lc);
  186.     PrintLink(lc);
  187.     fclose(fp);
  188. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值