西南交大数据结构实验报告Word文档下载推荐.docx

上传人:b****4 文档编号:7816787 上传时间:2023-05-09 格式:DOCX 页数:92 大小:40.17KB
下载 相关 举报
西南交大数据结构实验报告Word文档下载推荐.docx_第1页
第1页 / 共92页
西南交大数据结构实验报告Word文档下载推荐.docx_第2页
第2页 / 共92页
西南交大数据结构实验报告Word文档下载推荐.docx_第3页
第3页 / 共92页
西南交大数据结构实验报告Word文档下载推荐.docx_第4页
第4页 / 共92页
西南交大数据结构实验报告Word文档下载推荐.docx_第5页
第5页 / 共92页
西南交大数据结构实验报告Word文档下载推荐.docx_第6页
第6页 / 共92页
西南交大数据结构实验报告Word文档下载推荐.docx_第7页
第7页 / 共92页
西南交大数据结构实验报告Word文档下载推荐.docx_第8页
第8页 / 共92页
西南交大数据结构实验报告Word文档下载推荐.docx_第9页
第9页 / 共92页
西南交大数据结构实验报告Word文档下载推荐.docx_第10页
第10页 / 共92页
西南交大数据结构实验报告Word文档下载推荐.docx_第11页
第11页 / 共92页
西南交大数据结构实验报告Word文档下载推荐.docx_第12页
第12页 / 共92页
西南交大数据结构实验报告Word文档下载推荐.docx_第13页
第13页 / 共92页
西南交大数据结构实验报告Word文档下载推荐.docx_第14页
第14页 / 共92页
西南交大数据结构实验报告Word文档下载推荐.docx_第15页
第15页 / 共92页
西南交大数据结构实验报告Word文档下载推荐.docx_第16页
第16页 / 共92页
西南交大数据结构实验报告Word文档下载推荐.docx_第17页
第17页 / 共92页
西南交大数据结构实验报告Word文档下载推荐.docx_第18页
第18页 / 共92页
西南交大数据结构实验报告Word文档下载推荐.docx_第19页
第19页 / 共92页
西南交大数据结构实验报告Word文档下载推荐.docx_第20页
第20页 / 共92页
亲,该文档总共92页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

西南交大数据结构实验报告Word文档下载推荐.docx

《西南交大数据结构实验报告Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《西南交大数据结构实验报告Word文档下载推荐.docx(92页珍藏版)》请在冰点文库上搜索。

西南交大数据结构实验报告Word文档下载推荐.docx

while(q&&q-〉expn〈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;

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);

