多项式的除法有点问题,怎么改才能使改动最少
#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);
}
}