24点游戏数据结构.docx
《24点游戏数据结构.docx》由会员分享,可在线阅读,更多相关《24点游戏数据结构.docx(20页珍藏版)》请在冰点文库上搜索。
24点游戏数据结构
//project.cpp:
Definestheentrypointfortheconsoleapplication.
//
#include
#include
#include
#include
#include
/////////////
classmemory
{
public:
intscore;
charname[10];
memory*link;
};
/////////////
classmemory_star
{
public:
voidenter(intscore,charname[]);
voidfirst(){head=newmemory;head->link=NULL;}
voidmemory_work();
private:
memory*head;
};
/////////////
voidmemory_star:
:
enter(intscore,charname[])
{
memory*p,*temp;
p=head;
while(p->link!
=NULL)
{
if(p->link->score>score)
p=p->link;
}
temp=newmemory;
strcpy(temp->name,name);
temp->score=score;
temp->link=p->link;
p->link=temp;
}
voidmemory_star:
:
memory_work()
{
inti=1;
cout<<"军功榜"<memory*p=head->link;
while(p!
=NULL)
{
cout<<"第"<name<<"分数"<score<i++;
p=p->link;
}
cout<<"按任意键结束:
"<getchar();
cout<}
///////////////////////////
classArithmetic
{
public:
voidgame_create();
intprior(charch,charch1);
intcompare(charch);
floataction(char*ch);
floatma(charch);
floatopmath(charop,floata,floatb);
//voidstar();
floatdata[4];
charsign[3];
};
/////////////////////////////
classMenu
{
public:
voidMain_Menu();
voidsecond_Menu();
voidfirst_Menu();
voidSet_Menu();
voidStar();
voidCompete_Game();
voidmath();
private:
charname[20];
charname2[20];
intTime;//游戏时间
inthard;
intselect;
intscore,score2;//记录分数
memory_starmemory;
};
////////////
voidMenu:
:
Set_Menu()
{
cout<cout<cout<cout<cout<<"1-游戏难度设置"<cout<<"2-设置背景颜色"<cout<<"3-游戏时间设置"<cout<<"4-其他键跳出"<cout<<"";
cin>>select;
if(select==1)
{
cout<<"1-简单"<cout<<"2-普通"<cout<<"3-困难"<cout<<"4-其他键跳出"<cout<<"";
cin>>hard;
}
if(select==2)
{
}
if(select==3)
{
}
}
voidMenu:
:
Main_Menu()
{
cout<cout<cout<cout<cout<cout<cout<cout<cout<<"STARGAME"<cout<<"<请输任意键开始>"<getchar();
first_Menu();
second_Menu();
}
voidMenu:
:
first_Menu()
{
cout<cout<cout<cout<cout<cout<<"*******************"<cout<<"*****************"<cout<<"********************"<cout<<"*********"<cout<<"*****************"<cout<<"*********"<cout<<"******************"<cout<<"**********************************"<cout<<"************"<cout<<"***************"<cout<<"***************************"<cout<cout<cout<cout<cout<cout<cout<cout<<"请输入游戏名:
";
cin>>name;
}
voidMenu:
:
second_Menu()
{
select=0;
memory.first();
while(select!
=6)
{
cout<cout<cout<cout<cout<<"1-普通模式"<cout<<"2-比赛模式"<cout<<"3-天梯模式"<cout<<"4-游戏设置"<cout<<"5-游戏排行"<cout<<"6-退出"<cout<<"";
cin>>select;
switch(select)
{
case1:
Star();break;
case2:
Compete_Game();break;
case3:
math();break;
case4:
Set_Menu();break;
case5:
memory.memory_work();break;
}
}
}
voidMenu:
:
math()
{
Arithmeticstar;
inti=1;//题数
score=0;
charstring[10];
cout<<"游戏开始"<cout<<"模式:
普通模式"<while(true)
{
cout<star.game_create();
cout<<"第"<
cout<cout<<"请回答:
";
cin>>string;
i++;
if(star.action(string)==24)
{
score+=10;
cout<<"真聪明,答对了"<}
else
{
cout<<"sorry错了"<cout<<"正确答案:
"<break;
}
}
cout<<"最终得分"<memory.enter(score,name);
}
voidMenu:
:
Star()
{
Arithmeticstar;
inti=1,j;//题数
score=0;
charadjuct='y';
charstring[10];
cout<<"游戏开始"<cout<<"模式:
普通模式"<while(adjuct!
='n'&&adjuct!
='N')
{
cout<star.game_create();
cout<<"第"<
cout<cout<<"请回答:
";
cin>>string;
i++;
j=star.action(string);
if(j==24)
{
score+=10;
cout<<"真聪明,答对了"<}
else
{
cout<<"sorry错了"<cout<<"您输入的结果为"<cout<<"正确答案:
"<}
adjuct=getchar();
}
cout<<"最终得分"<}
voidMenu:
:
Compete_Game()
{
Arithmeticstar;
charstring[10];
cout<<"游戏开始"<cout<<"模式:
比赛模式"<cout<cout<<"请输入第二玩家游戏名:
";
cin>>name2;
score=score2=0;
inti=1;//题数
intsum;
cout<cin>>sum;
while(i<=sum)
{
cout<<"--------------------------------------------------------"<cout<<"第"<
star.game_create();
cout<<"玩家1"<cout<cout<<"请回答:
";
cin>>string;
if(star.action(string)==24)score+=10;
cout<cout<<"玩家2"<star.game_create();
cout<cout<<"请回答:
";
cin>>string;
if(star.action(string)==24)score2+=10;
i++;
}
cout<cout<<"--------------------------------------------------------"<if(score>score2)cout<<"恭喜玩家1,获得了游戏的胜利"<elseif(scoreelsecout<<"旗鼓相当,平手"<}
///////////
classWorry
{
};
////////////////////////
classmath//公式数据暂存
{
public:
math(){maxsize=10;top=-1;data=newfloat[maxsize];}
boolIsEmpty()const{if(top==-1)returntrue;returnfalse;}
boolIsFull()const{if(top==maxsize-1)returntrue;returnfalse;}
floatpop(){returndata[top--];}
voidpush(floatch){data[++top]=ch;}
floatgetTop(){returndata[top];}
private:
float*data;
inttop;
intmaxsize;
};
/////////////////////////
classop//公式符号暂存
{
public:
op(){maxsize=10;top=-1;opt=newchar[maxsize];}
boolIsEmpty()const{if(top==-1)returntrue;returnfalse;}
boolIsFull()const{if(top==maxsize-1)returntrue;returnfalse;}
charpop(){returnopt[top--];}
voidpush(charch){opt[++top]=ch;}
chargetTop(){returnopt[top];}
private:
inttop;
intmaxsize;
char*opt;
};
///////////////////////
floatArithmetic:
:
ma(charch)
{
switch(ch)
{
case'0':
return0;
case'1':
return1;
case'2':
return2;
case'3':
return3;
case'4':
return4;
case'5':
return5;
case'6':
return6;
case'7':
return7;
case'8':
return8;
case'9':
return9;
}
}
intArithmetic:
:
prior(charisp,charicp)
{
intis,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(isif(is>ic)return-1;
return0;
}
floatArithmetic:
:
action(char*st)
{
mathd;
floatrembermath=0;/////////////////////////////////////////////////////////////
intj=strlen(st);
st[j]='#';
st[j+1]='^';
opopt;
charch='#',ch1;
opt.push(ch);
boolflag=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++;
}
elseif(prior(opt.getTop(),*st)==-1)
{
d.push(opmath(opt.pop(),d.pop(),d.pop()));
}
else
{
ch1=opt.pop();
if(ch1=='(')st++;
}
}
}
returnd.pop();
}
voidArithmetic:
:
game_create()
{
booladjuct=false;
inta,b,c,d;
inte,f,g;
charop[5]="+-*/";
floatmath[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];
}
}
}
}
}
}
}
intArithmetic:
:
compare(charch)
{
if(ch>='0'&&ch<='9')
return0;
elseif(ch>='a'&&ch<='z')
return1;
elsereturn-1;
}
floatArithmetic:
:
opmath(charop,floata,floatb)
{
switch(op)
{
case'+':
return(a+b);
case'-':
return(a-b);
case'*':
return(a*b);
case'/':
return(a/b);
}
}
/////////////////////
voidmain()
{
Menua;
a.Main_Menu();
}