logo资料库

数据结构——一元多项式加法、减法、乘法运算的实现.doc

第1页 / 共20页
第2页 / 共20页
第3页 / 共20页
第4页 / 共20页
第5页 / 共20页
第6页 / 共20页
第7页 / 共20页
第8页 / 共20页
资料共20页,剩余部分请下载后查看
1.一元多项式加法、减法、乘法运算的实现 1.1 设计内容及要求 1)设计内容 (1)使用顺序存储结构实现多项式加、减、乘运算。 例如: )( xf  6 8 x  5 5 x  10 x 4  32 x 2  x 10 , )( xg  5 7 x  10 x 4  3 20 x  10 x 2  x 求和结果: )( xf  )( xg  6 8 x  12 x 5  3 20 x  2 22 x  10 (2)使用链式存储结构实现多项式加、减、乘运算, )( xf  100 x 100  5 x 50  30 x 10  10 , )( xg  150 x 90  5 x 50  40 x 20  20 x 10  3 x 求和结果: )( xf  )( xg  100 x 100  150 x 90  40 x 20  10 x 10  3 x  10 2)设计要求 (1)用 C 语言编程实现上述实验内容中的结构定义和算法。 (2)要有 main()函数,并且在 main()函数中使用检测数据调用上述算法。 (3)用 switch 语句设计如下选择式菜单。 ***************数据结构综合性实验**************** *******一、多项式的加法、减法、乘法运算********** ******* ******* ******* ******* ******* ******* ******* 1.多项式创建 2.多项式相加 3.多项式相减 4.多项式相乘 5.清空多项式 0.退出系统 请选择(0—5) ********** ********** ********** ********** ********** ********** ********** ************************************************* *请选择(0-5): 1.2 数据结构设计 根据下面给出的存储结构定义: 1
#define MAXSIZE 20 //定义线性表最大容量 //定义多项式项数据类型 typedef struct { float coef; int expn; }term,elemType; typedef struct { //系数 //指数 term terms[MAXSIZE]; //线性表中数组元素 int last; //指向线性表中最后一个元素位置 }SeqList; typedef SeqList polynomial; 1.3 基本操作函数说明 polynomial*Init_Polynomial(); //初始化空的多项式 int PloynStatus(polynomial*p) //判断多项式的状态 int Location_Element(polynomial*p,term x) 在多项式 p 中查找与 x 项指数相同的项是否存在 int Insert_ElementByOrder(polynomial*p,term x) //在多项式 p 中插入一个指数项 x int CreatePolyn(polynomial*P,int m) //输入 m 项系数和指数,建立表示一元多项式的有序表 p char compare(term term1,term term2) //比较指数项 term1 和指数项 term2 polynomial*addPloyn(polynomial*p1,polynomial*p2) 2
//将多项式 p1 和多项式 p2 相加,生成一个新的多项式 polynomial*subStractPloyn(polynomial*p1,polynomial*p2) //多项式 p1 和多项式 p2 相减,生成一个新的多项式 polynomial*mulitPloyn(polynomial*p1,polynomial*p2) //多项式 p1 和多项式 p2 相乘,生成一个新的多项式 void printPloyn(polynomial*p) //输出在顺序存储结构的多项式 p 1.4 程序源代码 #include #include #include #define NULL 0 #define MAXSIZE 20 typedef struct { float coef; int expn; }term,elemType; typedef struct { term terms[MAXSIZE]; int last; }SeqList; typedef SeqList polynomial; void printPloyn(polynomial*p); int PloynStatus(polynomial*p) { if(p==NULL) { return -1; } else if(p->last==-1) { return 0; } 3
else { return 1; } } polynomial*Init_Polynomial() { polynomial*P; P=new polynomial; if(P!=NULL) { P->last=-1; return P; } else { return NULL; } } void Reset_Polynomial(polynomial*p) { if(PloynStatus(p)==1) { p->last=-1; } } int Location_Element(polynomial*p,term x) { int i=0; if(PloynStatus(p)==-1) return 0; while(i<=p->last && p->terms[i].expn!=x.expn) { i++; } if(i>p->last) { return 0; } else { return 1; } } 4
int Insert_ElementByOrder(polynomial*p,term x) { int j; if(PloynStatus(p)==-1) return 0; if(p->last==MAXSIZE-1) { cout<<"The polym is full!"<last; while(p->terms[j].expn=0) { p->terms[j+1]=p->terms[j]; j--; } p->terms[j+1]=x; p->last++; return 1; } int CreatePolyn(polynomial*P,int m) { float coef; int expn; term x; if(PloynStatus(P)==-1) return 0; if(m>MAXSIZE) { printf("顺序表溢出\n"); return 0; } else { printf("请依次输入%d 对系数和指数...\n",m); for(int i=0;i
} } return 1; } char compare(term term1,term term2) { if(term1.expn>term2.expn) { return'>'; } else if(term1.expn': p3->terms[k++]=p1->terms[i++]; p3->last++; break; case'<': p3->terms[k++]=p2->terms[j++]; p3->last++; break; case'=': if(p1->terms[i].coef+p2->terms[j].coef!=0) 6
{ p3->terms[k].coef=p1->terms[i].coef+p2->terms[j].coef; p3->terms[k].expn=p1->terms[i].expn; k++; p3->last++; } i++; j++; } } while(i<=p1->last) { p3->terms[k++]=p1->terms[i++]; p3->last++; } return p3; } polynomial*subStractPloyn(polynomial*p1,polynomial*p2) { int i; i=0; if((PloynStatus(p1)!=1)||(PloynStatus(p2)!=1)) { return NULL; } polynomial*p3=Init_Polynomial(); p3->last=p2->last; for(i=0;i<=p2->last;i++) { p3->terms[i].coef=-p2->terms[i].coef; p3->terms[i].expn=p2->terms[i].expn; } p3=addPloyn(p1,p3); return p3; } polynomial*mulitPloyn(polynomial*p1,polynomial*p2) { int i; int j; int k; i=0; if((PloynStatus(p1)!=1)||(PloynStatus(p2)!=1)) { return NULL; 7
} polynomial*p3=Init_Polynomial(); polynomial**p=new polynomial*[p2->last+1]; for(i=0;i<=p2->last;i++) { for(k=0;k<=p2->last;k++) { p[k]=Init_Polynomial(); p[k]->last=p1->last; for(j=0;j<=p1->last;j++) { p[k]->terms[j].coef=p1->terms[j].coef*p2->terms[k].coef; p[k]->terms[j].expn=p1->terms[j].expn+p2->terms[k].expn; } p3=addPloyn(p3,p[k]); } } return p3; } void printPloyn(polynomial*p) { int i; for(i=0;i<=p->last;i++) { if(p->terms[i].coef>0 && i>0) cout<<"+"<terms[i].coef; else cout<terms[i].coef; cout<<"x^"<terms[i].expn; } cout<
分享到:
收藏