注册 登录
编程论坛 数据结构与算法

关于多项式的相乘问题

langziyou 发布于 2010-05-08 09:41, 913 次点击
//用c语言实现 按降幂输出 请高手帮我修改下
struct LNode *chengfa(LinkList &L1,LinkList &L2)//乘法  
{LNode *p,*q,*L,*m,*n;
LNode *qq;
int xishu,zhishu;
L=(LinkList)malloc(sizeof(LNode));

L->next=NULL;


p=L1->next;
while(p)
 {
      m=L;
      n=L->next;
 
 while(q)
 {
      
      
   qq=(LinkList)malloc(sizeof(LNode));
    xishu=(p->xishu)*(q->xishu);
   zhishu=(p->zhishu)+(q->zhishu);
   qq->xishu=xishu;
   qq->zhishu=zhishu;
   qq->next=NULL;
   while(n&&zhishu<n->zhishu)//指数小后移
    {m=n;
     n=n->next;
    }
   
    if(n&&zhishu==n->zhishu)
          {if(xishu+n->xishu==0)
               {m->next=n->next;
               free(n);}
            else{n->xishu=xishu+n->xishu;}
          }
    else
     {qq->next=n;
      m->next=qq;
     }
               
   /*if (n!=NULL&&zhishu<n->zhishu)
   { m=n;
   n=n->next;
   
   }
   if (n==NULL||zhishu>n->zhishu)
   {qq=m->next;
    n=qq->next;
   }
    else
    {n->xishu+=xishu;
    }*/
    q=q->next;
  }
   
    p=p->next;
    q=L2->next;
  }
  return L;
}
3 回复
#2
a176742010-05-08 10:19
帮你顶一下
#3
hzh5122010-05-08 13:46
写程序怎么能用 拼音呢!!
帮你写了一个,自己回去琢磨一下吧。
测试实例:
Input:
2 1 3 2 0 0
Output:
f(x)=2x+3x^2
Input:
1 1 1 0 0 0
Output:
f(x)=x+1
Output:
The Result is :
f(x)=x+5x^2+3x^3

下面时刻运行程序:
程序代码:
#include <iostream>
using namespace std;
struct list
{

 int coef; //系数
int exp; //指数
list *next;
};
list *Creat(); //创建带头结点的链表
void Display(list *h); //输出链表
list *Merge(list *h1); //合并同类项
list *Multiply(list *h1,list *h2); //实现两个链表相乘
int main()
{

 list *h1,*h2;

 h1=Creat();

 Display(h1);

 h2=Creat();

 Display(h2);

 cout<<"The Result is:"<<endl;

 h1=Multiply(h1,h2);

 Display(h1);

 return 0;
}

list *Creat()//创建带头结点的链表
{

 list *h,*r,*s;//h是头结点,存放项的个数,指向第一项
r=h=new list;

 h->next=NULL;

 while(1)

 {
  s=new list;
  cin>>s->coef>>s->exp;
  if(s->coef==0 )
   break;
  if(h->next==NULL)
  {
   r=s;//r=h->next
   h->next=r;
  }
  else
  {
   r->next=s;
   r=s;
  }
  r->next=NULL;

 }

 return h;
}
void Display(list *h)//输出链表
{

 list *p;

 p=h->next;

 cout<<"f(x)=";

 while(p)

 {
  if(p->next!=NULL)
  {
   switch (p->exp)
   {
   case 0:
    cout<<p->coef<<"+";
    break;
   case 1:
    cout<<"X"<<"+";
    break;
   default:
    cout<<p->coef<<"X^"<<p->exp<<"+";
   }
  }
  else
  {
   switch (p->exp)
   {
   case 0:
    cout<<p->coef;
    break;
   case 1:
    cout<<"X";
    break;
   default:
    cout<<p->coef<<"X^"<<p->exp;
   }
  }
  p=p->next;

 }

 cout<<endl;
}
list *Merge(list *h1)//合并同类项
{

 list *p1,*q1,*q2;
    for(p1=h1->next;p1;p1=p1->next)

 {
  for(q1=p1,q2=q1->next;q2;q1=q2,q2=q2->next)
  {
   if(p1->exp==q2->exp)
   {
    p1->coef+=q2->coef;
    q1->next=q2->next;
    delete q2;
    q2=q1;//q2=q1->next;
   }
  }

 }

 //sort
struct list *temp, *cur, *pre, *preindex, *curindex;

 for(pre=h1->next, cur=h1->next->next; cur !=NULL; pre=cur, cur=cur->next)

 {
  preindex=h1;
  curindex=h1->next;
  while (curindex != cur )
  {
   if (curindex->exp > cur->exp)
   {
    temp=cur;
    cur=cur->next;
    pre->next=cur;
    temp->next=curindex;
    preindex->next=temp;
    break;
   }
   preindex=curindex;
   curindex=curindex->next;
  }

 }

 return h1;
}
list *Multiply(list *h1,list *h2)//实现两个链表相乘
{
    // h1 * h2
    list *result = new list;
    result->next = NULL;
    list *temp = result;

    list *p1, *p2;

    p1 = h1->next;
    while (p1)
    {
        p2 = h2->next;
        while (p2)
        {
            list *q = new list;
            q->coef = p1->coef * p2->coef;
            q->exp = p1->exp + p2->exp;
         
            temp->next = q;
            q->next = NULL;
            temp = q;

 
            p2 = p2->next;
        }
        p1 = p1->next;
    }

    result = Merge(result);
    return result;
}



#4
流氓之父2010-05-25 12:14
永远
1