北京工业大学计算机软件基础实验指导书文档格式.docx
《北京工业大学计算机软件基础实验指导书文档格式.docx》由会员分享,可在线阅读,更多相关《北京工业大学计算机软件基础实验指导书文档格式.docx(106页珍藏版)》请在冰豆网上搜索。
i,I......Insert/*插入元素到指定位置*/
d,D......Delete/*删出指定位置的元素*/
q,Q......Quit/*退出*/
Yourchoice:
编译并运行样本程序;
显示屏上出现:
选择上面4个功能进行测试。
例如:
选择“CreateList”功能,键入:
c<
回车>
,
Pleaseinputtotalnumberofdataitem
键入5<
data[1]=1<
data[2]=2<
data[3]=3<
data[4]=4<
data[5]=5<
Pressanykeytocontinue
程序显示:
程序返回显示主菜单:
c,C......CreateList
i,I......Insert
d,D......Delete
q,Q......Quit
I<
Inputthedatatobeinserted:
88<
Inputthepoistiontobeinserted:
(1--5):
2<
data[1]=1data[2]=88data[3]=2data[4]=3data[5]=4
data[6]=5
Pressanykeytocontinue
i,I.....Insert
d<
Inputtheindex_Noofdatatobedeleted
(1---6):
data[1]=1data[2]=2data[3]=3data[4]=4data[5]=5
2)装入样本程序shiyan12.c或输入自己编写的程序;
4)编译并运行样本程序;
Inputthe1stpolynomial/*要求输入第一个多项式*/
inputcoefandexp(1,2<
CR>
):
3,10<
Inputcoefandexp:
4,8<
5,6<
6,4<
-2,2<
8,0<
0,0<
Inputthe2ndpolynomial/*要求输入第二个多项式*/
4,7<
3,5<
2,2<
12,0<
the1stpolynomialis:
3,104,85,66,4-2,28,0
the2ndpolynomialis:
4,73,52,212,0
/*多项式相加的结果*/
theadditionofthetwopolynomialis:
3,104,84,75,63,56,420,0
/*多项式相乘的结果*/
themultiplicationofthetwopolynomialis:
12,1725,1532,136,1239,1144,1010,958,826,772,624,568,4-8,296,0
3.“约瑟夫环游戏的两种实现方法对比”部分
A.链式结构实现
2)装入样本程序shiyan13.c或输入自己编写的程序;
Inputthetotalnumberofpeople:
8<
inputthenumberofpersonyouaretocall:
4<
48521376thelastone:
6
B.顺序结构实现
2)装入样本程序shiyan14.c或输入自己编写的程序;
Johephasproblem
inputnumberofpeople,numbertocall,andstartnumber:
8,4,1
48521376.
六、实验数据及处理
将上述实验输入、输出数据根据所涉及的问题进行核对,对程序结果进行确认分析。
针对实验内容的各个问题,设计一至二组测试用例,对程序的正确性进行分析。
七、实验报告要求
1.对样本程序(或自己所设计的完成相应功能的程序)进行分析注释;
2.对测试数据及执行结果进行分析;
3.讨论程序的改进方法。
八、思考题
1.如何改进“约瑟夫环游戏”程序设计,使初始时入环的每一个人有“姓名”,“编号”等其它信息,出环的人打印出“姓名”。
2.如何实现两多项式的相减?
/*线性表的程序设计*/
#include<
stdio.h>
conio.h>
#defineNull0
#defineMaxSize1024
typedefintDataType;
typedefstructnode{
DataTypedata[MaxSize];
intlast;
}SequenList;
voidPrintOut(SequenList*L);
intDelete(SequenList*L,inti);
voidCreateList(SequenList*L);
intmain(){
SequenListMyList,*L;
charcmd;
inti,t,x;
L=&
MyList;
L->
last=-1;
do{
do{
clrscr();
printf("
\n\tc,C......CreateList\n"
);
\n\ti,I.....Insert"
\n\td,D......Delete"
\n\tq,Q......Quit\n\tYourchoice:
"
cmd=getchar();
}while((cmd!
='
d'
)&
&
(cmd!
D'
q'
Q'
i'
I'
(cmd!
c'
C'
));
/*or:
while((toupper(cmd)!
=’D’)&
(toupper(cmd)!
=’Q’)&
(toupper(cmd)!
=’I’)&
=’C’));
*/
switch(cmd){
case'
:
CreateList(L);
break;
printf("
\nInputthedatatobeinserted:
scanf("
%d"
&
x);
\nInputthepoistiontobeinserted:
\n(1--%d)\n"
(L->
last+2));
i);
Insert(L,x,i);
PrintOut(L);
getch();
\nInputtheindex_Noofdatatobedeleted\n"
\n(1---%d):
\n"
last+1));
Delete(L,i);
default:
}
}while((cmd!
)&
(cmd!
}
intInsert(SequenList*L,DataTypex,inti){
intj;
p=L;
/*可以删去,本函数中所有的p均改为L.*/
if(p->
last>
=(MaxSize-1)){
\nOverflow!
returnNull;
elseif((i<
1)||(i>
(p->
last+2))){
rangeerror"
else{
for(j=L->
last;
j>
=i-1;
j--)L->
data[j+1]=L->
data[j];
data[i-1]=x;
last=L->
last+1;
return
(1);
voidPrintOut(SequenList*L){
inti;
for(i=0;
i<
=L->
i++){
data[%d]="
i+1);
%d\t"
L->
data[i]);
if((i!
=0)&
((i%4)==0))printf("
}/*PrintOut*/
intDelete(SequenList*L,inti){
if((i<
L->
last+1)){
for(j=i;
j<
j++)L->
data[j-1]=L->
last--;
}
voidCreateList(SequenList*L){
intn,i;
intmydata;
\nPleaseinputtotalnumberofdataitem\n"
n);
n;
mydata);
data[i]=mydata;
last=n-1;
\nPressanykeytocontinue\n"
}/*CreateList*/
/*一元多项式的相加、相乘的程序设计*/
#defineTrue1
#defineFalse0
typedefstructpolyterm{
intcoef;
intexp;
structpolyterm*next;
}TERM;
TERM*reverse(TERM*q);
voidpolyout(TERM*head);
TERM*creatpoly(){
TERM*head,*r,*s;
intm,n;
head=(TERM*)malloc(sizeof(TERM));
%d,%d"
n,&
m);
r=head;
while(n){
s=(TERM*)malloc(sizeof(TERM));
s->
coef=n;
s->
exp=m;
r->
next=s;
r=s;
\nInputcoefandexp:
next=Null;
r=head;
head=head->
next;
free(r);
return(head);
}/*creatpoly*/
TERM*polyadd(TERM*ha,TERM*hb){
TERM*hc,*p,*q,*s,*r;
intx;
p=ha;
q=hb;
hc=(TERM*)malloc(sizeof(TERM));
s=hc;
while((p!
=Null)&
(q!
=Null)){
exp==q->
exp){/*coeficients*/
x=p->
coef+q->
coef;
if(x!
=0){
r=(TERM*)malloc(sizeof(TERM));
r->
exp=p->
exp;
coef=x;
s->
next=r;
s=r;
p=p->
q=q->
elseif(p->
exp<
q->
exp){
coef=q->
r->
exp=q->
s=r;
q=q->
else{/*p->
exp>
exp*/
coef=p->
while(p!
=Null){
while(q!
r=hc;
hc=hc->
free(r);
return(hc);
}/*polyadd*/
TERM*polymulti(TERM*f,TERM*g){/*关于一元多项式相乘的程序*/
TERM*fp,*gp,*hp,*q,*h;
intmaxp,p,r,x;
maxp=f->
exp+g->
h=(TERM*)malloc(sizeof(TERM));
hp=h;
g=reverse(g);
for(r=maxp;
r>
=0;
r--){
x=0;
fp=f;
gp=g;
while((fp!
(gp!
p=fp->
exp+gp->
if(p>
r)fp=fp->
elseif(p<
r)gp=gp->
else{
x+=fp->
coef*gp->
fp=fp->
gp=gp->
}/*endofwhile*/
q=(TERM*)malloc(sizeof(TERM));
exp=r;
q->
hp->
next=q;
hp=q;
}/*endoffor*/
hp=h;
h=h->
free(hp);
return(h);
TERM*reverse(TERM*q){
TERM*p1,*p2;
if(q!
p1=q->
while(p1!
p2=p1->
p1->
q=p1;
p1=p2;
}/*endofwhile*/
return(q);
voidpolyout(TERM*head){
TERM*p,*q;
p=head;
/*p=head->
while(p!
%d,%d"
p->
coef,p->
exp);
p=p->
voidmain(){
TERM*ha,*hb,*hc,*p,*q,*h;
\nInputthe1stpolynomial"
ha=creatpoly();
\nInputthe2ndpolynomial"
hb=creatpoly();
\nthe1stpolynomialis:
polyout(ha);
\nthe2ndpolynomialis:
polyout(hb);
hc=polyadd(ha,hb);
\ntheadditionofthetwopolynomialis:
polyout(hc);
h=polymulti(ha,hb);
\nthemultiplicationofthetwopolynomialis:
polyout(h);
return;
/*约瑟夫环的程序设计*/
stdlib.h>
typedefstructtagnode{
intnum;
structtagnode*next;
}LinkList;
LinkList*creat(intn);
LinkList*select(LinkList*head,intm);
LinkList*head=Null,*last;
intn,m;
\nInputthetotalnumberofpeople:
\ninputthenumberofpersonyouaretocall:
head=creat(n);
last=select(head,m);
n=last->
num;
thelastone:
%d\n"
n);
free(last);
\nPressanykeytocontinue...\n"
}/*main*/
LinkList*select(LinkList*head,intm){
LinkList*p,*q;
inti,t,flag=0;
t=1;
q=p;
/*q--前趋指针,p--当前指针*/
p=q->
t=t+1;
if(t%m==0){/*报数到*/
%4d\t"
num);
if(q->
next==q){flag=1;
break;
q->
next=p->
free(p);
p=q;
elseq=p;
}while((q==p)||(flag==0));
head=p;
}/*Selec