链表实现一元多项式加,减,乘,求导.
#include<iostream>
#include<cstring>
using namespace std;
typedef struct polyn{
float coef;
int expn;
struct polyn *next;
}POLYN, *pPOLYN;
pPOLYN polynAdd(pPOLYN a, pPOLYN b,int judge);
pPOLYN createPolyn();
void printPolyn(pPOLYN head);
pPOLYN copyPolyn(pPOLYN a);
pPOLYN polynMul(pPOLYN a, pPOLYN b);
pPOLYN itemMul(pPOLYN a, pPOLYN b);
pPOLYN polynDer(pPOLYN a);
pPOLYN polynDer(pPOLYN a){
pPOLYN tem, p1;
p1 = a->next;
if (p1->expn==0)
{
a->next = p1->next;
a->expn--;
tem = p1;
p1 = p1->next;
}
while (p1!=NULL){
p1->coef *= p1->expn;
p1->expn--;
p1 = p1->next;
}
return a;
}
pPOLYN itemMul(pPOLYN a, pPOLYN b){
pPOLYN res;
if (!(res = (pPOLYN)malloc(sizeof(POLYN)))){
cout << "error!" << endl;
exit(0);
}
res->coef = a->coef*b->coef;
res->expn = a->expn + b->expn;
return res;
}
pPOLYN polynMul(pPOLYN a,pPOLYN b){
pPOLYN c,p1, p2;
pPOLYN jItems[1000];
pPOLYN iItems[1000];
c =(pPOLYN) malloc(sizeof(POLYN));
c->next = NULL;
c->expn = 0;
p1 = a->next;
p2 = b->next;
for (int i = 0; i < a->expn; i++)
{
for (int j = 0; j < b->expn; j++)
{
jItems[j]=itemMul(p1,p2);
jItems[j]->next = NULL;
p2 = p2->next;
}
iItems[i] = (pPOLYN)malloc(sizeof(POLYN));
iItems[i]->expn =0;
iItems[i]->next = NULL;
for (int k = 0; k <b->expn; k++)
{
pPOLYN tem = (pPOLYN)malloc(sizeof(POLYN));
tem->next = jItems[k];
tem->expn = 1;
iItems[i]=polynAdd(iItems[i],tem,1);
}
p2 = b->next;
p1 = p1->next;
}
for (int i = 0; i < a->expn; i++)
{
c = polynAdd(c,iItems[i],1);
}
return c;
}
pPOLYN createPolyn(){
int min=-99999,num;
pPOLYN q,p,head;
cout << "请输入创建的一元多项式项数"<< endl;
if (!(head = (POLYN *)malloc(sizeof(POLYN)))){
cout << "error" << endl;
exit(0);
}
cin >> head->expn;
num = head->expn;
if (head->expn==0)
{
return head;
}
q = head;
q->next = NULL;
for (int i = 0; i < num; i++)
{
if (!(p = (POLYN *)malloc(sizeof(POLYN))))
{
cout << "error" << endl;
exit(0);
}
cout << "输入第"<<i+1<<"个项的指数和系数:" << endl;
cin >> p->expn>>p->coef;
while (p->expn<min)
{
cout << "输入的指数不能比上一项小!" << endl;
cout << "输入第" << i + 1 << "个项的指数和系数:" << endl;
cin >> p->expn >> p->coef;
}
if (p->expn==min)
{
q->coef += p->coef;
head->expn--;
free(p);
continue;
}
min = p->expn;
q->next = p;
q = q->next;
}
q->next = NULL;
p = head;
while (p->next!=NULL){
if (p->next->coef==0)
{
q = p->next;
p->next = q->next;
free(q);
head->expn--;
}
p = p->next;
}
return head;
}
void printPolyn(pPOLYN head){
int len = head->expn;
pPOLYN p1 = head->next;
for (int i = 0; i < len; i++)
{
cout <<"第"<<i+1<<"项指数:"<<p1->expn<<" \t系数:"<<p1->coef<< endl<<endl;
p1 = p1->next;
}
}
pPOLYN copyPolyn(pPOLYN a){
pPOLYN q,p,tem,head;
tem = a->next;
if (!(q = (pPOLYN)malloc(sizeof(POLYN))))
{
cout << "error" << endl;
exit(0);
}
head = q;
q->expn = a->expn;
for (int i = 0; i <a->expn ; i++)
{
if (!(p = (pPOLYN)malloc(sizeof(POLYN))))
{
cout << "error" << endl;
exit(0);
}
p->coef = tem->coef;
p->expn = tem->expn;
tem = tem->next;
q->next = p;
q = q->next;
}
q->next = NULL;
return head;
}
pPOLYN polynAdd(pPOLYN a,pPOLYN b,int judge){
pPOLYN c,p1, p2,p3;
p1 = a->next;
p2 = b->next;
c=p3 = a;
c->expn = a->expn + b->expn;
while (p1!=NULL&&p2!=NULL){
if (judge==2)
{
p2->coef = -p2->coef;
}
if (p1->expn<p2->expn)
{
p3->next = p1;
p3 = p3->next;
p1 = p1->next;
}
else if (p1->expn>p2->expn){
p3->next = p2;
p3 = p3->next;
p2 = p2->next;
}
else if (p1->expn==p2->expn)
{
pPOLYN tem;
c->expn--;
if ((p1->coef+p2->coef)!=0)
{
tem = p2;
p3->next = p1;
p3 = p3->next;
p1 = p1->next;
p3->coef += p2->coef;
p2 = p2->next;
free(tem);
}
else{
c->expn--;
tem = p1;
p1 = p1->next;
free(tem);
tem = p2;
p2 = p2->next;
free(tem);
}
}
p3->next = NULL;
}
if (p1!=NULL){
p3->next = p1;
}
if (p2!=NULL){
p3->next = p2;
}
free(b);
return c;
}
int main(){
pPOLYN operand1, operand2, addRes, copy1, copy2, reduceRes, mulRes, copyA, copyB, derRes;
operand1=createPolyn();
operand2 = createPolyn();
copy1 = copyPolyn(operand1);
copy2 = copyPolyn(operand2);
copyA = copyPolyn(operand1);
copyB = copyPolyn(operand2);
addRes = polynAdd(operand1, operand2, 1);
reduceRes = polynAdd(copy1,copy2,2);
mulRes = polynMul(copyA,copyB);
cout << "两个一元多项式相加:" << endl;
printPolyn(addRes);
cout << "两个一元多项式相减:" << endl;
printPolyn(reduceRes);
cout << "两个一元多项式相乘:" << endl;
printPolyn(mulRes);
derRes = polynDer(copyA);
cout << "第一个一元多项式求导:" << endl;
printPolyn(derRes);
getchar();
getchar();
return 0;
}