《数据结构》课程设计试验报告
- 格式:doc
- 大小:196.00 KB
- 文档页数:11
《数据结构》课程设计 实验报告
题 目 约瑟夫问题和八皇后问题求解 学 院 数学与信息工程学院 专 业 计算机科学与技术 班 级 计科081 学 号 200853225142 学生姓名 陈哲 同组成员 魏艳舞 杨青 施晓洁 吴亚君 徐珍艳 张素姿 指导教师 刘小晶 编写日期 2009年6月28日 1
一、问题描述 1、约瑟夫问题描述 编号为1,2… n的n个人按顺时针方向围坐一圈,每人持有一个密码(正整数)。一开始任选一个正整数作为报数的上限值m,从第一个人开始按顺时针方向自1开始顺序报数,报到m时停止报数,报m的人出列,将他的密码作为新的m值,从他的顺时针方向上的下一个开始重新从1报数,如此下去,直至所有人全部出列为止,设计一个程序求出出列顺序。
2、八皇后问题描述 在一个8×8的棋盘里放置8个皇后,要求每个皇后两两之间不相"冲"(在每一横列竖列斜列只有一个皇后)。
3、界面设计模块问题描述 设计一个菜单式界面,让用户可以选择要解决的问题,同时可以退出程序。界面要求简洁明了,大方得体,便于用户的使用,同时,对于用户的错误选择可以进行有效的处理。
二、问题分析 本人负责的是为用户设计菜单界面,使用户可以根据菜单进行选择其中的某个问题进行处理。对于一个菜单界面,首先要求界面简单明了,使得用户可以轻松通过界面知道如何获取自己想要的操作。其次,我们不能保证用户每次的选择都是有效的,即用户的选择是在我们提供的服务范围之内,所以要设计容错操作,即当用户的选择超出我们提供的范围时,提示用户重新选择。最后,要保证用户选择相应的操作后,程序能正确的按照用户的选择运行下去,完成用户的要求。
三、数据结构描述 Int choice; 记录用户的选择,然后选择相应的操作。
typedef struct LNode{ int num; int code; struct LNode *next; }node,*linklist; 单链表解决约瑟夫问题时储存结点信息
char a[9][9]; 八皇后问题中记录棋盘信息
四、算法设计 2
1、程序层次模块图 程序总层次模块图 2、算法设计 void jiemian() { printf(" the problem of people our of circlr\n"); printf(" ****************************************************\n"); printf(" * 1.solve by lianbiao; *\n"); printf(" * 2.solve by shunxubiao; *\n"); printf(" * 3.help; *\n"); printf(" * 4.ruturn to menu; *\n");
约瑟夫问题 八皇后问题 用链表解决 用顺序表解决 查看帮助 回到主菜单 主菜单 结束 建立顺序表并出列 建立单向循环链表 在单向链表中出列
棋盘初始化 判断能否安放皇后 递归安放
8皇
后 3
printf(" ****************************************************\n"); } void help() { printf(" the problem of people our of circlr\n"); printf("*******************************************************************************\n"); printf("* you can choise 1,2,3 or 4 to get the function you want.if you choise 1, *\n"); printf("* you will solve the problem by lianbiao.in this way,when a people out of *\n"); printf("* the circle,the node which contain his message will be deleted.if you *\n"); printf("* choise 2,you will solve the problem by shunxubiao.in this way,when a *\n"); printf("* people out of the circle,his code will change into 0,but his message *\n"); printf("* will be remained.if you choise 4,end the game. *\n"); printf("*******************************************************************************\n"); }
void welcome() { printf(" welcome to our system\n"); printf(" ****************************************************\n"); printf(" * 1.Joseph Central issues *\n"); printf(" * 2.Queen 8 *\n"); printf(" * 3.exit *\n"); printf(" ****************************************************\n");
} int main() { int choice; welcome(); printf(" please make your choice:"); scanf("%d",&choice); while(choice!=3) { switch(choice) { case 1:system("cls");Joseph();getch();break; case 2:system("cls");Queen();getch();getch();break; default:printf(" please chooose the right choice!");getch();getch();break; } system("cls"); 4
welcome(); printf(" please make your choice:"); scanf("%d",&choice); } }
五、详细程序清单 #include #include typedef struct LNode{ int num; int code; struct LNode *next; }node,*linklist; //数据结构声明
char a[9][9]; int total;
linklist creatstart(linklist L,int number) //建立单向循环链表 { int m,i; linklist s,p; s=L; for(i=1;i<=number;i++) { p=(linklist)malloc(sizeof(node)); if(!p) exit(0); p->num=i; printf("please input the code of number %d:",i); scanf("%d",&p->code); p->next=NULL; s->next=p; s=p; } s->next=L->next; return s; }
void chulie(linklist L,int number) //链表中的出列 { int turn,i,j; linklist p,s; printf("please input the start code:"); 5
scanf("%d",&turn); p=L; printf("the turn out of the circle is:"); 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"); }
void lianbiao() //用单向循环链表解决约瑟夫问题 { int number; linklist L; L=(linklist)malloc(sizeof(node)); if(!L) exit(0); printf("please input the number of people:"); scanf("%d",&number); L=creatstart(L,number); chulie(L,number); }
void jiemian() //约瑟夫问题的界面 { printf(" the problem of people our of circlr\n"); printf(" ****************************************************\n"); printf(" * 1.solve by lianbiao; *\n"); printf(" * 2.solve by shunxubiao; *\n"); printf(" * 3.help; *\n"); printf(" * 4.ruturn to menu; *\n"); printf(" ****************************************************\n");
} void shunxubiao() //用顺序表解决约瑟夫问题 { int a[500]={0},i,code,number,shu; printf("please input the number of people:");