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