| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 848 人关注过本帖
标题:多项式加减乘
只看楼主 加入收藏
吴珂
Rank: 1
等 级:新手上路
帖 子:16
专家分:2
注 册:2010-3-31
结帖率:50%
收藏
 问题点数:0 回复次数:1 
多项式加减乘
标题: 多项式加减乘
时 限: 5000 ms
内存限制: 10000 K
总时限: 15000 ms
描述: 多项式加减乘
有A,B,C…… n 个关于x的个多项式,求一个关于A,B,C……的多项式的结果
多项式的每一项之间用空格隔开,每一项的系数,x,指数之间也用空格隔开,即多项式中的每一项的表达方式为 “系数 x 指数”
输入: 基于多项式A,B,C……的运算表达式
关于x的多项式A
关于x的多项式B
关于x的多项式C

输出: 表达式的运算结果
输出多项式表达式按指数降序排序(小数点后保持2位)
输入样例: (A+B)*(A-B)
A 1 x 2.1 3 x -3.09
B 2 x 1 2 x 0
输出样例: 9.00 x -6.18 6.00 x -0.99 -4.00 x 0.00 -8.00 x 1.00 -4.00 x 2.00 1.00 x 4.20
搜索更多相关主题的帖子: 多项式 
2010-05-26 19:56
军刀之恋
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-3-31
收藏
得分:0 
#include<malloc.h>
#include<iostream.h>


typedef struct Polynomialnomial
{
    int coef,ex;
 Polynomialnomial *next;
}Polynomial;

//创建多项式
Polynomial *create_Polynomial(void);
//多项式加法
Polynomial *addition_of_Polynomialnomial(Polynomial *,Polynomial *);
//多项式乘法
Polynomial *Polynomialnomial_Multiplication(Polynomial *,Polynomial *);
//输入多项式
void print_Polynomial(Polynomial *);
//多项式系数取反
Polynomial *negate_Polynomial(Polynomial *head);


int main(void)
{
    static Polynomial *heada2,*headb2,*heada=NULL,*headb=NULL,*headsum=NULL,*headmul=NULL;
 Polynomial *headsubduction=NULL,*headsubduction2=NULL;

 int choose;
 int flag=1;
 do{
  cout<<"+++++++++++++++++++++++++"<<endl;
  cout<<"+       欢迎使用!      +"<<endl;
  cout<<"+     请创建多项式      +"<<endl;
  cout<<"+++++++++++++++++++++++++"<<endl;
  cout<<"        创建多项式A:\n";
  heada=create_Polynomial();
  cout<<"\n        创建多项式B:\n";
  headb=create_Polynomial();
  cout<<"\n多项式A:\t";
  print_Polynomial(heada);
  cout<<"多项式B:\t";
  print_Polynomial(headb);
  int t=1;
  do{
   cout<<"\n请选择你要进行的操作:"<<endl;
   cout<<"1.A+B   2.A-B \n3.B-A   4.A*B"<<endl;
   cout<<"选择:";
   cin>>choose;
   switch(choose)
   {
   case 1:
    cout<<"A+B:"<<endl;
    headsum=addition_of_Polynomialnomial(heada,headb);
    cout<<"两个多项式的和:\t";
    print_Polynomial(headsum);
    break;
   case 2:
    cout<<"A-B:\n";
    cout<<"第一个多项式减去第二个多项式:\t";
    headb2=negate_Polynomial(headb);
    headsubduction=addition_of_Polynomialnomial(heada,headb2);
    print_Polynomial(headsubduction);
    negate_Polynomial(headb);
    break;
   case 3:
    cout<<"B-A:"<<endl;
    cout<<"第二个多项式";
    cout<<"减去第一个多项式:\t";
    heada2=negate_Polynomial(heada);
    headsubduction2=addition_of_Polynomialnomial(headb,heada2);
    print_Polynomial(headsubduction2);
    negate_Polynomial(heada);
    break;
   case 4:
    headmul=Polynomialnomial_Multiplication(heada, headb);
    cout<<"两个多项式的积:\t";
    print_Polynomial(headmul);
    break;
   }//switch
   cout<<"\n继续操作?\n";
   cout<<"1.是的   0.不是\n";
   cout<<"选择:";
   cin>>t;
  }while(t==1);
  cout<<"+++++++++++++++++++++++++"<<endl;
  cout<<"+      继续创建多项式? +\n";
  cout<<"+      1.是的   0.不是  +\n";
  cout<<"+++++++++++++++++++++++++"<<endl;
  cout<<"    选择:";
  cin>>flag;
  cout<<endl;
 }while(flag==1);
 cout<<"+++++++++++++++++++++++++"<<endl;
 cout<<"+        您已经退出!    +"<<endl;
 cout<<"+++++++++++++++++++++++++"<<endl;
 return 0;
}


//创建
Polynomial *create_Polynomial()
{
    int coefficient=0,exponent=0;
 Polynomial *head=NULL,*temp=NULL,*rear=NULL;
 head=(Polynomial *)malloc(sizeof(Polynomial));
 rear=head;
 cout<<"请输入系数 指数(指数降序排列,以 0 0 结束):\n";
 cin>>coefficient>>exponent;
 while(coefficient != 0)
 {
  temp=(Polynomial *)malloc(sizeof(Polynomial));
  temp->coef=coefficient;
  temp->ex=exponent;
  rear->next=temp;
  rear=temp;
  cin>>coefficient>>exponent;
 }
 rear->next=NULL;
 return head;
}


Polynomial *addition_of_Polynomialnomial(Polynomial *p1, Polynomial *p2) //多项式相加
{
 Polynomial *head, *tail, *temp;
    int value;
    p1=p1->next;
    p2=p2->next;
    head=tail=(Polynomial*)malloc(sizeof(Polynomial));
    head->next=NULL;
    while(p1 && p2)
 {
  if((p1->ex) == (p2->ex))
  {
   value=(p1->coef)+(p2->coef);
            if(value != 0)
   {
    temp=(Polynomial *)malloc(sizeof(Polynomial));
                temp->coef=value;
    temp->ex=p1->ex;
    temp->next=NULL;      
   }
   if(value==0 && !p1->next && !p2->next)
   {
    p1=p1->next;
    p2=p2->next;
    break;
   }
   if(value==0 && p1->next && p2->next)
   {
    p1=p1->next;
    p2=p2->next;
    continue;
   }
   p1=p1->next;
   p2=p2->next;
  }
  else if((p1->ex) > (p2->ex))
  {
   temp=(Polynomial *)malloc(sizeof(Polynomial));
   temp->coef=p1->coef;
   temp->ex=p1->ex;
   temp->next=NULL;
   p1=p1->next;
  }
  else
  {
   temp=(Polynomial *)malloc(sizeof(Polynomial));
   temp->coef=p2->coef;
   temp->ex=p2->ex;
   temp->next=NULL;
   p2=p2->next;
  }
  if(head->next==NULL)
  {
   head->next=temp;
   tail=temp;
  }
  else
  {
   tail->next=temp;
   tail=temp;
  }
 }
    tail->next=p1?p1:p2; //有剩余项的接在后面。
 return head;
}

//相乘
Polynomial *Polynomialnomial_Multiplication(Polynomial *p1, Polynomial *p2)
{
 Polynomial *head;
    Polynomial *t,*q,*s,*r;
 head=(Polynomial *)malloc(sizeof(Polynomial));
 head->next=NULL;
 r=(Polynomial *)malloc(sizeof(Polynomial));
 r->next=NULL;
 for(t=p1->next;t;t=t->next)
 {
  for(q=p2->next;q;q=q->next)
  {
   s=(Polynomial *)malloc(sizeof(Polynomial));
   r->next=s;
   s->coef=q->coef * t->coef;
   s->ex=q->ex + t->ex;
   s->next=NULL;
   head=addition_of_Polynomialnomial(r,head);//调用加法函数
  }
 }
 return head;
}


//输出
void print_Polynomial(Polynomial *head)
{
 Polynomial *p=NULL;
 p=head->next;
 if(p == NULL)
 {
  cout<<"0\n";
  return;
 }
 else
 {
  do
  {
   if(p->coef>=0 && p->ex!=0)
    cout<<"+"<<p->coef<<"x^"<<p->ex;
   if(p->coef>=0 && p->ex==0)
    cout<<"+"<<p->coef;

   if(p->coef<0 && p->ex!=0)
    cout<<p->coef<<"x^"<<p->ex;
   if(p->coef<0 && p->ex==0)
    cout<<p->coef;
   p=p->next;
  }while(p != NULL);
  cout<<endl;
 }
}

//多项式的取反
Polynomial *negate_Polynomial(Polynomial *head)
{
 Polynomial *p=NULL;
 p=head->next;
 if(p == NULL)
 {
  cout<<"这个多项式是空的.\n";
  return NULL;
 }
 else
 {
  while(p)
  {
   p->coef=0-(p->coef);

   p=p->next;
  }
 }
 return head;
}

2011-03-31 10:14
快速回复:多项式加减乘
数据加载中...
 
   



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

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