数据结构课程设计-24点游戏

  • 格式:doc
  • 大小:349.66 KB
  • 文档页数:29

设计任务书注意:1.任务书格式参照“任务书范例”执行。

2.范例中的红色..文字应根据你所选择的具体课题,修改为对应的内容。

范例中的其它内容不变。

【摘要】80 年代全世界流行一种数字游戏,在中国我们把这种游戏称为“24 点”。

用给出4个数,实现24 点,就是从数中中任意抽出四张(数字表示为1-13),用加、减、乘、除的方法使结果成为24。

程序要实现由系统随机产生四个数,玩家输入表达式,然后判断表达式是否合法且等于24。

如果玩家认为这四张牌算不出24 点(如:1,1,1,1),可只输入?,程序将判断这四张牌是否能得出24 点,如果能,则程序将给出算式,如果不能则做出说明。

【关键词】24点,随机产生数,分数记录目录1需求分析 (5)1.1 程序的功能 (5)1.2 输入输出要求…….........…………………………………………………52概要设计.......………………………………………………………..52.1模块分析........………………………………………………………..52.2程序设计组成框图 (6)2.3根据对 24 点游戏的模块分析..……………………………………………62.4游戏算法整个流程图 (6)3详细设计和实现 (7)4调试与操作说明…….........………………………………………………..20总结…….........……………………………………………………..26致谢…….........……………………………………………………..27参考文献 (28)1.需求分析1.1程序的功能程序运行后产生 4 个数,其中每个数字只能使用一次;任意使用 + - * / ( ) ,构造出一个表达式,使得最终结果为 24,这就是常见的算 24 点的游戏。

在输入表达式时程序会自动检测用户输入的数据是否为程序产生的那4个整数,如果不是就提醒用户输入有误,重新输入或退出程序。

有的时候程序随机产生的扑克牌对应的整数可能不能算成 24,如产生了 4个1,这个时候用户只要点击无解程序就会自动退出了。

