📄 polynamial.cpp
字号:
/////////////////////////////////////////////
// 计算多项式乘法及加法 //
// //
//因为要用到带头结点的链表,所以在建立一个新//
//链表的时候麻烦了一点,要把头结点单独处理不//
//能纳入循环中使得代码过长.同样一个程序有同//
//学只用了120行,我写完代码就有150行,调试完 //
//就达到202行了,我吓了一跳. //
/////////////////////////////////////////////
/////////////////////////////////////////////
/*在调试程序方面获得了不少经验.上周日晚上完成
了除加法和打印以外的其他部分,从周一开始到今天
中午就一直出去调试程序的阶段,所以这么说丝毫也
不夸张.排除发,在怀疑程序出错的地方加入断点,输
出一些容易发现的信息,一步步排除,这样就不用拿
着笔和纸跟着程序走了,效率提高不少;我学会了用
"*/ /*",这样可以跟进一步提高调试的效率*/
/*最近做了两个和链表有关的程序,对单链表的操作可
以用轻车熟路来形容了,为接下来的电梯程序打好了
基础*/
/*越来越喜欢C++的语言风格了,的确在输入代码方面
要灵活不少,而且它减少了C中一些我不喜欢的控制
符之类的东西感觉上更有亲切感了.经过改进C++的
程序也不缺乏可读性,不愧为程序员首选*/
//在新建一个结点时候切记不要忘了给结点的指针赋值
//对于函数,变量的命名要形成一定的习惯,不然容易混淆
//注意逻辑运算"=="于赋值运算"="的区别
#include<iostream.h>
struct node
{
float data;
int exp;
node* next;
};
node* Polynomial_Create()
{
cout<<"Please enter the item number of Polynomial"<<endl;
int ItemNum,i=1;
cin>>ItemNum;
node* NodeEmpty,*NodeTemp,*NodeNew;
NodeEmpty=new node;
NodeEmpty->exp=ItemNum;
NodeEmpty->data=0;
NodeEmpty->next=NULL;
NodeNew=new node;
cout<<"Please enter the exp of NO.1 item"<<endl;
cin>>NodeNew->exp;
cout<<"Please enter the data of NO.1 item"<<endl;
cin>>NodeNew->data;
NodeNew->next=NULL;
NodeEmpty->next=NodeNew;
NodeTemp=NodeNew;
while(i<ItemNum)
{
NodeNew=new node;
cout<<"Please enter the exp of next item"<<endl;
cin>>NodeNew->exp;
cout<<"Please enter the data of next item"<<endl;
cin>>NodeNew->data;
NodeNew->next=NULL;
NodeTemp->next=NodeNew;
NodeTemp=NodeNew;
i++;
};
return NodeEmpty;
}
node* Polynomial_Plus(node*A,node*B)
{
int i=0,e;
node*C_empty=new node,*A_temp=A->next,*B_temp=B->next,*C_temp,*C_last;
C_temp=new node;
C_temp->next=NULL;
e=A_temp->exp-B_temp->exp;
if(e==0)
if((A_temp->data+B_temp->data)!=0)
{
C_temp->exp=A_temp->exp;
C_temp->data=A_temp->data+B_temp->data;
C_empty->next=C_temp;
A_temp=A_temp->next;
B_temp=B_temp->next;
};
if(e>0)
{
C_temp->exp=B_temp->exp;
C_temp->data=B_temp->data;
C_empty->next=C_temp;
B_temp=B_temp->next;
};
if(e<0)
{
C_temp->exp=A_temp->exp;
C_temp->data=A_temp->data;
C_empty->next=C_temp;
A_temp=A_temp->next;
};
C_last=C_temp;
while((A_temp!=NULL)&&(B_temp!=NULL))
{
C_temp=new node;
C_temp->next=NULL;
e=A_temp->exp-B_temp->exp;
if(e==0)
if(A_temp->data+B_temp->data!=0)
{
C_temp->exp=A_temp->exp;
C_temp->data=A_temp->data+B_temp->data;
A_temp=A_temp->next;
B_temp=B_temp->next;
};
if(e>0)
{
C_temp->exp=B_temp->exp;
C_temp->data=B_temp->data;
B_temp=B_temp->next;
};
if(e<0)
{
C_temp->exp=A_temp->exp;
C_temp->data=A_temp->data;
A_temp=A_temp->next;
};
C_last->next=C_temp;
C_last=C_temp;
};
if(A_temp==NULL)
{
while(B_temp!=NULL)
{
C_temp=new node;
C_temp->next=NULL;
C_temp->exp=B_temp->exp;
C_temp->data=B_temp->data;
B_temp=B_temp->next;
C_last->next=C_temp;
C_last=C_temp;
};
};
if(B_temp==NULL)
{
while(A_temp!=NULL)
{
C_temp=new node;
C_temp->next=NULL;
C_temp->exp=A_temp->exp;
C_temp->data=A_temp->data;
A_temp=A_temp->next;
C_last->next=C_temp;
C_last=C_temp;
};
};
return C_empty;
}
node* Polynomial_New(node*item,node*poly)
{
node*result_empty,*poly_temp,*temp,*result_temp;;
int i=1;
poly_temp=poly->next;
result_empty=new node;
result_empty->exp=poly->exp;
result_empty->next=NULL;
result_temp=new node;
result_temp->exp=item->exp+poly_temp->exp;
result_temp->data=poly_temp->data*item->data;
result_temp->next=NULL;
result_empty->next=result_temp;
temp=result_temp;
while(i<=poly->exp-1)
{
poly_temp=poly_temp->next;
result_temp=new node;
result_temp->exp=item->exp+poly_temp->exp;
result_temp->data=poly_temp->data*item->data;
result_temp->next=NULL;
temp->next=result_temp;
temp=result_temp;
i++;
};
return result_empty;
}
node* Polynomial_Multiply(node*A,node*B)
{
node*C=NULL,*temp,*p;
int i=0;
p=A->next;
C=Polynomial_New(p,B);
p=p->next;
while(p!=NULL)
{
temp = Polynomial_New(p,B);
C=Polynomial_Plus(C,temp);
p=p->next;
};
return C;
}
void Polynomial_Print(node*poly)
{
node* temp;
temp=poly->next;
while(temp->next!=NULL)
{
cout<<"("<<temp->data<<"x^"<<temp->exp<<")+";
temp=temp->next;
};
cout<<"("<<temp->data<<"x^"<<temp->exp<<")"<<endl;
}
void main()
{
node *A, *B, *C,*D;
A=Polynomial_Create();
B=Polynomial_Create();
C=Polynomial_Plus(A,B);
D=Polynomial_Multiply(A,B);
cout<<"The first polynomial is:"<<endl;
Polynomial_Print(A);
cout<<"The second polynomial is:"<<endl;
Polynomial_Print(B);
cout<<"The sum of the two is:"<<endl;
Polynomial_Print(C);
cout<<"The product of the two is:"<<endl;
Polynomial_Print(D);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -