标题:大家有兴趣的话就对这个用C开发的24点小游戏 的程序进行解析一下
只看楼主
sammy6083
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2005-4-11
 问题点数:0 回复次数:0 
大家有兴趣的话就对这个用C开发的24点小游戏 的程序进行解析一下

#include<stdio.h> #include<math.h>

int treat(float a,float b,float c,float d); float myF(int flag,float m,float n); void myPrint(int type,int i,int j,int k,float a,float b,float c,float d);

int time,temp=0; void main() { int i,j,k,t,again,res,flag; float num[4]; again=1; while(again==1) { printf ("\nPlease Enter 4 nums(1~13):\n"); i=0; flag=0; while (flag==0) { i++; // printf ("Input num-%d\n",i); for(i=0;i<4;i++) { scanf("%f",&num[i]); if (num[i]<1 || num[i]>13 || num[i]!=int(num[i])) flag++; } if(flag!=0) { printf ("Error input again\n",i); flag=0; } else flag=1; } for (i=0;i<4;i++) for (j=0;j<4;j++) if (j!=i) for (k=0;k<4;k++) if (k!=j && k!=i) for (t=0;t<4;t++) if (t!=i && t!=j && t!=k) { res=treat(num[i],num[j],num[k],num[t]); } if (res==0) printf ("\nNo answer\n"); else ; // printf ("time=%d\n\n",time); printf ("\n1: Go on\n2: Quit\n"); scanf ("%d",&again); } }

int treat(float a,float b,float c,float d) { int i,j,k; float sum1,sum2,sum3; for (i=0;i<4;i++) for (j=0;j<6;j++) for (k=0;k<6;k++) { if ((!(i==3 && b==0)) && (!(j==3 && c==0)) && (!(k==3 && d==0))) { sum1=myF(i,a,b); sum2=myF(j,sum1,c); sum3=myF(k,sum2,d); if (fabs(sum3-24)<0.1) { temp++; myPrint(1,i,j,k,a,b,c,d); // printf ("sum1:myF(%d,%2.0f,%2.0f) sum1=%f\n",i,a,b,sum1); // printf ("sum2:myF(%d,%2.0f,%2.0f) sum2=%f\n",j,c,d,sum2); // printf ("1:myF(%d,myF(%d,myF(%d,%2.0f,%2.0f),%2.0f),%2.0f) sum3=%f\n\n",k,j,i,a,b,c,d,sum3); } } if (k==2) { sum1=myF(i,a,b); sum2=myF(j,c,d); sum3=sum1*sum2; if (fabs(sum3-24)<0.1) { temp++; myPrint(2,i,j,k,a,b,c,d); // printf ("sum1:myF(%d,%2.0f,%2.0f) sum1=%f\n",i,a,b,sum1); // printf ("sum2:myF(%d,%2.0f,%2.0f) sum2=%f\n",j,c,d,sum2); // printf ("2:myF(%d,myF(%d,%2.0f,%2.0f),myF(%d,%2.0f,%2.0f)) sum3=%f\n\n",k,i,a,b,j,c,d,sum3); } }

if (k==3) { sum1=myF(i,a,b); sum2=myF(j,c,d); if (sum2!=0) { sum3=sum1/sum2; if (fabs(sum3-24)<0.1) { temp++; myPrint(3,i,j,k,a,b,c,d); // printf ("sum1:myF(%d,%2.0f,%2.0f) sum1=%f\n",i,a,b,sum1); // printf ("sum2:myF(%d,%2.0f,%2.0f) sum2=%f\n",j,c,d,sum2); // printf ("3:myF(%d,myF(%d,%2.0f,%2.0f),myF(%d,%2.0f,%2.0f)) sum3=%f\n\n",k,i,a,b,j,c,d,sum3); } } } } if (temp==0) return 0; else return 1; }

float myF(int flag,float m,float n) { // time++; if (flag==0) return (m+n); if (flag==1) return (m-n); if (flag==2) return (m*n); if (flag==3) if (n==0) return 30000; else return (m/n); if (flag==4) return (n-m); if (flag==5) if (m==0) return 30000; else return (n/m); return 0; }

void myPrint(int type,int i,int j,int k,float a,float b,float c,float d) { char sigle[6]; sigle[0]='+'; sigle[1]='-'; sigle[2]='*'; sigle[3]='/'; sigle[4]='-'; sigle[5]='/'; if (type==1){ if(j==4 || j==5) { if (k==4 || k==5) printf("%2.0f %c (%2.0f %c (%2.0f %c %2.0f)) =24\n",d,sigle[k],c,sigle[j],a,sigle[i],b); else printf("(%2.0f %c (%2.0f %c %2.0f)) %c %2.0f =24\n",c,sigle[j],a,sigle[i],b,sigle[k],d); } else if (k==4 || k==5) { printf("%2.0f %c ((%2.0f %c %2.0f) %c %2.0f) =24\n",d,sigle[k],a,sigle[i],b,sigle[j],c); } else printf("((%2.0f %c %2.0f) %c %2.0f) %c %2.0f =24\n",a,sigle[i],b,sigle[j],c,sigle[k],d); } if (type==2 || type==3) { // if (k==4 || k==5) // printf("(%2.0f %c %2.0f) %c (%2.0f %c %2.0f)=24\n",c,sigle[j],d,sigle[k],a,sigle[i],b); // else printf("(%2.0f %c %2.0f) %c (%2.0f %c %2.0f) =24\n",a,sigle[i],b,sigle[k],c,sigle[j],d); } }

搜索更多相关主题的帖子: float int 小游戏 flag 兴趣 
2005-05-10 18:59



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-17819-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.214104 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved