文档详情

多项式表示与运算实训报告.doc

发布:2017-04-03约2.67千字共10页下载文档
文本预览下载声明
多 项 式 的 表 示 与 运 算 实 训 报 告 专业:11级软件 姓名:王建婷 学号:111202070102 指导教师: 王宏刚 软件技术多项式运算 1. 理解 在许多实际应用中,经常会遇到多项式的处理与运算问题,在读了这节之后,我知道以循环链表来表示多项式类,可以有效而合理地利用存储空间,这样会为我们省去时间方便运算。 2. 程序 #includeiostream using namespace std; struct node { int exp; double coef; node*next; }; class Poly {private: node*head; public: Poly(); void in1_Poly(); void in2_Poly(int, int[],double[]); void del_Poly(); void prt_Poly(); Poly operator +(Poly ); Poly operator *(Poly ); }; Poly::Poly() {node*p; p=new node; p-exp=-1; p-next=p; head=p; return; } void Poly::in1_Poly() {node*p,*k; int e; double c; k=head; cout输入:系数空格指数。输入指数-1结束!endl; cince; while(e=0) {p=new node; p-exp=e;p-coef=c; p-next=head; k-next=p; k=p; cince; } return; } void Poly::in2_Poly(int n,int e[],double c[]) {int k; node *p; for (k=n-1;k=0;k--) {p=new node; p-coef=c[k];p-exp=e[k]; p-next=head-next; head-next=p; } return; } void Poly::del_Poly() {node*p,*q; q=head-next; while (q!=head) {p=q-next;delete q;q=p;} q-next=head; return; } void Poly::prt_Poly() {node*k; if(head-next==head) cout空表endl; k=head-next; while(k!=head) {cout(k-coef,k-exp)endl; k=k-next; } return; } Poly Poly::operator +(Poly p2) {Poly p; node*k,*q,*m,*n; int e; double c; k=p.head; m=head-next; n=p2.head-next; while ((m-exp!=-1)||(n-exp!=-1)) {if (m-exp==n-exp) {c=m-coef+n-coef; e=m-exp; m=m-next;n=n-next; } else if (m-expn-exp) {c=m-coef;e=m-exp; m=m-next; } else {c=n-coef;e=n-exp; n=n-next; } if(c!=0) {q=new node; q-exp=e; q-coef=c; q-next=p.head;k-next=q; k=q; } } return(p); } Poly Poly::operator *(Poly p2) {Poly p,p1,p3; node*q,*k,*m,*n; m=head-next; while (m-exp!=-1) {p3=p; k=p1.head; n=p2.head-next; while (n-exp!=-1) {q=new node; q-exp=m-exp+n-exp; q-coef=(m-coef)*(n-coef); q-next=p1.head; k-next=q; n=n-next; k=q; } p=p3+p1; p1.del_Poly(); p3.del_Poly(); m=m-next; } return(p); } #include 789.h int main() {Poly p1,p2,add_p,mul_p; int pel[6]={10,8,5,4,1,0}; double pcl[6]={3.0,4.0,-5.0,2.0,-3.0,10.0}; int pe2[6]={14,
显示全部
相似文档