| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 629 人关注过本帖
标题:多项式的除法有点问题,怎么改才能使改动最少
只看楼主 加入收藏
jiegeng
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2015-3-22
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
多项式的除法有点问题,怎么改才能使改动最少
#include<iostream>
#include<fstream>
using namespace std;
struct item{
        double xishu;
        int zhishu;
        item* next;
    };


void jiafa(item*elem1,item*elem2);        //加法
void Sub(item *elem1,item *elem2);        //减法
void cheng(item*elem1,item*elem2);        //乘法
void chufa(item*elem1,item*elem2);       //除法
void weifen(item*elem);                  //微分
void paixu(item *elem);                  //对链表按指数的降序排序
void hebing(item*elem);                  //对一个链表中的元素合并同类项
void print(item*elem);                   //输出函数

void paixu(item *elem){
    item*temp,*linshi;
    temp=elem;
    while(temp){
        linshi=temp->next;
         while(linshi){
         if(temp->zhishu<linshi->zhishu){
            swap(temp->xishu,linshi->xishu);
            swap(temp->zhishu,linshi->zhishu);
        }
             linshi=linshi->next;
        }
    temp=temp->next;

    }
}
  



void hebing(item*elem){
    item*temp,*linshi,*fuben;
   
    fuben=elem;
    while(fuben){
        temp=fuben;
while(temp&&temp->next){
    item*s1=temp->next;
    if(temp->next->zhishu==fuben->zhishu){
        fuben->xishu+=temp->next->xishu;
        temp->next=s1->next;
     delete s1;                        //删除已经合并的
    }
   
    temp=temp->next;
}
    fuben=fuben->next;
}
}


void jiafa(item *elem1,item*elem2){
    item*temp1;
    temp1=elem1;
     while(temp1->next!=NULL){
         temp1=temp1->next;
     }
     temp1->next=elem2;              //连接两个链表为一个
    hebing(elem1);
    paixu(elem1);
    print(elem1);
}

void Sub(item*elem1,item*elem2){
item*temp1,*temp2;
temp1=elem1;
temp2=elem2;
while(temp2){
    temp2->xishu=-temp2->xishu;
    temp2=temp2->next;
}
 while(temp1){
         temp1=temp1->next;
     }
     temp1=elem2;              
    hebing(elem1);
    paixu(elem1);
    print(elem1);
}


void cheng(item*elem1,item*elem2){
 item*temp1,*temp2;
 item *linshi,*head,*tail;
 linshi=new item;
 head=linshi;
 tail=head;
 temp1=elem1;
 while(temp1){
     temp2=elem2;
         while(temp2){
             if(linshi){
         linshi->next=NULL;
         tail=linshi;
         linshi->xishu=temp1->xishu*temp2->xishu;
         linshi->zhishu=temp1->zhishu+temp2->zhishu;
             }
        else{
            delete linshi;
            tail->next=NULL;
            break;
        }
          temp2=temp2->next;
        linshi->next=new item;
        linshi=linshi->next;
         }
 temp1=temp1->next;
 }
 delete linshi;
tail->next=NULL;
 hebing(head);
 paixu(head);
 print(head);
}
   
void chufa(item*elem1,item*elem2){
item*max,*min,*temp1,*temp2;
if(elem1->zhishu>elem2->zhishu)
{
    max=elem1;
    min=elem2;
}
else{
    max=elem2;
    min=elem1;
}  
temp1=max;
temp2=min;                                                         //决定分子多项式,大的为分子
double yinzi;
item*store,*jieyong,*head,*tail,*tail2,*head2;
store=new item;
head=store;
tail=head;


int n;
while(temp1){
    jieyong=temp1;
     while(jieyong)
     jieyong=jieyong->next;

     jieyong=new item;
     head2=jieyong;
     tail2=head2;
if(temp1->zhishu>=min->zhishu){
    store->next=NULL;
    tail=store;
yinzi=temp1->xishu/min->xishu;                                    //yinzi为最高项的系数比
store->xishu=yinzi;
n=temp1->zhishu-min->zhishu;
store->zhishu=n;
}
else {
    delete store;
    tail->next=NULL;
    break;
}
store->next=new item;
store=store->next;

 while(temp2->next){
     if(jieyong){
         jieyong->next=NULL;
         tail2=jieyong;
         jieyong->zhishu=n+temp2->next->zhishu;
    jieyong->xishu=-temp2->next->xishu*yinzi;
     }
     else{
         delete jieyong;
         tail2->next=NULL;
         break;
     }
     jieyong->next=new item;
     jieyong=jieyong->next;
    temp2=temp2->next;
 }
 
     delete jieyong;
    tail2->next=NULL;
   
   temp1=temp1->next;
   item*temp3;
  temp3=temp1;
   hebing(temp3);
paixu(temp3);
   
             //分子中去掉一个被除项                                                
}
paixu(head);
print(head);                                                //store里存放商式
}
 

void weifen(item*elem){
    item*temp;
    temp=elem;
while(temp){
    temp->xishu*=temp->zhishu;
    temp->zhishu-=1;
    temp=temp->next;
}
print(elem);
}
 
void print(item*elem){
    item*temp;
    temp=elem;
while(temp->next){
    cout<<temp->xishu<<"x"<<temp->zhishu<<"+";
temp=temp->next;
}
cout<<temp->xishu<<"x"<<temp->zhishu<<endl;//最后一项单独处理
}



void main(){
    item *p1,*p2,*first1,*first2,*last1,*last2;
     p1=new item;
     first1=p1;
     last1=first1;                      //链表1
     p2=new item;
     first2=p2;
     last2=first2;  //链表2
    int a,b;
    ifstream infile1("data.txt");
    while(!infile1.eof()){
    infile1>>a>>b;
    if(a!=0&&b!=0){
        p1->next=NULL;
        last1=p1;
    p1->xishu=a;
    p1->zhishu=b;
    }
    else{
        delete p1;
        last1->next=NULL;
        break;
    }
     p1->next=new item;
     p1=p1->next;
    }
    infile1.close();
    int c,d;
    ifstream infile2("data5.txt");
    while(!infile2.eof()){
    infile2>>c>>d;
    if(c!=0&&d!=0){
        p2->next=NULL;
        last2=p2;
    p2->xishu=c;
    p2->zhishu=d;
    }
    else{
        delete p2;
        last2->next=NULL;
        break;
    }
     p2->next=new item;
     p2=p2->next;
    }
    infile2.close();                      //构建第二个多项式
paixu(first1);
paixu(first2);
cout<<"请输入您想对多项式执行的操作:+,-,*,/"<<endl;
char op;
cin>>op;
if(op=='+')
jiafa(first1,first2);                                 //多项式的加法
else if(op=='-')
    Sub(first1,first2);
else if(op=='*')
    cheng(first1,first2);                           //乘法
else if(op=='/')
    chufa(first1,first2);                               //除法
else{
    weifen(first1);                                    //微分
    weifen(first2);
}
}
搜索更多相关主题的帖子: include double 多项式 cheng 
2015-03-22 23:56
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:14 
多项式除法的规则都忘记了。
2015-03-23 14:11
jiegeng
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2015-3-22
收藏
得分:0 
回复 2楼 yangfrancis
谢谢哈,我己经解决了
2015-03-24 00:10
快速回复:多项式的除法有点问题,怎么改才能使改动最少
数据加载中...
 
   



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

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