C++链表多项式加减运算问题
#include<iostream>usingnamespacestd;structpolynomial{doublecoef;//系数intexp;//指数polyno...
#include<iostream>
using namespace std;
struct polynomial
{
double coef; // 系数
int exp; // 指数
polynomial *next;
}*headA,*headB,*addAB,*cutAB;
void insert(polynomial * head ,double c,int e)
{
polynomial *p = head,*q;
q =new polynomial;
q -> coef = c;
q -> exp = e;
q -> next = NULL;
while(p->next)
{
if(p->next->exp < e)
break;
if(p->next->exp == e)
{
p -> next -> coef += q -> coef;
return ;
}
p = p->next;
}
q ->next = p->next;
p->next = q;
}
void print(polynomial * head)
{
int i = 0;
polynomial * ptemp = head -> next;
for(;ptemp!= NULL;ptemp = ptemp->next)
{
if(!ptemp -> coef)
continue;
cout<<ptemp->coef;
if(ptemp -> exp == 0)
cout<<"";
else if(ptemp -> exp == 1)
cout<<"X";
else
cout<<"X"<<ptemp -> exp;
if(ptemp -> next &&ptemp -> coef > 0)
cout<<" + ";
else cout<<" ";
i++;
}
}
void add()
{
addAB -> next = NULL;
polynomial *ptemp = addAB;
polynomial *p1 = headA->next,*p2 = headB->next;
for(;p1 != NULL;p1 = p1->next)
insert(addAB,p1->coef,p1->exp);
for(;p2 != NULL;p2 = p2->next)
insert(addAB,p1->coef,p1->exp);
}
void cut()
{
cutAB -> next = NULL;
polynomial *ptemp = cutAB;
polynomial *p1 = headA->next,*p2 = headB->next;
for(;p1 != NULL;p1 = p1->next)
insert(cutAB,- (p1->coef),p1->exp);
for(;p2 != NULL;p2 = p2->next)
insert(cutAB,-(p1->coef),p1->exp);
}
void creat(polynomial * head)
{
head -> next = NULL;
double c;
int e;
while(e>=0)
{
cout<<"input a coef and a exp (exp<0 to end)"<<endl;
cin>>c>>e;
insert(head,c,e);
}
}
int main()
{
cout<<"creat A polynomial"<<endl;
creat(headA);
cout<<"headA = ";
print(headA);
cout<<"creat B polynomial"<<endl;
creat(headB);
cout<<"headB = ";
print(headB);
cout<<"A polynomial add B polynomial"<<endl;
add();
print(addAB);
cout<<"A polynomial minus B polynomial"<<endl;
cut();
print(cutAB);
} 展开
using namespace std;
struct polynomial
{
double coef; // 系数
int exp; // 指数
polynomial *next;
}*headA,*headB,*addAB,*cutAB;
void insert(polynomial * head ,double c,int e)
{
polynomial *p = head,*q;
q =new polynomial;
q -> coef = c;
q -> exp = e;
q -> next = NULL;
while(p->next)
{
if(p->next->exp < e)
break;
if(p->next->exp == e)
{
p -> next -> coef += q -> coef;
return ;
}
p = p->next;
}
q ->next = p->next;
p->next = q;
}
void print(polynomial * head)
{
int i = 0;
polynomial * ptemp = head -> next;
for(;ptemp!= NULL;ptemp = ptemp->next)
{
if(!ptemp -> coef)
continue;
cout<<ptemp->coef;
if(ptemp -> exp == 0)
cout<<"";
else if(ptemp -> exp == 1)
cout<<"X";
else
cout<<"X"<<ptemp -> exp;
if(ptemp -> next &&ptemp -> coef > 0)
cout<<" + ";
else cout<<" ";
i++;
}
}
void add()
{
addAB -> next = NULL;
polynomial *ptemp = addAB;
polynomial *p1 = headA->next,*p2 = headB->next;
for(;p1 != NULL;p1 = p1->next)
insert(addAB,p1->coef,p1->exp);
for(;p2 != NULL;p2 = p2->next)
insert(addAB,p1->coef,p1->exp);
}
void cut()
{
cutAB -> next = NULL;
polynomial *ptemp = cutAB;
polynomial *p1 = headA->next,*p2 = headB->next;
for(;p1 != NULL;p1 = p1->next)
insert(cutAB,- (p1->coef),p1->exp);
for(;p2 != NULL;p2 = p2->next)
insert(cutAB,-(p1->coef),p1->exp);
}
void creat(polynomial * head)
{
head -> next = NULL;
double c;
int e;
while(e>=0)
{
cout<<"input a coef and a exp (exp<0 to end)"<<endl;
cin>>c>>e;
insert(head,c,e);
}
}
int main()
{
cout<<"creat A polynomial"<<endl;
creat(headA);
cout<<"headA = ";
print(headA);
cout<<"creat B polynomial"<<endl;
creat(headB);
cout<<"headB = ";
print(headB);
cout<<"A polynomial add B polynomial"<<endl;
add();
print(addAB);
cout<<"A polynomial minus B polynomial"<<endl;
cut();
print(cutAB);
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询