《数据结构》课程设计试验报告.docx
《《数据结构》课程设计试验报告.docx》由会员分享,可在线阅读,更多相关《《数据结构》课程设计试验报告.docx(16页珍藏版)》请在冰豆网上搜索。
《数据结构》课程设计试验报告
《数据结构》课程设计
实验报告
题目约瑟夫问题和八皇后问题求解
学院数学与信息工程学院
专业计算机科学与技术
班级计科081
学号200853225142
学生姓名陈哲
同组成员魏艳舞杨青施晓洁吴亚君徐珍艳张素姿
指导教师刘小晶
编写日期2009年6月28日
一、问题描述
1、约瑟夫问题描述
编号为1,2…n的n个人按顺时针方向围坐一圈,每人持有一个密码(正整数)。
一开始任选一个正整数作为报数的上限值m,从第一个人开始按顺时针方向自1开始顺序报数,报到m时停止报数,报m的人出列,将他的密码作为新的m值,从他的顺时针方向上的下一个开始重新从1报数,如此下去,直至所有人全部出列为止,设计一个程序求出出列顺序。
2、八皇后问题描述
在一个8×8的棋盘里放置8个皇后,要求每个皇后两两之间不相"冲"(在每一横列竖列斜列只有一个皇后)。
3、界面设计模块问题描述
设计一个菜单式界面,让用户可以选择要解决的问题,同时可以退出程序。
界面要求简洁明了,大方得体,便于用户的使用,同时,对于用户的错误选择可以进行有效的处理。
二、问题分析
本人负责的是为用户设计菜单界面,使用户可以根据菜单进行选择其中的某个问题进行处理。
对于一个菜单界面,首先要求界面简单明了,使得用户可以轻松通过界面知道如何获取自己想要的操作。
其次,我们不能保证用户每次的选择都是有效的,即用户的选择是在我们提供的服务范围之内,所以要设计容错操作,即当用户的选择超出我们提供的范围时,提示用户重新选择。
最后,要保证用户选择相应的操作后,程序能正确的按照用户的选择运行下去,完成用户的要求。
三、数据结构描述
Intchoice;
记录用户的选择,然后选择相应的操作。
typedefstructLNode{
intnum;
intcode;
structLNode*next;
}node,*linklist;单链表解决约瑟夫问题时储存结点信息
chara[9][9];八皇后问题中记录棋盘信息
四、算法设计
1、程序层次模块图
程序总层次模块图
2、算法设计
voidjiemian()
{
printf("theproblemofpeopleourofcirclr\n");
printf("****************************************************\n");
printf("*1.solvebylianbiao;*\n");
printf("*2.solvebyshunxubiao;*\n");
printf("*3.help;*\n");
printf("*4.ruturntomenu;*\n");
printf("****************************************************\n");
}
voidhelp()
{
printf("theproblemofpeopleourofcirclr\n");
printf("*******************************************************************************\n");
printf("*youcanchoise1,2,3or4togetthefunctionyouwant.ifyouchoise1,*\n");
printf("*youwillsolvetheproblembylianbiao.inthisway,whenapeopleoutof*\n");
printf("*thecircle,thenodewhichcontainhismessagewillbedeleted.ifyou*\n");
printf("*choise2,youwillsolvetheproblembyshunxubiao.inthisway,whena*\n");
printf("*peopleoutofthecircle,hiscodewillchangeinto0,buthismessage*\n");
printf("*willberemained.ifyouchoise4,endthegame.*\n");
printf("*******************************************************************************\n");
}
voidwelcome()
{
printf("welcometooursystem\n");
printf("****************************************************\n");
printf("*1.JosephCentralissues*\n");
printf("*2.Queen8*\n");
printf("*3.exit*\n");
printf("****************************************************\n");
}
intmain()
{
intchoice;
welcome();
printf("pleasemakeyourchoice:
");
scanf("%d",&choice);
while(choice!
=3)
{
switch(choice)
{
case1:
system("cls");Joseph();getch();break;
case2:
system("cls");Queen();getch();getch();break;
default:
printf("pleasechooosetherightchoice!
");getch();getch();break;
}
system("cls");
welcome();
printf("pleasemakeyourchoice:
");
scanf("%d",&choice);
}
}
五、详细程序清单
#include
#include
typedefstructLNode{
intnum;
intcode;
structLNode*next;
}node,*linklist;//数据结构声明
chara[9][9];
inttotal;
linklistcreatstart(linklistL,intnumber)//建立单向循环链表
{
intm,i;
linklists,p;
s=L;
for(i=1;i<=number;i++)
{
p=(linklist)malloc(sizeof(node));
if(!
p)exit(0);
p->num=i;
printf("pleaseinputthecodeofnumber%d:
",i);
scanf("%d",&p->code);
p->next=NULL;
s->next=p;
s=p;
}
s->next=L->next;
returns;
}
voidchulie(linklistL,intnumber)//链表中的出列
{
intturn,i,j;
linklistp,s;
printf("pleaseinputthestartcode:
");
scanf("%d",&turn);
p=L;
printf("theturnoutofthecircleis:
");
for(i=1;i<=number-1;i++)
{
for(j=1;j<=turn-1;j++)p=p->next;
printf("%d",p->next->num);
turn=p->next->code;
s=p->next;
p->next=s->next;
free(s);
}
printf("%d",p->next->num);
printf("\n");
}
voidlianbiao()//用单向循环链表解决约瑟夫问题
{
intnumber;
linklistL;
L=(linklist)malloc(sizeof(node));
if(!
L)exit(0);
printf("pleaseinputthenumberofpeople:
");
scanf("%d",&number);
L=creatstart(L,number);
chulie(L,number);
}
voidjiemian()//约瑟夫问题的界面
{
printf("theproblemofpeopleourofcirclr\n");
printf("****************************************************\n");
printf("*1.solvebylianbiao;*\n");
printf("*2.solvebyshunxubiao;*\n");
printf("*3.help;*\n");
printf("*4.ruturntomenu;*\n");
printf("****************************************************\n");
}
voidshunxubiao()//用顺序表解决约瑟夫问题
{
inta[500]={0},i,code,number,shu;
printf("pleaseinputthenumberofpeople:
");
scanf("%d",&number);
shu=number;
for(i=1;i<=number;i++)
{
printf("pleaseinputthecodeofnumber%d:
",i);
scanf("%d",&a[i]);
}
printf("pleaseinputthestartcode:
");
scanf("%d",&code);
i=0;
printf("theturnoutofthecircleis:
");
while(shu>0)
{
while(code>0)
{
if(i+1>number)i=0;
if(a[i+1]!
=0)code--;
i++;
}
printf("%d",i);
code=a[i];
a[i]=0;
shu--;
}
printf("\n");
}
voidhelp()//帮助文档
{
printf("theproblemofpeopleourofcirclr\n");
printf("*******************************************************************************\n");
printf("*youcanchoise1,2,3or4togetthefunctionyouwant.ifyouchoise1,*\n");
printf("*youwillsolvetheproblembylianbiao.inthisway,whenapeopleoutof*\n");
printf("*thecircle,thenodewhichcontainhismessagewillbedeleted.ifyou*\n");
printf("*choise2,youwillsolvetheproblembyshunxubiao.inthisway,whena*\n");
printf("*peopleoutofthecircle,hiscodewillchangeinto0,buthismessage*\n");
printf("*willberemained.ifyouchoise4,endthegame.*\n");
printf("*******************************************************************************\n");
}
voidJoseph()//约瑟夫问题
{
intchoice;
jiemian();
printf("pleasemakeyourchoice:
");
scanf("%d",&choice);
while(choice!
=4)
{
switch(choice)
{
case1:
lianbiao();getch();getch();break;
case2:
shunxubiao();getch();getch();break;
case3:
system("cls");help();getch();getch();break;
default:
printf("pleasechooosetherightchoice!
");getch();getch();break;
}
system("cls");
jiemian();
printf("pleasemakeyourchoice:
");
scanf("%d",&choice);
}
}
voidinit()//八皇后问题棋盘初始化
{
inti,j;
for(i=1;i<=8;i++)
for(j=1;j<=8;j++)
a[i][j]='*';
}
voidshuchu()//输出可行解
{
inti,j;
for(i=1;i<=8;i++)
{
for(j=1;j<=8;j++)
printf("%c",a[i][j]);
printf("\n");
}
printf("\n");
}
voiddigui(inthang)//递归解决八皇后问题
{
intlie;
for(lie=1;lie<=8;lie++)
{
if(panduan(hang,lie))
{
a[hang][lie]='@';
if(hang!
=8)digui(hang+1);
else
{
shuchu();
total++;
}
}
a[hang][lie]='*';
}
}
intpanduan(inthang,intlie)//判断能否放置皇后
{
inti,j;
for(i=1;iif(a[i][lie]=='@')return0;
for(i=1;iif(a[hang][i]=='@')return0;
i=hang;
j=lie;
while((i-1>0)&&(j-1>0))
{
if(a[i-1][j-1]=='@')return0;
i--;
j--;
}
i=hang;
j=lie;
while((i-1>0)&&(j+1<=8))
{
if(a[i-1][j+1]=='@')return0;
i--;
j++;
}
return1;
}
voidQueen()//八皇后问题
{
digui
(1);
printf("thetotalpossibilityis:
%d\n",total);
}
voidwelcome()//主程序界面
{
printf("welcometooursystem\n");
printf("****************************************************\n");
printf("*1.JosephCentralissues*\n");
printf("*2.Queen8*\n");
printf("*3.exit*\n");
printf("****************************************************\n");
}
intmain()//主函数
{
intchoice;
welcome();
printf("pleasemakeyourchoice:
");
scanf("%d",&choice);
while(choice!
=3)
{
switch(choice)
{
case1:
system("cls");Joseph();getch();break;
case2:
system("cls");Queen();getch();getch();break;
default:
printf("pleasechooosetherightchoice!
");getch();getch();break;
}
system("cls");
welcome();
printf("pleasemakeyourchoice:
");
scanf("%d",&choice);
}
}
六、程序运行结果
七、使用说明
用户进入主界面后根据菜单内容选择1、2、3分别可进行约瑟夫问题、八皇后问题、退出系统操作。
进入约瑟夫问题后按照菜单选择1、2、3、4分别可进行链表求解、顺序表求解、查看帮助文档、回到主菜单操作。
进入八皇后问题系统即可输出所有的可能解,并计算解的数目。
八、心得体会
良好的界面不仅能反映出程序的主题结构,让用户了解系统的答大体功能,同时可以极大地方便用户随系统的了解与使用,增加用户与系统的交互性。