{if(p->

coef〉0)printf(”+”);

if(p->

%。

p->

coef);

elseprintf(”%。

2lfx^%d"

coef,p->

p=p->

/************************************************************/

/*判断两个多项式项的关系*/

/************************************************************/

intItemComp(Itemx,Itemy)

{if(x。

expn<

y。

expn)

return(-1);

elseif(x。

expn==y。

elsereturn

(1);

intmenu(void)

{intnum;

system("

cls"

printf(”nowthechoiceyoucanmake:

\n”);

(1)createP(x)\n”);

(2)createQ(x)\n"

(3)p(x)+Q(x)\n”);

(4)P(x)—Q(x)\n"

printf(”(5)p(x)*Q(x)\n”);

printf(”(6)printP(x)\n”);

(7)printQ(x)\n"

printf(”(8)printP(x)+Q(x)\n”);

(9)printP(x)—Q(x)\n”);

printf(”(10)printP(x)*Q(x)\n"

(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)

%sisnotexist!

p);

getchar();

return0;

/************************************************************/

/*两多项式多项式相加*/

/************************************************************/

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;

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->

pb=pb-〉next;

break;

case0:

if(pa—>

coef+pb—>

coef)//相加不为0,写入

CreateItem(s);

s->

s—〉expn=pa->

expn;

last—>

last=last->

}

pa=pa—〉next;

pb=pb->

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);

while(pa&&pb)

{switch(ItemComp(*pa,*pb))

{case—1:

s-〉coef=pa-〉coef;

expn=pa—>

last-〉next=s;

last=last—>

pa=pa-〉next;

coef*(-1);

expn=pb->

pb=pb—>

case0:

if(pa-〉coef—pb—>

{

s—〉coef=pa—〉coef-pb->

last—〉next=s;

last=last—〉next;

break;

last-〉next=pa;

elseif(pb)//pb未到尾,后面附负值

{while(pb)

{CreateItem(s);

s—〉coef=pb—>

coef*(-1);

s—〉expn=pb->

last->

pb=pb—〉next;

next=pb;

returnhead;

/************************************************************/

/*两多项式多项式相乘*/

/************************************************************/

PolynMultPolyn(Polynh1,Polynh2)//两个多项式相乘

{intexpn;

Item*head,*pa,*pb=h2->

next,*s,*pp;

doublecoef;

head->

while(pb)//双层循环,每项都乘到

{pa=h1->

while(pa)

{expn=pa—〉expn+pb->

coef=pa->

coef*pb->

if(InsertLocate(head,expn,&pp))//返回值非0表示插入新结点

{CreateItem(s);

coef=coef;

s—>

expn=expn;

insert(pp,s);

else

pp—〉coef=pp->

coef+coef;

//找到有相同指数,直接加上去

pa=pa—>

}

pb=pb—〉next;

/************************************************************/

/*主函数*/

/************************************************************/

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)

{num=menu();

getchar();

switch(num)

{case1:

//输入第一个多项式,若多项式存在,首先撤消然后再输入

if(h1!

{if(Select(”P(x)isnotEmpty,CreateP(x)again?

))

{Destroy(h1);

h1=Input();

elseh1=Input();

case2:

//输入第二个多项式,若多项式存在,首先撤消然后再输入

if(h2!

{if(Select(”Q(x)isnotEmpty,CreateQ(x)again?

”))

{Destroy(h2);

h2=Input();

elseh2=Input();

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)"

P(x)+Q(x)hasfinished!

case4:

//两多项式相减

)&

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!

getchar();

case5:

//两多项式相乘

PolynNotEmpty(h2,"

Q(X)”))

{h5=MultPolyn(h1,h2);

Output(h1,”P(x)”);

Q(x)”);

Output(h5,”P(x)*Q(x)"

P(x)*Q(x)hasfinished!

case6:

//显示第一个多项式

{Output(h1,"

P(x)"

case7:

//显示第二个多项式

if(PolynNotEmpty(h2,”Q(x)"

{Output(h2,”Q(x)"

getchar();

case8:

//显示相加结果多项式

if(PolynNotEmpty(h3,”P(x)+Q(x)"

{Output(h1,"

P(x)”);

Output(h3,"

P(x)+Q(x)”);

case9:

//显示相减结果多项式

if(PolynNotEmpty(h4,"

P(x)-Q(x)”))

{Output(h1,”P(x)"

Output(h2,"

Q(x)"

Output(h4,"

P(x)—Q(x)”);

case10:

//显示相乘结果多项式

if(PolynNotEmpty(h5,"

P(x)*Q(x)”))

{Output(h1,"

P(x)”);

Output(h5,"

P(x)*Q(x)"

case11:

//结束程序运行。

结束前应先撤消已存在的多项式

/*if(h1!

=NULL)Destroy(h1);

=NULL)Destroy(h2);

if(h3!

=NULL)Destroy(h3);

if(h4!

=NULL)Destroy(h4);

if(h5!

=NULL)Destroy(h5);

*/

return;

getch();

实验三停车场管理

#include<

stdio.h〉

#include<

stdlib。

h>

#include〈string。

conio.h>

#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;

}

//判栈满

intStackFull()

{

if(stack.top==STACKSIZE)

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=Queue.rear=(QNODE

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 高等教育 > 艺术

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2