1.2输入输出的要求输入的运算符只能是+、-、*、/、)这六个,输入的整数必须是程序随机(、产生的那四个数,而且每个数只能用一次。

当用户正确输入算式后,用“堆栈来求表达式的值” 的原理,求出结果并判断是否为 24,得出用户是输是赢的结果。

输出结果后,询问用户是否继续,是则重新启动程序,否则结束程序。

2.概要设计2.1 模块分析24 算法游戏的功能模块不多,也比较容易实现。

基本思想就是由程序先随机产生 4 张扑克牌,每张扑克牌对应一个整数,所以首先要有一个随机产生扑克牌的模块。

接下来就是用户用程序产生的扑克牌来算 24 点了,所以要有一个输入用户去接表达式的模块。

用户输入完成后,判断输入表达式是否合法,所以要有一个判断表达式合法与否的模块。

如果表达式合法程序就开始计算表达式了,所以要有一个计算表达式的模块。

程序计算完表达式后就要判断用户输入的表达式是不是为 24 点了,所以还有一个判断表达式正误的模块。

这 5 个模块是逐步递进的,是要前一个模块的功能完成后,才能执行后一个模块,否则程序就将出错。

程序所涉及的数据结构主要为整形和字符型,整形数据结构主要是用来计算表达式的,字符型数据结构主要是用来输入表达式的。

程序所涉及的数据库结构主要有数组和栈,数组在计算表达式和产生扑克牌是都要用到,栈只是用来计算表达式的。

2.2程序设计组成框图2.3根据对 24 点游戏的模块分析2.4游戏算法整个流程图3.详细设计和实现// project.cpp : Defines the entry point for the console application.//#include<stdio.h>#include<iostream.h>#include<stdlib.h>#include<string.h>#include<time.h>/////////////class memory{public :int score;char name[10];memory *link;};/////////////class memory_starpublic:void enter(int score,char name[]);void first(){head=new memory;head->link=NULL;}void memory_work();private:memory *head;};/////////////void memory_star::enter(int score,char name[] ){memory *p,*temp;p=head;while(p->link!=NULL){if(p->link->score>score)p=p->link;}temp=new memory;strcpy(temp->name,name);temp->score=score;temp->link=p->link;p->link=temp;}void memory_star::memory_work(){int i=1;cout<<" 军功榜"<<endl;memory *p=head->link;while(p!=NULL){cout<<" 第"<<i<<"名"<<"姓名"<<p->name<<" 分数"<<p->score<<endl;i++;p=p->link;}cout<<" 按任意键结束:"<<endl;getchar();cout<<endl;}///////////////////////////class Arithmetic{public:void game_create();int prior(char ch,char ch1);int compare (char ch);float action(char *ch);float ma(char ch);float opmath(char op,float a,float b); // void star();float data[4];char sign[3];};/////////////////////////////class Menu{public:void Main_Menu();void second_Menu();void first_Menu();void Set_Menu();void Star();void Compete_Game();void math();private :char name[20];char name2[20];int Time;//游戏时间int hard;int select;int score,score2;//记录分数memory_star memory;};////////////void Menu::Set_Menu(){cout<<endl;cout<<endl;cout<<endl;cout<<" 1-游戏难度设置"<<endl;cout<<" 2-设置背景颜色"<<endl;cout<<" 3-游戏时间设置"<<endl;cout<<" 4-其他键跳出"<<endl;cout<<" ";cin>>select;if(select==1){cout<<" 1-简单"<<endl;cout<<" 2-普通"<<endl;cout<<" 3-困难"<<endl;cout<<" 4-其他键跳出"<<endl;cout<<" ";cin>>hard;}if(select==2){}if(select==3){}}void Menu::Main_Menu(){cout<<endl;cout<<endl;cout<<endl;cout<<endl;cout<<endl;cout<<endl;cout<<endl;cout<<" STAR GAME "<<endl;cout<<" <请输任意键开始>"<<endl;getchar();first_Menu();second_Menu();}void Menu::first_Menu(){cout<<endl;cout<<endl;cout<<endl;cout<<endl;cout<<endl;cout<<" ******* *** ***** **** "<<endl;cout<<" **** ******* ** ** ** "<<endl;cout<<" **** *** *** ***** ***** "<<endl;cout<<" *** *** *** "<<endl;cout<<" ** *** *** ********* "<<endl;cout<<" ** *** *** * "<<endl;cout<<" ** *************** * "<<endl;cout<<" ** ****** ***************** ********* "<<endl;cout<<" *** ** *** *** * "<<endl;cout<<" ****** ** *** *** * "<<endl;cout<<" ************ *** *** *********"<<endl;cout<<endl;cout<<endl;cout<<endl;cout<<endl;cout<<endl;cout<<endl;cout<<endl;cout<<" 请输入游戏名:";cin>>name;}void Menu::second_Menu(){select=0;memory.first();while(select!=6){cout<<endl;cout<<endl;cout<<endl;cout<<endl;cout<<" 1-普通模式"<<endl;cout<<" 2-比赛模式"<<endl;cout<<" 3-天梯模式"<<endl;cout<<" 4-游戏设置"<<endl;cout<<" 5-游戏排行"<<endl;cout<<" 6-退出"<<endl;cout<<" ";cin>>select;switch(select){case 1:Star();break;case 2:Compete_Game();break;case 3:math();break;case 4:Set_Menu();break;case 5: memory.memory_work();break;}}}void Menu::math(){Arithmetic star;int i=1;//题数score=0;char string[10];cout<<" 游戏开始"<<endl;cout<<"模式:普通模式"<<endl<<"按‘N’键结束其他键继续"<<endl<<endl<<endl;while(true){cout<<endl<<endl<<endl;star.game_create();cout<<"第"<<i<<"题"<<endl;cout<<star.data[0]<<" "<<star.data[1]<<" "<<star.data[2]<<" "<<star.data[3]<<endl;cout<<"请回答:";cin>>string;i++;if(star.action(string)==24){score+=10;cout<<"真聪明,答对了"<<endl;}else{cout<<"sorry 错了"<<endl;cout<<"正确答案:"<<star.sign[0]<<" "<<star.sign[1]<<" "<<star.sign[2]<<endl;break;}}cout<<" 最终得分"<<score<<endl;memory.enter(score,name);}void Menu::Star(){Arithmetic star;int i=1,j;//题数score=0;char adjuct='y';char string[10];cout<<" 游戏开始"<<endl;cout<<"模式:普通模式"<<endl<<"按‘N’键结束其他键继续"<<endl<<endl<<endl;while(adjuct!='n'&&adjuct!='N'){cout<<endl<<endl<<endl;star.game_create();cout<<"第"<<i<<"题"<<endl;cout<<star.data[0]<<" "<<star.data[1]<<" "<<star.data[2]<<" "<<star.data[3]<<endl;cout<<"请回答:";cin>>string;i++;j=star.action(string);if(j==24){score+=10;cout<<"真聪明,答对了"<<endl;}else{cout<<"sorry 错了"<<endl;cout<<"您输入的结果为"<<j<<endl;cout<<"正确答案:"<<star.sign[0]<<" "<<star.sign[1]<<" "<<star.sign[2]<<endl;}adjuct=getchar();}cout<<" 最终得分"<<score<<endl;}void Menu::Compete_Game(){Arithmetic star;char string[10];cout<<" 游戏开始"<<endl;cout<<"模式:比赛模式"<<endl<<"按‘N’键结束其他键继续"<<endl<<endl<<endl;cout<<endl;cout<<" 请输入第二玩家游戏名:";cin>>name2;score=score2=0;int i=1;//题数int sum;cout<<endl<<endl<<"请输入题目数量";cin>>sum;while(i<=sum){cout<<"--------------------------------------------------------"<<e ndl;cout<<"第"<<i<<"轮"<<endl;star.game_create();cout<<"玩家1"<<endl;cout<<star.data[0]<<" "<<star.data[1]<<" "<<star.data[2]<<" "<<star.data[3]<<endl;cout<<"请回答:";cin>>string;if(star.action(string)==24)score+=10;cout<<endl<<endl;cout<<"玩家2"<<endl;star.game_create();cout<<star.data[0]<<" "<<star.data[1]<<" "<<star.data[2]<<" "<<star.data[3]<<endl;cout<<"请回答:";cin>>string;if(star.action(string)==24)score2+=10;i++;}cout<<endl<<endl<<endl;cout<<"--------------------------------------------------------"<<endl;if(score>score2)cout<<"恭喜玩家1,获得了游戏的胜利"<<endl;else if(score<score2)cout<<"恭喜玩家2,获得了游戏的胜利"<<endl;else cout<<"旗鼓相当,平手"<<endl;}///////////class Worry{};////////////////////////class math //公式数据暂存{public :math(){maxsize=10;top=-1;data=new float[maxsize]; } bool IsEmpty() const{if(top==-1)return true;return false;} bool IsFull() const{if(top==maxsize-1)return true;return false;}float pop(){return data[top--];}void push(float ch){data[++top]=ch;}float getTop(){return data[top];}private:float * data;int top;int maxsize;};/////////////////////////class op //公式符号暂存{public :op() {maxsize=10; top=-1; opt=new char[maxsize]; } bool IsEmpty() const{if(top==-1)return true;return false;} bool IsFull() const{if(top==maxsize-1)return true;return false;}char pop(){return opt[top--];}void push(char ch){opt[++top]=ch;}char getTop(){return opt[top];}private:int top;int maxsize;char *opt;};///////////////////////float Arithmetic::ma(char ch){switch (ch){case '0':return 0;case '1':return 1;case '2':return 2;case '3':return 3;case '4':return 4;case '5':return 5;case '6':return 6;case '7':return 7;case '8':return 8;case '9':return 9;}}int Arithmetic::prior(char isp,char icp){int is,ic;switch(isp){case '+': is=3;break;case '-': is=3;break;case '*': is=5;break;case '/': is=5;break;case '(': is=1;break;case ')': is=6;break;case '#': is=0;break;}switch(icp){case '+': ic=2;break;case '-': ic=2;break;case '*': ic=4;break;case '/': ic=4;break;case '(': ic=6;break;case ')': ic=1;break;case '#': ic=0;break;}if(is<ic)return 1;if(is>ic)return -1;return 0;}float Arithmetic::action(char *st){math d;floatrembermath=0;////////////////////////////////////////////////////////// ///int j=strlen(st);st[j]='#';st[j+1]='^';op opt;char ch='#',ch1;opt.push(ch);bool flag=false;while(opt.IsEmpty()==false&&*st!='^'){if(compare(*st)==0){rembermath=rembermath*10+ma(*st);st++;flag=false;}else{if(flag==false){d.push(rembermath);rembermath=0;flag=true;}if(prior(opt.getTop(),*st)==1){opt.push(*st);st++;}else if(prior(opt.getTop(),*st)==-1) {d.push(opmath(opt.pop(),d.pop(),d.pop()));}else{ch1=opt.pop();if(ch1=='(')st++;}}}return d.pop();}void Arithmetic::game_create(){bool adjuct=false;int a,b,c,d;int e,f,g;char op[5]="+-*/";float math[4],sum;srand((unsigned)time(NULL));for(;!adjuct;){a=rand()%25;b=rand()%25;c=rand()%25;d=rand()%25;math[0]=(float)a;math[1]=(float)b;math[2]=(float)c;math[3]=(float)d;for(a=0;a<4&&!adjuct;a++){for(b=0;b<4&&!adjuct;b++){if(a==b)continue;for(c=0;c<4&&!adjuct;c++){if(a==c||c==b)continue;d=6-a-b-c;for(e=0;e<4&&!adjuct;e++)for(g=0;g<4&&!adjuct;g++)for(f=0;f<4&&!adjuct;f++){if(math[b]==0&&e==3)continue;sum=opmath(op[e],math[a],math[b]);if(math[c]==0&&g==3)continue;sum=opmath(op[g],sum,math[c]);if(math[d]==0&&f==3)continue;sum=opmath(op[f],sum,math[d]);if(sum==24){adjuct=true;data[0]=math[a];data[1]=math[b];data[2]=math[c];data[3]=math[d];sign[0]=op[e];sign[1]=op[g];sign[2]=op[f];}}}}}}}int Arithmetic:: compare (char ch){if(ch>='0'&&ch<='9')return 0;else if(ch>='a'&&ch<='z')return 1;else return -1;}float Arithmetic::opmath(char op,float a,float b){switch(op){case '+':return(a+b);case '-':return(a-b);case '*':return (a*b);case '/':return(a/b);}}/////////////////////void main(){Menu a;a.Main_Menu();}4.调试与操作说明首先,运行程序,进入程序主界面输入玩家姓名进入程序,选项界面开始24点游戏,系统产生四个数,提示玩家输入表达式。