再来一个24点..... - -!!
/*****************************************************************
** HighlightCodeV3.0 software by yzfy(雨中飞燕) http:// **
*****************************************************************/
#include<cstdio>
#include<cstdlib>
#include<ctime>
void Calculate(float* rst,float num,char opt)
{
switch(opt)
{
case '+': *rst+=num;break;
case '-': *rst-=num;break;
case '*': *rst*=num;break;
case '/': if(num==0) return;
else *rst/=num;
break;
}
}
bool GetRst(float* num,char* opt__)
{
int map[3]={0},i=0;
char opt[4]={'+','-','*','/'};
float opt_a=num[0],opt_b=num[2];
for( ; ;opt_a=num[0],opt_b=num[2] )
{
Calculate(&opt_a,num[1],opt[map[0]]);
Calculate(&opt_b,num[3],opt[map[1]]);
if(Calculate(&opt_a,opt_b,opt[map[2]]),opt_a==24 )
{
opt__[0]=opt[map[0]];
opt__[1]=opt[map[1]];
opt__[2]=opt[map[2]];
return false;
}
if((opt[map[0]]=='/')&&(opt[map[1]]=='/')&&(opt[map[2]]=='/')) return true;
for(i=0;i<3&&++map[i]>3;++i) map[i]=0;
}
}
void GetSolve(float *num,char* opt__)
{
int i=0;
srand((unsigned int)time(0));
do
{
for(i=0;i<4;++i)
for(num[i]=(float)rand();num[i]<=0.f||num[i]>=14.f;num[i]=(float)rand());
}while(GetRst(num,opt__));
}
int main()
{
char ch='Y',opt[3],opt_c[3];
float sum_a=0.f,sum_b=0.f,num[4];
int i;
printf("============ 24点小游戏 ==============\n");
printf("输入会按如下格式处理: (( ) ( )) \n");
while(ch=='Y')
{
GetSolve(num,opt);
for(i=0;i<4;++i) printf("%.f ",num[i]);
scanf("\n%c,%c,%c",&opt_c[0],&opt_c[1],&opt_c[2]);
sum_a=num[0]; sum_b=num[2];
Calculate(&sum_a,(float)num[1],opt_c[0]);
Calculate(&sum_b,(float)num[3],opt_c[2]);
Calculate(&sum_a,sum_b,opt_c[1]);
if(sum_a==24)
{
printf("答对了!!!!!!!!\n");
}
else
{
printf("((%.f%c%.f)%c(%.f%c%.f))",num[0],opt_c[0],num[1],opt_c[1],num[2],opt_c[2],num[3]);
printf("错误!!!\n");
}
printf("答案: ");
printf("((%.f%c%.f)%c(%.f%c%.f))\n",num[0],opt[0],num[1],opt[2],num[2],opt[1],num[3]);
printf("是否继续游戏?(Y/N)");
scanf(" %c",&ch);
}
return 0;
}
** HighlightCodeV3.0 software by yzfy(雨中飞燕) http:// **
*****************************************************************/
#include<cstdio>
#include<cstdlib>
#include<ctime>
void Calculate(float* rst,float num,char opt)
{
switch(opt)
{
case '+': *rst+=num;break;
case '-': *rst-=num;break;
case '*': *rst*=num;break;
case '/': if(num==0) return;
else *rst/=num;
break;
}
}
bool GetRst(float* num,char* opt__)
{
int map[3]={0},i=0;
char opt[4]={'+','-','*','/'};
float opt_a=num[0],opt_b=num[2];
for( ; ;opt_a=num[0],opt_b=num[2] )
{
Calculate(&opt_a,num[1],opt[map[0]]);
Calculate(&opt_b,num[3],opt[map[1]]);
if(Calculate(&opt_a,opt_b,opt[map[2]]),opt_a==24 )
{
opt__[0]=opt[map[0]];
opt__[1]=opt[map[1]];
opt__[2]=opt[map[2]];
return false;
}
if((opt[map[0]]=='/')&&(opt[map[1]]=='/')&&(opt[map[2]]=='/')) return true;
for(i=0;i<3&&++map[i]>3;++i) map[i]=0;
}
}
void GetSolve(float *num,char* opt__)
{
int i=0;
srand((unsigned int)time(0));
do
{
for(i=0;i<4;++i)
for(num[i]=(float)rand();num[i]<=0.f||num[i]>=14.f;num[i]=(float)rand());
}while(GetRst(num,opt__));
}
int main()
{
char ch='Y',opt[3],opt_c[3];
float sum_a=0.f,sum_b=0.f,num[4];
int i;
printf("============ 24点小游戏 ==============\n");
printf("输入会按如下格式处理: (( ) ( )) \n");
while(ch=='Y')
{
GetSolve(num,opt);
for(i=0;i<4;++i) printf("%.f ",num[i]);
scanf("\n%c,%c,%c",&opt_c[0],&opt_c[1],&opt_c[2]);
sum_a=num[0]; sum_b=num[2];
Calculate(&sum_a,(float)num[1],opt_c[0]);
Calculate(&sum_b,(float)num[3],opt_c[2]);
Calculate(&sum_a,sum_b,opt_c[1]);
if(sum_a==24)
{
printf("答对了!!!!!!!!\n");
}
else
{
printf("((%.f%c%.f)%c(%.f%c%.f))",num[0],opt_c[0],num[1],opt_c[1],num[2],opt_c[2],num[3]);
printf("错误!!!\n");
}
printf("答案: ");
printf("((%.f%c%.f)%c(%.f%c%.f))\n",num[0],opt[0],num[1],opt[2],num[2],opt[1],num[3]);
printf("是否继续游戏?(Y/N)");
scanf(" %c",&ch);
}
return 0;
}