zoukankan      html  css  js  c++  java
  • polynomial multiplication

    use poly ADT:

       1:  #include <stdio.h>
       2:  #include <stdlib.h>
       3:  #define MAX_DEGREE  9999
       4:   
       5:  struct polynomial
       6:  {
       7:      int coeff_array[MAX_DEGREE];
       8:      int maxpower;
       9:  };
      10:  typedef struct polynomial *ptr_to_poly;
      11:  typedef ptr_to_poly poly;
      12:   
      13:   
      14:  void zero_polynomial(poly p)
      15:  {
      16:      int i;
      17:      for(i=MAX_DEGREE;i>=0;i--)
      18:      {
      19:          p->coeff_array[i]=0;
      20:      }
      21:   
      22:      p->maxpower=0;
      23:  }
      24:   
      25:  poly add_polynomial(const poly p1,const poly p2)
      26:  {
      27:      poly sum=malloc(sizeof(struct polynomial));
      28:      zero_polynomial(sum);
      29:      if(p1->maxpower>p2->maxpower)
      30:      {
      31:          sum->maxpower=p1->maxpower;
      32:      }
      33:      else
      34:      {
      35:          sum->maxpower=p2->maxpower;
      36:      }
      37:   
      38:      int i;
      39:      for(i=sum->maxpower;i>=0;i--)
      40:      {
      41:          sum->coeff_array[i]=p1->coeff_array[i]+p2->coeff_array[i];
      42:      }
      43:      return sum;
      44:  }
      45:   
      46:  poly multiply(const poly p1,const poly p2)
      47:  {
      48:      poly res=malloc(sizeof(struct polynomial));
      49:      zero_polynomial(res);
      50:      res->maxpower=p1->maxpower+p2->maxpower;
      51:   
      52:      int i,j;
      53:      for(i=0;i<=p1->maxpower;i++)
      54:      {
      55:          for(j=0;j<=p2->maxpower;j++)
      56:          {
      57:              res->coeff_array[i+j]+=p1->coeff_array[i]*p2->coeff_array[j];
      58:          }
      59:      }
      60:      return res;
      61:  }
      62:   
      63:  void print_poly(poly p)
      64:  {
      65:      int i;
      66:      printf("%d*X(%d)",p->coeff_array[p->maxpower],p->maxpower);
      67:      for(i=p->maxpower-1;i>=0;i--)
      68:      {
      69:          if(p->coeff_array[i]!=0)
      70:          {
      71:              printf("+%d*X(%d)",p->coeff_array[i],i);
      72:          }
      73:      }
      74:      printf("\n");
      75:  }
      76:   
      77:  int main()
      78:  {
      79:      poly p1=malloc(sizeof(struct polynomial));
      80:      poly p2=malloc(sizeof(struct polynomial));
      81:      zero_polynomial(p1);
      82:      zero_polynomial(p2);
      83:      p1->maxpower=9;
      84:      p1->coeff_array[9]=9;
      85:      p1->coeff_array[8]=8;
      86:   
      87:      p2->maxpower=9;
      88:      p2->coeff_array[9]=9;
      89:      p2->coeff_array[8]=8;
      90:      print_poly(p1);
      91:      print_poly(p2);
      92:      poly p3=add_polynomial(p1,p2);
      93:      print_poly(p3);
      94:      poly p4=multiply(p1,p2);
      95:      print_poly(p4);
      96:      return 0;
      97:  }
  • 相关阅读:
    H3C交换机删除VLAN与其绑定端口配置
    H3CNE实验:配置交换机接口
    在H3C交换机上开通一个VLAN并且开通一个端口ping通它
    局域网交换技术知识点
    Java开发中常用的设计模式(二)---单例模式
    Java开发中常用的设计模式(一)---工厂模式
    DevExpress13.2.9 控件使用经验总结
    基于.Net下整合RestSharp,实现REST服务客户端
    基于.Net下整合FastReport,实现条码标签批量打印
    基于.Net下整合IBatis
  • 原文地址:https://www.cnblogs.com/dancewithautomation/p/2559449.html
Copyright © 2011-2022 走看看