数据结构答案.docx
- 文档编号:9240027
- 上传时间:2023-02-03
- 格式:DOCX
- 页数:26
- 大小:20.70KB
数据结构答案.docx
《数据结构答案.docx》由会员分享,可在线阅读,更多相关《数据结构答案.docx(26页珍藏版)》请在冰豆网上搜索。
数据结构答案
一、实习报告题头
题目:
一元稀疏多项式相加
班级:
信管199923301姓名:
丁一学号:
199935361
完成日期:
2001-4-15
二、实习目的和要求
(1)进一步了解一元多项式抽象数据类型定义,掌握线性表的单链表存储结构形式。
(2)输入并建立多项式a和b;
(3)实现求和,即建立多项式c=a+b;
(4)输出和多项式,输出形式为整数序列:
n,c1,e1,c2,e2,…,cn,en,其中n是多项式的项数,ci,ei分别是第i项的系数和指数,序列按指数升序排列
三、实习内容
设计一个程序,演示一元稀疏多项式相加的过程。
测试数据采用:
A(x)=7+3x+9x8+5x17
B(x)=8x+22x7-9x8
四、开发环境和工具
1、程序设计环境:
计算机的外存:
20G内存:
128MB显示器的分辨率:
1024*768打印机:
hpDeskJet640c
2、软件工具,windows2000,visualc++
五、设计过程描述
设计中用带头结点的单链表存储一元多项式,多项式的项数存放在头结点;演示程序以用户和计算机的对话方式执行,即在计算机终端上显示“提示信息”后,由用户在键盘上输入多项式的项数及各项的系数和指数,然后进行相加运算,运算根据一元多项式相加的运算规则进行:
对于两个一元多项式中所有指数相同的项,对应指数相加,若其和不为零,则构成“和多项式”中的一项;对于两个一元多项式中所有指数不相同的项,则分别复抄到“和多项式”中去,复抄时应比较相应的指数值的大小,保证多项式序列的升序排列。
一元多项式的单链表存储结构形式如下:
typedefstruct{
floatcoef;
intexpn;
}term;
typedefstructLnode{
termdata;
structLnode*next;
}Lnode,*Linklist;
抽象数据类型一元多项式定义如下:
ADTPolynomial{
数据对象:
D={ai|ai∈TermSet,I=1,2,…,m,m≥0TermSet中的每个元素包含一个表示系数的实数和表示指数的整数}
数据关系:
R1={i-1,ai>|ai-1,ai∈D,且ai-1中的指数值 基本操作: CreatePolyn(&p,m) 操作结果: 输入m项的系数和指数,建立一元多项式p.cmp(terma,termb)依a的指数值<(或=)(或>b的指数值,分别返回-1,0,+1 AddPolyn(&pa,&pb) 初始条件: 一元多项式pa和pb已存在。 操作结果: 完成多项式相加运算,即pc=pa+pb. PrintPolyn(P) 初始条件: 一元多项式p已存在。 操作结果: 打印输出一元多项式p 程序流程图如下: 七、附录(源程序代码) #include #include typedefstruct{ floatcoef; intexpn; }term; typedefstructLnode{ termdata; structLnode*next; }Lnode,*Linklist; voidmain(){ LinklistL1; LinklistL2; LinklistL3; voidCreatPolyn(Linklist&L,intm); voidPrintPolyn(Linklist&pa,Linklist&pb,Linklist&pc,Linklist&L3); intm1,m2; cout< cin>>m1; cout< cin>>m2; CreatPolyn(L1,m1); CreatPolyn(L2,m2); CreatPolyn(L3,0); Linklistpa,pb,pc; pa=L1->next; pb=L2->next; pc=L3; PrintPolyn(pa,pb,pc,L3); }//主函数 voidCreatPolyn(Linklist&L,intm){ inti;Linklistp; L=(Linklist)malloc(sizeof(Lnode)); L->next=NULL; for(i=m;i>0;--i){ p=(Linklist)malloc(sizeof(Lnode)); cout< cin>>(p->data).coef>>(p->data).expn; p->next=L->next; L->next=p; } }//逆序创建带头结点单链表 intcmp(terma,termb){ if(a.expn else{ if(a.expn==b.expn)return0; elsereturn1; } }//比较指数项 voidPrintPolyn(Linklist&pa,Linklist&pb,Linklist&pc,Linklist&L3){ while(pa&&pb){ terma,b;floatsum; a=pa->data; b=pb->data; switch(cmp(a,b)){ case-1: pc=pc->next=(Linklist)malloc(sizeof(Lnode)); (pc->data).expn=a.expn; (pc->data).coef=a.coef; pa=pa->next; break; case0: pa=pa->next; pb=pb->next; sum=a.coef+b.coef; if(sum==0)break; else{ pc=pc->next=(Linklist)malloc(sizeof(Lnode)); (pc->data).expn=a.expn; (pc->data).coef=sum; } break; case1: pc=pc->next=(Linklist)malloc(sizeof(Lnode)); (pc->data).expn=b.expn; (pc->data).coef=b.coef; pb=pb->next; break; }//switch }//while if(! pa){ pc->next=pb; }else{pc->next=pa; } pc=L3->next; while(pc){ cout<<(pc->data).coef<<(pc->data).expn< pc=pc->next; } }//进行加法运算并输出和多项式 八、测试结果 测试数据采用: A(x)=7+3x+9x8+5x17 B(x)=8x+22x7-9x8 运行结果: pleaseinputthelengthoflinklist1: 4 pleaseinputthelengthoflinklist1: 3 inputthecoefandexpn: 517 98 31 70 -98 227 81 和多项式为: 70 111 227 517 /*多项式加法和乘法示例*/ #include #include #include usingnamespacestd; //定义多项式的项类 classterm{ public: intcoef;//多项式系数 intexp;//多项式指数 //初始化项的系数和指数 term(intc=0,inte=0): coef(c),exp(e){} }; //定义多项式类 classPolyArith{ private: list list list //多项式私有成员函数,用以乘法时的调用 list list { list list : iteratoriter_first=poly_list_first.begin(); list : iteratoriter_second=poly_list_second.begin(); //该while循环针对两个链表迭代器都没有指到结尾的情形 while(iter_first! =poly_list_first.end()&&\ iter_second! =poly_list_second.end()) { termt_temp; termt_first=(term)*iter_first; termt_second=(term)*iter_second; if(t_first.exp>t_second.exp) { poly_list_result.push_back(t_first); iter_first++; } elseif(t_second.exp>t_first.exp) { poly_list_result.push_back(t_second); iter_second++; } else { t_temp.coef=t_first.coef+t_second.coef; t_temp.exp=t_first.coef; poly_list_result.push_back(t_temp); iter_first++; iter_second++; } } //该for循环针对第一个多项式的迭代器没有指到结尾 //第二个指到结尾的情形 for(;iter_first! =poly_list_first.end();iter_first++) { poly_list_result.push_back(*iter_first); } //该for循环针对第二个多项式的迭代器没有指到结尾 //第一个指到结尾的情形 for(;iter_second! =poly_list_second.end();iter_second++) { poly_list_result.push_back(*iter_second); } returnpoly_list_result; } public: //输入函数,用以输入多项式 voidPoly_input() { intn; cout<<"请输入第一个多项式的项数: "< cin>>n; cout<<"按降幂输入第一个多项式的每一项的系数和指数: "; cout< for(inti=1;i<=n;i++) { termt_temp; cout<<"请输入第"< "; cout< cin>>t_temp.coef; cin>>t_temp.exp; m_poly_list_first.push_back(t_temp); } n=0; cout<<"请输入第二个多项式的项数: "< cin>>n; cout<<"按降幂输入第二个多项式的每一项的系数和指数: "; cout< for(intj=1;j<=n;j++) { termt_temp; cout<<"请输入第"< "; cout< cin>>t_temp.coef; cin>>t_temp.exp; m_poly_list_second.push_back(t_temp); } } //输出函数,用以输出多项式 voidPoly_output() { //用以指向输出多项式的第一个元素 list : iteratoriter=m_poly_list_result.begin(); //输出多项式的每一项 for(;iter! =m_poly_list_result.end();) { termt_temp=*iter; cout< if(++iter! =m_poly_list_result.end()) cout<<"+"; } cout< } //加法函数,其基本思想同上边的私有成员函数Poly_add() //此处不带参数,多项式运算对象为私有数据成员 voidPoly_add() { list : iteratoriter_first=m_poly_list_first.begin(); list : iteratoriter_second=\ m_poly_list_second.begin(); while(iter_first! =m_poly_list_first.end()&&\ iter_second! =m_poly_list_second.end()) { termt_temp; termt_first=(term)*iter_first; termt_second=(term)*iter_second; if(t_first.exp>t_second.exp) { m_poly_list_result.push_back(t_first); iter_first++; } elseif(t_second.exp>t_first.exp) { m_poly_list_result.push_back(t_second); iter_second++; } else { t_temp.coef=t_first.coef+t_second.coef; t_temp.exp=t_first.exp; m_poly_list_result.push_back(t_temp); iter_first++; iter_second++; } } for(;iter_first! =m_poly_list_first.end();iter_first++) { m_poly_list_result.push_back(*iter_first); } for(;iter_second! =m_poly_list_second.end();iter_second++) { m_poly_list_result.push_back(*iter_second); } } //乘法函数,用以作多项式乘法 voidPoly_multi() { list list : iteratoriter_first=m_poly_list_first.begin(); for(;iter_first! =m_poly_list_first.end();iter_first++) { list list : iteratoriter_second=\ m_poly_list_second.begin(); for(;iter_second! =m_poly_list_second.end();\ iter_second++) { termt_temp;//用以存储项的中间运算结果 termt_first=(term)*iter_first; termt_second=(term)*iter_second; //此处实现多项式项的相乘 t_temp.coef=t_first.coef*t_second.coef;//系数相乘 t_temp.exp=t_first.exp+t_second.exp;//指数相加 poly_list_temp.push_back(t_temp); } //此处调用私有成员函数Poly_add() poly_list_result=\ Poly_add(poly_list_temp,poly_list_result); } //将运算结果赋值给私有数据成员,用以输出 m_poly_list_result=poly_list_result; } }; //测试函数 intmain() { cout<<"************本程序实现多项式的加法与乘法************"; cout< PolyArithpoly_a; poly_a.Poly_input();//输入多项式 poly_a.Poly_add();//多项式加法 cout<<"多项式加法的运算结果: "< poly_a.Poly_output();//输出多项式 cout< poly_a.Poly_multi();//多项式乘法 cout<<"多项式乘法的运算结果: "< poly_a.Poly_output(); system("pause"); return0; } 一元多项式的计算器 #include #include typedefstructPolynomial{ floatcoef; intexpn; structPolynomial*next; }*Polyn,Polynomial;//Polyn为结点指针类型 voidInsert(Polynp,Polynh){ if(p->coef==0)free(p);//系数为0的话释放结点 else{ Polynq1,q2; q1=h;q2=h->next; while(q2&&p->expn q1=q2; q2=q2->next; } if(q2&&p->expn==q2->expn){//将指数相同相合并 q2->coef+=p->coef; free(p); if(! q2->coef){//系数为0的话释放结点 q1->next=q2->next; free(q2); } } else{//指数为新时将结点插入 p->next=q2; q1->next=p; } } }//Insert PolynCreatePolyn(Polynhead,intm){//建立一个头指针为head、项数为m的一元多项式 inti; Polynp; p=head=(Polyn)malloc(sizeof(structPolynomial)); head->next=NULL; for(i=0;i p=(Polyn)malloc(sizeof(structPolynomial));//建立新结点以接收数据 printf("请输入第%d项的系数与指数: ",i+1); scanf("%f%d",&p->coef,&p->expn); Insert(p,head);//调用Insert函数插入结点 } returnhead; }//CreatePolyn voidDestroyPolyn(Polynp){//销毁多项式p Polynq1,q2; q1=p->next; q2=q1->next; while(q1->next){ free(q1); q1=q2;//指针后移 q2=q2->next; } } voidPrintPolyn(PolynP){ Polynq=P->next; intflag=1;//项数计数器 if(! q){//若多项式为空,输出0 putchar('0'); printf("\n"); return; } while(q){ if(q->coef>0&&flag! =1)putchar('+');//系数大于0且不是第一项 if(q->coef! =1&&q->coef! =-1){//系数非1或-1的普通情况 printf("%g",q->coef); if(q->expn==1)putchar('X'); elseif(q->expn)printf("X^%d",q->expn); } else{ if(q->coef==1){ if(! q->expn)putchar('1'); elseif(q->expn==1)putchar('X'); elseprintf("X^%d",q->expn); } if(q->coef==-1){ if(! q->expn)printf("-1"); elseif(q->expn==1)printf("-X"); elseprintf("-X^%d",q->expn); } } q=q->next; flag++; }//while printf("\n"); }//PrintPolyn intcompare(Polyna,Polynb){ if(a&&b){ if(! b||a->expn>b->expn)return1; elseif(! a||a->expn elsereturn0; } elseif(! a&&b)return-1;//a多项式已空,但b多项式非空 elsereturn1;//b多项式已空,但a多项式非空 }//compare PolynAddPolyn(Polynpa,Polynpb){//求解并建立多项式a+b,返回其头指针 Pol
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 答案
