二十四点小游戏——C语言实现

二十四点小游戏(C语言实现)

过程笨拙,有很多局限之处,比如对答案的输出有限制,代码行数过多,故仅供思路参考。

#include<stdio.h>
#include<windows.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>

int l=0,t=0;				//定义全局变量,接受答案判断 

float cal(float x,float y,char ope)	   
	//calculate:计算,对两个数进行四则运算,两个数的左右顺序不能互换 
{
	if(ope=='+')
		return x+y;
	else if(ope=='-')
		return x-y;
	else if(ope=='*')
	 	return x*y;
	else if(ope=='/')
		return x/y;
}		//接收两个整数和一个运算符,判断运算符号,然后进行计算 
float exp1(float a,float b,float c,float d,char ope1,char ope2,char ope3)      
	//expression:表达式。exp-1用于计算(( a b ) c) d 表达式(略去三个运算符号) 
{
	float r1,r2,r3;
	r1=cal(a,b,ope1);		//1.计算a和b的运算结果 
	r2=cal(r1,c,ope2);		//2.计算a和b运算结果和c的运算结果 
	r3=cal(r2,d,ope3);		//3.最后与d进行最后一步运算
	 
	return r3;				//result:结果 
} 
float exp2(float a,float b,float c, float d,char ope1,char ope2,char ope3)	
	//exp-2用于计算( a b )( c d )表达式 
{
	float r1,r2,r3;
	r1=cal(a,b,ope1);
	r2=cal(c,d,ope3);
	r3=cal(r1,r2,ope2); 
	
	return r3;
} 
float exp3(float a,float b,float c,float d, char ope1,char ope2,char ope3)	
	//exp-3用于计算 ( a ( b c ) ) d 表达式 
{
	float r1,r2,r3;
	r1=cal(b,c,ope2);
	r2=cal(a,r1,ope1);
	r3=cal(r2,d,ope3);
	
	return r3;
}
float exp4(float a,float b,float c,float d,char ope1,char ope2,char ope3)  
	//exp-4用于计算  a ( ( b c ) d) 表达式 
{
	float r1,r2,r3;
	r1=cal(b,c,ope2);
	r2=cal(r1,d,ope3);
	r3=cal(a,r2,ope1);
	
	return r3;
}
float exp5(float a,float b, float c, float d,char ope1,char ope2,char ope3)		
	//exp-5用于计算 a ( b ( c d ) )表达式 
{
	float r1,r2,r3;
	r1=cal(c,d,ope3);
	r2=cal(b,r1,ope2);
	r3=cal(a,r2,ope1);
	
	return r3; 
}
void p(int a,int b,int c,int d,float e,float f,float g,float h,char ans[])		//printf所有的正确答案 
{
	extern int l,t; /*在p函数中对全局变量l,t,进行说明,将作用域扩展到p函数内部。l判断答案对不对*//*t判断有无答案*/
	
	char ope[4]={'+','-','*','/'};  //operator:运算符
	int i,j,k;
	char o1,o2,o3;	
		for(i=0;i<4;i++)
		{
			o1=ope[i];
			for(j=0;j<4;j++)
			{
				o2=ope[j];
				for(k=0;k<4;k++)
				{
					o3=ope[k];
					if(exp1(e,f,g,h,o1,o2,o3)==+24.0)
					{	printf("\t((%d%c%d)%c%d)%c%d=24\n",a,o1,b,o2,c,o3,d);//exp-1用于计算(( a b ) c) d 表达式
						++t;													
						char cha0[16];			
						char cha1[3]="(";						 
						char cha2[3]="(";
						char cha3[3];
						char cha4[3];					
						char cha5[3];
						char cha6[3]=")";
						char cha7[3];
						char cha8[3];
						char cha9[3]=")";
						char cha10[3];
						char cha11[3];   //character:字符                                          												
						sprintf(cha3,"%d", a);		//将a转为字符串输入到 cha中	
						cha4[0]=o1;
						cha4[1]='\0';			
						sprintf(cha5,"%d", b);						
						cha7[0]=o2;
						cha7[1]='\0';
						sprintf(cha8,"%d", c);						
						cha10[0]=o3;
						cha10[1]='\0';
						sprintf(cha11,"%d", d);
						cha0[0]='\0';
						strcat(cha0,cha1);
						strcat(cha0,cha2);
						strcat(cha0,cha3);
						strcat(cha0,cha4);
						strcat(cha0,cha5);
						strcat(cha0,cha6);
						strcat(cha0,cha7);
						strcat(cha0,cha8);
						strcat(cha0,cha9);
						strcat(cha0,cha10);
						strcat(cha0,cha11);						
						if(strcmp(cha0,ans)==0)		//answer:答案(客户输入的答案)。	
							++l;					//l用于判断答案正误 
									 
					}	 				 
					
																														
					if(exp2(e,f,g,h,o1,o2,o3)==+24.0)					
					{	printf("\t(%d%c%d)%c(%d%c%d)=24\n",a,o1,b,o2,c,o3,d);//exp-2用于计算( a b )( c d )表达式
						++t;					
						char chb0[16];
						char chb1[3]="(";
						char chb2[3];
						char chb3[3];
						char chb4[3];
						char chb5[3]=")";
						char chb6[3];
						char chb7[3]="(";
						char chb8[3];
						char chb9[3];
						char chb10[3];
						char chb11[3]=")"; 						
						sprintf(chb2,"%d", a);
						chb3[0]=o1;
						chb3[1]='\0';
						sprintf(chb4,"%d", b);						
						chb6[0]=o2;
						chb6[1]='\0';						
						sprintf(chb8,"%d", c);
						chb9[0]=o3;
						chb9[1]='\0';
						sprintf(chb10,"%d", d);
						chb0[0]='\0';						
						strcat(chb0,chb1);		//连接字符串,获取正确答案完整字符串 
						strcat(chb0,chb2);
						strcat(chb0,chb3);
						strcat(chb0,chb4);
						strcat(chb0,chb5);
						strcat(chb0,chb6);
						strcat(chb0,chb7);
						strcat(chb0,chb8);
						strcat(chb0,chb9);
						strcat(chb0,chb10);
						strcat(chb0,chb11);						
						if(strcmp(chb0,ans)==0)		//answer:答案(客户输入的答案)。	
						++l;				//l用于判断答案正误 
									 
					}
						
											
					if(exp3(e,f,g,h,o1,o2,o3)==+24.0)							
					{	printf("\t(%d%c(%d%c%d))%c%d=24\n",a,o1,b,o2,c,o3,d);//exp-3用于计算 ( a ( b c ) ) d 表达式	
						++t;												
						char chc0[16];
						char chc1[3]="(";
						char chc2[3];
						char chc3[3];
						char chc4[3]="(";
						char chc5[3];
						char chc6[3];
						char chc7[3];
						char chc8[3]=")";
						char chc9[3]=")";
						char chc10[3];
						char chc11[3];						
						sprintf(chc2,"%d", a);
						chc3[0]=o1;
						chc3[1]='\0';						
						sprintf(chc5,"%d", b);
						chc6[0]=o2;
						chc6[1]='\0';
						sprintf(chc7,"%d", c);												
						chc10[0]=o3;
						chc10[1]='\0';
						sprintf(chc11,"%d", d);
						chc0[0]='\0';
						strcat(chc0,chc1);
						strcat(chc0,chc2);
						strcat(chc0,chc3);
						strcat(chc0,chc4);
						strcat(chc0,chc5);
						strcat(chc0,chc6);
						strcat(chc0,chc7);
						strcat(chc0,chc8);
						strcat(chc0,chc9);
						strcat(chc0,chc10);
						strcat(chc0,chc11);						
						if(strcmp(chc0,ans)==0)		//answer:答案(客户输入的答案)。	
						++l;					//l用于判断答案正误 									 
					}
						
						
						
						
											
					if(exp4(e,f,g,h,o1,o2,o3)==+24.0)										
					{	printf("\t%d%c((%d%c%d)%c%d)=24\n",a,o1,b,o2,c,o3,d);//exp-4用于计算  a ( ( b c ) d) 表达式 
						++t;												
						char chd0[16];
						char chd1[3];
						char chd2[3];
						char chd3[3]="(";
						char chd4[3]="(";
						char chd5[3];
						char chd6[3];
						char chd7[3];
						char chd8[3]=")";
						char chd9[3];
						char chd10[3];
						char chd11[3]=")";
						chd0[0]='\0';
						sprintf(chd1,"%d", a);
						chd2[0]=o1;
						chd2[1]='\0';												
						sprintf(chd5,"%d", b);
						chd6[0]=o2;
						chd6[1]='\0';
						sprintf(chd7,"%d", c);						
						chd9[0]=o3;
						chd9[1]='\0';
						sprintf(chd10,"%d", d);						
						strcat(chd0,chd1);
						strcat(chd0,chd2);
						strcat(chd0,chd3);
						strcat(chd0,chd4);
						strcat(chd0,chd5);
						strcat(chd0,chd6);
						strcat(chd0,chd7);
						strcat(chd0,chd8);
						strcat(chd0,chd9);
						strcat(chd0,chd10);
						strcat(chd0,chd11);						
						if(strcmp(chd0,ans)==0)		//answer:答案(客户输入的答案)。	
						++l;					//l用于判断答案正误 									 
					}
			
						
												
					if(exp5(e,f,g,h,o1,o2,o3)==+24.0)					
					{	printf("\t%d%c(%d%c(%d%c%d))=24\n",a,o1,b,o2,c,o3,d);//exp-5用于计算 a ( b ( c d ) )表达式
						++t; 
						
						char che0[16];
						char che1[3];
						char che2[3];
						char che3[3]="(";
						char che4[3];
						char che5[3];
						char che6[3]="(";
						char che7[3];
						char che8[3];
						char che9[3];
						char che10[3]=")";
						char che11[3]=")";
						che0[0]='\0';
						sprintf(che1,"%d", a);
						che2[0]=o1;
						che2[1]='\0';
						
						sprintf(che4,"%d", b);
						che5[0]=o2;
						che5[1]='\0';
						
						sprintf(che7,"%d", c);
						che8[0]=o3;
						che8[1]='\0';
						sprintf(che9,"%d", d);
						
						
						strcat(che0,che1);
						strcat(che0,che2);
						strcat(che0,che3);
						strcat(che0,che4);
						strcat(che0,che5);
						strcat(che0,che6);
						strcat(che0,che7);
						strcat(che0,che8);
						strcat(che0,che9);
						strcat(che0,che10);
						strcat(che0,che11);						
						if(strcmp(che0,ans)==0)		//answer:答案(客户输入的答案)。	
							++l;					//l用于判断答案正误 
									 
					}
						
						
												
				}
			}			
		}
		 
}
int main() 
{
	

	
	/* 游戏开始界面显示 */ 
	printf("\t_________________***二十四点小游戏***______________________\n");
	printf("\n");																
	printf("\t24点是4个数通过四则运算、括号得到结果为24的一种数字游戏。\n");
	Sleep(1*1000);								//延时显示,避免瞬间弹出大量文字,令用户能详尽了解规则 
	printf("\t在扑克中表现为;您将随机获得四张牌,\n");
	Sleep(1*1000);
	printf("\t而且J代表11,Q代表12,K代表13。\n");
	Sleep(1*1000);	
	printf("\t规则简单,变化多多,益智有趣!\n");
	printf("\t你是否已经迫不及待?\n");
	Sleep(1*1000);
	printf("\t动动脑筋,告诉我答案吧。\n");	
	printf("\t___________________________________________________________\n");
	printf("\n");
	Sleep(1*1000);
	printf("\t按任意键开始游戏---");
	getch();
	printf("\n");
	
	
	again:
	t=0,l=0;
	char ans[16];
	srand(time(NULL));              //srand函数种下种子,供rand函数使用(需用到#include<stdlib.h>和#include<time.h>头文件) 
	int a,b,c,d;
	a=rand()%13+1;    //随机数的范围是1-13 
	b=rand()%13+1;
	c=rand()%13+1;
	d=rand()%13+1;
	
	float e,f,g,h;
	e=a;
	f=b;
	g=c;
	h=d;
	
	
	printf("\t题目为:"); 
	printf("%d %d %d %d",a,b,c,d);
	printf("\n"); 	
	printf("\t________________________________________________________\n");
	printf("\n\t***输入规范***:四个数字将产生三次运算。\n");
	printf("\t请在前两步运算中使用括号(即使是括号可以省略的情况下)。\n"); 
	printf("\t如:1+2+3+4;应输入((1+2)+3)+4\n。");
	printf("\t括号应使用英文中的“(”“)”括号。\n");
	printf("\t无解题目请输入“no answer”。\n");
	printf("\t________________________________________________________\n");	
	printf("\t请输入您的答案(输入完毕请按回车键):");
	gets(ans);	
	
	
	
		
	p(a,b,c,d,e,f,g,h,ans);
	/*第二种组合*/
	p(a,b,d,c,e,f,h,g,ans);
	/*第三种组合*/
	p(a,c,b,d,e,g,f,h,ans); 
	/*第四种组合*/
	p(a,c,d,b,e,g,h,f,ans);
	/*第五种组合*/
	p(a,d,b,c,e,h,f,g,ans);
	/*第六种组合*/
	p(a,d,c,b,e,h,g,f,ans);
	
	
	/*第七种组合*/
	p(b,a,c,d,f,e,g,h,ans);
	/*第八种组合*/
	p(b,a,d,c,f,e,h,g,ans);
	/*第九种组合*/
	p(b,c,a,d,f,g,e,h,ans);
	/*第十种组合*/
	p(b,c,d,a,f,g,h,e,ans);
	/*第十一种组合*/
	p(b,d,a,c,f,h,e,g,ans);
	/*第十二种组合*/
	p(b,d,c,a,f,h,g,e,ans);
	
	
	/*第十三种组合*/
	p(c,a,b,d,g,e,f,h,ans);
	/*第十四种组合*/
	p(c,a,d,b,g,e,h,f,ans);
	/*第十五种组合*/
	p(c,b,a,d,g,f,e,h,ans);
	/*第十六种组合*/
	p(c,b,d,a,g,f,h,e,ans);
	/*第十七种组合*/
	p(c,d,a,b,g,h,e,f,ans);
	/*第十八种组合*/
	p(c,d,b,a,g,h,f,e,ans);
	
	
	/*第十九种组合*/
	p(d,a,b,c,h,e,f,g,ans); 
	/*第二十种组合*/
	p(d,a,c,b,h,e,g,f,ans);
	/*第二十一种组合*/
	p(d,b,a,c,h,f,e,g,ans);
	/*第二十二种组合*/
	p(d,b,c,a,h,f,g,e,ans);
	/*第二十三种组合*/
	p(d,c,a,b,h,g,e,f,ans);
	/*第二十四种组合*/
	p(d,c,b,a,h,g,f,e,ans);
	
	
	if(t!=0)
	{
		printf("\t本题答案共有如上全部答案!\n");
	}
	else
	{
		printf("\t此题确是无解!\n");
	}
	if(l!=0)
	{	
	printf("\t___________________\n");
	printf("\t|恭喜你,你答对了!|\n");
	printf("\t___________________\n");
	}
	if(t!=0&&l==0)
	{
		printf("\t___________________________\n");
		printf("\t|很可惜,您的答案不尽人意!|\n");
		printf("\t___________________________\n");
		printf("\t\n"); 
	}
	system("pause");
	goto again; 
} 
	


    

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值