| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 913 人关注过本帖
标题:关于多项式的相乘问题
只看楼主 加入收藏
langziyou
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-5-7
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
关于多项式的相乘问题
//用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;
}
搜索更多相关主题的帖子: 多项式 相乘 
2010-05-08 09:41
a17674
Rank: 2
等 级:论坛游民
帖 子:3
专家分:12
注 册:2010-5-8
收藏
得分:10 
帮你顶一下
2010-05-08 10:19
hzh512
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:6
帖 子:234
专家分:1333
注 册:2009-6-5
收藏
得分:10 
写程序怎么能用 拼音呢!!
帮你写了一个,自己回去琢磨一下吧。
测试实例:
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;
} 




编程=用几种语言在某个或几个平台上通过抽象思维运用一系列算法来解决现实中问题的手段
2010-05-08 13:46
流氓之父
Rank: 2
来 自:新疆
等 级:论坛游民
帖 子:104
专家分:72
注 册:2009-6-2
收藏
得分:0 
永远
2010-05-25 12:14
快速回复:关于多项式的相乘问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018660 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved