西南交大数据结构实验报告.docx
- 文档编号:29984989
- 上传时间:2023-08-04
- 格式:DOCX
- 页数:86
- 大小:34.12KB
西南交大数据结构实验报告.docx
《西南交大数据结构实验报告.docx》由会员分享,可在线阅读,更多相关《西南交大数据结构实验报告.docx(86页珍藏版)》请在冰豆网上搜索。
西南交大数据结构实验报告
实验一一元稀疏多项式的计算
#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,flag; Item*h,*p,*q,*pp; CreateItem(h);//产生头结点 h->next=NULL; printf("inputcoefandexpn(ifend,expn=-1)\n"); while (1) {printf("coef="); scanf("%lf",&coef); printf("expn="); scanf("%d",&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)printf("%.2lf",p->coef); elseprintf("%.2lfx^%d",p->coef,p->expn); } else {if(p->coef>0)printf("+"); if(p->expn==0)printf("%.2lf",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); } intmenu(void) {intnum; system("cls"); printf("nowthechoiceyoucanmake: \n"); printf(" (1)createP(x)\n"); printf(" (2)createQ(x)\n"); printf("(3)p(x)+Q(x)\n"); printf("(4)P(x)-Q(x)\n"); printf("(5)p(x)*Q(x)\n"); printf("(6)printP(x)\n"); printf("(7)printQ(x)\n"); printf("(8)printP(x)+Q(x)\n"); printf("(9)printP(x)-Q(x)\n"); printf("(10)printP(x)*Q(x)\n"); printf("(11)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(); return0; } elsereturn (1); } /************************************************************/ /*两多项式多项式相加*/ /************************************************************/ PolynAddPolyn(Polynh1,Polynh2) { Item*head,*last,*pa=h1->next,*pb=h2->next,*s; CreateItem(head);//头结点,不动 last=head; while(pa&&pb) {switch(ItemComp(*pa,*pb)) { case-1: CreateItem(s); s->coef=pa->coef; s->expn=pa->expn; last->next=s; last=last->next; pa=pa->next;break; case1: CreateItem(s); s->coef=pb->coef; s->expn=pb->expn; last->next=s; last=last->next; pb=pb->next;break; case0: if(pa->coef+pb->coef)//相加不为0,写入 { CreateItem(s); s->coef=pa->coef+pb->coef; s->expn=pa->expn; last->next=s; last=last->next; } pa=pa->next; pb=pb->next;break; } } if(pa)//a未到尾 last->next=pa; elseif(pb) last->next=pb; else//两者皆到尾 last->next=NULL; returnhead; } PolynSubtractPolyn(Polynh1,Polynh2) { Item*head,*last,*last1,*pa=h1->next,*pb=h2->next,*s; CreateItem(head); last=head; while(pa&&pb) {switch(ItemComp(*pa,*pb)) {case-1: CreateItem(s); s->coef=pa->coef; s->expn=pa->expn; last->next=s; last=last->next; pa=pa->next;break; case1: CreateItem(s); s->coef=pb->coef*(-1); s->expn=pb->expn; last->next=s; last=last->next; pb=pb->next;break; case0: if(pa->coef-pb->coef)//相加不为0,写入 { CreateItem(s); s->coef=pa->coef-pb->coef; s->expn=pa->expn; last->next=s; last=last->next; } pa=pa->next; pb=pb->next;break; } } if(pa)//a未到尾 last->next=pa; elseif(pb)//pb未到尾,后面附负值 {while(pb) {CreateItem(s); s->coef=pb->coef*(-1); s->expn=pb->expn; last->next=s; last=last->next; pb=pb->next; } last->next=pb; } else//两者皆到尾 last->next=NULL; returnhead; } /************************************************************/ /*两多项式多项式相乘*/ /************************************************************/ PolynMultPolyn(Polynh1,Polynh2)//两个多项式相乘 {intexpn; Item*head,*pa,*pb=h2->next,*s,*pp; doublecoef; CreateItem(head); head->next=NULL; while(pb)//双层循环,每项都乘到 {pa=h1->next; while(pa) {expn=pa->expn+pb->expn; coef=pa->coef*pb->coef; if(InsertLocate(head,expn,&pp))//返回值非0表示插入新结点 {CreateItem(s); s->coef=coef; s->expn=expn; insert(pp,s);//按顺序在插入 } else pp->coef=pp->coef+coef;//找到有相同指数,直接加上去 pa=pa->next; } pb=pb->next; } returnhead; } /************************************************************/ /*主函数*/ /************************************************************/ 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(h1,"P(x)"); Output(h2,"Q(x)"); 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(h1,"P(x)"); Output(h2,"Q(x)"); Output(h4,"Px)-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(h1,"P(x)"); Output(h2,"Q(x)"); 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(h1,"P(x)"); Output(h2,"Q(x)"); Output(h3,"P(x)+Q(x)"); getchar(); } break; case9: //显示相减结果多项式 if(PolynNotEmpty(h4,"P(x)-Q(x)")) {Output(h1,"P(x)"); Output(h2,"Q(x)"); Output(h4,"P(x)-Q(x)"); getchar(); } break; case10: //显示相乘结果多项式 if(PolynNotEmpty(h5,"P(x)*Q(x)")) {Output(h1,"P(x)"); Output(h2,"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);*/ return; } } getch(); } 实验三停车场管理 #include #include #include #include #defineSTACKSIZE3 typedefstruct{ intBno; inttype;//小车1,客车2,货车3 intarrivetime; intpushtime; intdeparturetime; }CAR; //链队结构定义(临时车道) typedefstructQNODE{ CARelm; structQNODE*next; }QNODE; //链队结构定义(注意申明方法,相当全局变量) structLinkQueue{ QNODE*front; QNODE*rear; }Queue; //顺序栈结构定义(停车场) structSqStack{ CARelm[STACKSIZE]; inttop; }stack; //收费标准 intpay[]={0,2,3,5};//每小时小车2元,客车3元,货车5元 //判栈空 intStackEmpty() { if(stack.top==0) return1; else return0; } //判栈满 intStackFull() { if(stack.top==STACKSIZE) return1; elsereturn0; } //顺序栈入栈 voidpush(structSqStack*stack,CARcar) {if(! StackFull()) stack->elm[stack->top++]=car; } //顺序栈出栈(CAR用了引用,不知为啥指针不管用) voidpop(structSqStack*stack,CAR&car) {if(! StackEmpty()) {stack->top--; car=stack->elm[stack->top]; } } //链栈入栈函数 voidLPush(QNODE*stack,QNODE*p) {p->next=stack->next; stack->next=p; } //链栈出栈函数(去掉了stack下一位的结点) voidLPop(QNODE*stack,QNODE**p) {(*p)=stack->next; stack->next=(*p)->next; } //链队初始化 voidInitQueue() {Queue.front=Que
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 西南 交大 数据结构 实验 报告
![提示](https://static.bdocx.com/images/bang_tan.gif)