数据结构实验报告.docx
- 文档编号:26079883
- 上传时间:2023-06-17
- 格式:DOCX
- 页数:73
- 大小:29.42KB
数据结构实验报告.docx
《数据结构实验报告.docx》由会员分享,可在线阅读,更多相关《数据结构实验报告.docx(73页珍藏版)》请在冰豆网上搜索。
数据结构实验报告
西南交通大学
<<数据结构>>
实验报告
专业年级:
软件(4)班
姓名:
魏仁斌
学号:
20122712
实验地点:
X7507
指导教师:
周荣辉
信息科学与技术学院
2013年12月
实验一一元稀疏多项式的计算
#include
#include
#include
typedefstructItem{
doublecoef;
intexpn;
structItem*next;
}Item,*Polyn;
#defineCreateItem(p)p=(Item*)malloc(sizeof(Item));
#defineDeleteItem(p)free((void*)p);
/************************************************************/
/*判断选择函数*/
/************************************************************/
intSelect(char*str)
{charch;
printf("%s\n",str);
printf("InputYorN:
");
do{ch=getch();
}while(ch!
='Y'&&ch!
='y'&&ch!
='N'&&ch!
='n');
printf("\n");
if(ch=='Y'||ch=='y')return
(1);
elsereturn(0);
}
/************************************************************/
/*插入位置定位函数*/
/**************************************************************/
intInsertLocate(Polynh,intexpn,Item**p)
{Item*pre,*q;
pre=h;
q=h->next;
while(q&&q->expn {pre=q; q=q->next; } if(! q) {*p=pre; return (1); } elseif(q->expn==expn) {*p=q; return(0); } else {*p=pre; return(-1); } } /************************************************************/ /*插入结点函数*/ /************************************************************/ voidinsert(Item*pre,Item*p) { p->next=pre->next; pre->next=p; } /************************************************************/ /*输入多项式*/ /************************************************************/ PolynInput(void) { doublecoef; intexpn; Item*h,*p,*pp; CreateItem(h);//产生头结点 h->next=NULL; printf("inputcoefandexpn(ifend,expn=-1)\n"); while (1) { scanf("%lf%d",&coef,&expn);//输入多项式的系数和指数 if(expn==-1)break;//若指数为-1,表示输入结束 if(InsertLocate(h,expn,&pp))//返回值非0表示插入新结点 {CreateItem(p); p->coef=coef; p->expn=expn; insert(pp,p); } elseif(Select("hasthesameexpn,Replaceoldervalue? ")) pp->coef=coef;//指数相同,替换系数 } returnh; } /************************************************************/ /*撤消多项式*/ /************************************************************/ voidDestroy(Polynh) { Item*p=h,*q; while(p! =NULL) { q=p; p=p->next; DeleteItem(q); } } /************************************************************/ /*输出多项式*/ /************************************************************/ voidOutput(Polynh,char*title) { intflag=1; Item*p=h->next; printf("%s=",title); while(p) {if(flag)//表示是否是多项式的第一项 {flag=0; if(p->expn==0||p->coef==0)printf("%.2lf",p->coef); elseif(p->expn==1)printf("%.2lfx",p->coef); elseprintf("%.2lfx^%d",p->coef,p->expn); } else {if(p->coef>0)printf("+"); elseif(p->coef==0)printf(""); elseif(p->expn==0)printf("%.2lf",p->coef); elseif(p->expn==1)printf("%.2lfx",p->coef); elseprintf("%.2lfx^%d",p->coef,p->expn); } p=p->next; } printf("\n"); } /************************************************************/ /*判断两个多项式项的关系*/ /************************************************************/ intItemComp(Itemx,Itemy) {if(x.expn return(-1); elseif(x.expn==y.expn) return(0); elsereturn (1); } /************************************************************/ /*两多项式多项式相加*/ /************************************************************/ PolynAddPolyn(Polynh1,Polynh2) { Item*head,*pa=h1->next,*pb=h2->next,*s,*s0; CreateItem(head); head->next=NULL; while(pa! =NULL&&pb! =NULL) { if(ItemComp(*pa,*pb)==-1) { CreateItem(s); s->coef=pa->coef; s->expn=pa->expn; insert(head,s); pa=pa->next; } elseif(ItemComp(*pa,*pb)==1) { CreateItem(s); s->coef=pb->coef; s->expn=pb->expn; insert(head,s); pb=pb->next; } else { CreateItem(s); s->coef=(pa->coef+pb->coef); s->expn=pa->expn; insert(head,s); pa=pa->next; pb=pb->next; } } while(pa! =NULL) { CreateItem(s0); s0->coef=pa->coef; s0->expn=pa->expn; insert(head,s0); pa=pa->next; } while(pb! =NULL) { CreateItem(s0); s0->coef=pb->coef; s0->expn=pb->expn; insert(head,s0); pb=pb->next; } returnhead; } /************************************************************/ /*两多项式多项式相减*/ /************************************************************/ PolynSubtractPolyn(Polynh1,Polynh2) { Item*head,*pa=h1->next,*pb=h2->next,*s,*s0; CreateItem(head); head->next=NULL; while(pa! =NULL&&pb! =NULL) { if(ItemComp(*pa,*pb)==-1) { CreateItem(s); s->coef=pa->coef; s->expn=pa->expn; insert(head,s); pa=pa->next; } elseif(ItemComp(*pa,*pb)==1) { CreateItem(s); s->coef=(-1)*pb->coef; s->expn=pb->expn; insert(head,s); pb=pb->next; } else { CreateItem(s); s->coef=(pa->coef-pb->coef); s->expn=pa->expn; insert(head,s); pa=pa->next; pb=pb->next; } } while(pa! =NULL) { CreateItem(s0); s0->coef=pa->coef; s0->expn=pa->expn; insert(head,s0); pa=pa->next; } while(pb! =NULL) { CreateItem(s0); s0->coef=(-1)*pb->coef; s0->expn=pb->expn; insert(head,s0); pb=pb->next; } returnhead; } /************************************************************/ /*两多项式多项式相乘*/ /************************************************************/ PolynMultPolyn(Polynh1,Polynh2)//两个多项式相乘 { Item*head,*pa=h1->next,*pb=h2->next,*s,*pp; intflag; CreateItem(head); head->next=NULL; while(pa! =NULL) { while(pb! =NULL) { pp=head; flag=0; while(pp! =NULL) { if(pa->expn+pb->expn==pp->expn) { pp->coef+=(pa->coef*pb->coef); flag=1; break; } pp=pp->next; } if(! flag) { CreateItem(s); s->coef=pa->coef*pb->coef; s->expn=pa->expn+pb->expn; insert(head,s); } pb=pb->next; } pa=pa->next; } returnhead; } /************************************************************/ /*菜单选择*/ /************************************************************/ intmenu(void) {intnum; system("cls"); printf("%20c1--createP(x)\n",''); printf("%20c2--createQ(x)\n",''); printf("%20c3--p(x)+Q(x)\n",''); printf("%20c4--P(x)-Q(x)\n",''); printf("%20c5--p(x)*Q(x)\n",''); printf("%20c6--printP(x)\n",''); printf("%20c7--printQ(x)\n",''); printf("%20c8--printP(x)+Q(x)\n",''); printf("%20c9--printP(x)-Q(x)\n",''); printf("%20c10--printP(x)*Q(x)\n",''); printf("%20c11--Quit\n",''); printf("pleaseselect1,2,3,4,5,6,7,8,9,10,11: "); do{ scanf("%d",&num); }while(num<1||num>11); return(num); } /************************************************************/ /*判断多项式是否存在*/ /************************************************************/ intPolynNotEmpty(Polynh,char*p) {if(h==NULL) {printf("%sisnotexist! \n",p); getchar(); } elsereturn (1); return0; } /************************************************************/ /*主函数*/ /************************************************************/ voidmain() {intnum; Polynh1=NULL;//p(x) Polynh2=NULL;//Q(x) Polynh3=NULL;//P(x)+Q(x) Polynh4=NULL;//P(x)-Q(x) Polynh5=NULL;//P(x)*Q(x) while (1) {num=menu(); getchar(); switch(num) { case1: //输入第一个多项式,若多项式存在,首先撤消然后再输入 if(h1! =NULL) {if(Select("P(x)isnotEmpty,CreateP(x)again? ")) {Destroy(h1); h1=Input(); } } elseh1=Input(); break; case2: //输入第二个多项式,若多项式存在,首先撤消然后再输入 if(h2! =NULL) {if(Select("Q(x)isnotEmpty,CreateQ(x)again? ")) {Destroy(h2); h2=Input(); } } elseh2=Input(); break; case3: //两多项式相加 if(PolynNotEmpty(h1,"p(x)")&&PolynNotEmpty(h2,"Q(X)")) {h3=AddPolyn(h1,h2); Output(h3,"P(x)+Q(X)"); printf("P(x)+Q(x)hasfinished! \n"); getchar(); } break; case4: //两多项式相减 if(PolynNotEmpty(h1,"p(x)")&&PolynNotEmpty(h2,"Q(X)")) {h4=SubtractPolyn(h1,h2); Output(h4,"P(x)-Q(x)"); printf("P(x)-Q(x)hasfinished! \n"); getchar(); } break; case5: //两多项式相乘 if(PolynNotEmpty(h1,"p(x)")&&PolynNotEmpty(h2,"Q(X)")) {h5=MultPolyn(h1,h2); Output(h5,"P(x)*Q(x)"); printf("P(x)*Q(x)hasfinished! \n"); getchar(); } break; case6: //显示第一个多项式 if(PolynNotEmpty(h1,"p(x)")) {Output(h1,"P(x)"); getchar(); } break; case7: //显示第二个多项式 if(PolynNotEmpty(h2,"Q(x)")) {Output(h2,"Q(x)"); getchar(); } break; case8: //显示相加结果多项式 if(PolynNotEmpty(h3,"P(x)+Q(x)")) {Output(h3,"P(x)+Q(x)"); getchar(); } break; case9: //显示相减结果多项式 if(PolynNotEmpty(h4,"P(x)-Q(x)")) {Output(h4,"P(x)-Q(x)"); getchar(); } break; case10: //显示相乘结果多项式 if(PolynNotEmpty(h5,"P(x)*Q(x)")) {Output(h5,"P(x)*Q(x)"); getchar(); } break; case11: //结束程序运行。 结束前应先撤消已存在的多项式 if(h1! =NULL)Destroy(h1); if(h2! =NULL)Destroy(h2); if(h3! =NULL)Destroy(h3); if(h4! =NULL)Destroy(h4); if(h5! =NULL)Destroy(h5); } if(num==11)break; } getch(); } 实验四算术表达式求值 #include #include #include #include #definePLUS0 #defineMINUS1 #definePOWER2 #defineDIVIDE3 #defineLEFTP4 #defineRIGHP5 #defineSTARTEND6 #defineDIGIT7 #definePOINT8 #defineNUM7 #defineNO32767 #defineSTACKSIZE20 //运算符 chara[]={'+','-','*','/','(',')','#'}; //优先关系矩阵,规定: =为0,>为1,<为-1,空为NO intPriorityTable[7][7]={{1,1,-1,-1,-1,1,1}, {1,1,-1,-1,-1,1,1}, {1,1,1,1,-1,1,1}, {1,1,1,1,-1,1,1}, {-1,-1,-1,-1,-1,0,NO}, {1,1,1,1,NO,1,1}, {-1,-1,-1,-1,-1,NO,0} }; intmenu(void); /***************************/ /*输入表达式函数*/ /***************************/ voidInputExpression(charstr[]) {intlen; printf("Inputexpressionstring: \n"); scanf("%s",str); len=strlen(str); str[len]='#'; str[len+1]='\0'; } /*************************************************/ /*确定当前字符类型*/ /*************************************************/ /*若为运算符,则返回运算符在运算符数组中的序号*/ /*若为数字字符,则返回DIGIT*/ /*若为小数点字
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 实验 报告
![提示](https://static.bdocx.com/images/bang_tan.gif)