程序设计(24点游戏)源码贴上来晒晒
刚刚做完了个小程序,24点游戏,就此把代码贴上来晒晒,大家给点意见啊用到的数据结构:队列、堆栈
用到的算法:中缀表达式转后缀表达式,后缀表达式的计算,穷举法(判断给定的4个数能否经()、+、-、*、/计算得出24)
QQ:353449704
24Points.rar
(6.32 KB)
// 24点求算.cpp : Defines the entry point for the console application. // #include "stdafx.h" void jieguo1(int w,float x,float y); void jieguo2(int w,float x); float faze(float x,float y); float s[6],ch[6],a[4]; int i=0,l=0,k=0; int main(int argc, char* argv[]) { float e; int t=0,p=0,q=0,c=0,d=0,m=0,f=0; m=0; for (i=0;i<=3;++i){ scanf("%f",&a[i]); } for (i=0;i<=2;++i){ for (l=i+1;l<=3;++l){ faze(a[i],a[l]); for (t=0;t<=5;++t){ ch[t]=s[t]; } for (f=0;f<=3;++f){ if(f!=i&&f!=l){ q=6-(i+f+l); for (k=0;k<=5;++k){ faze(ch[k],a[f]); for (p=0;p<=5;++p){ e=s[p]+a[q]; if (e==24){ printf("("); jieguo2(p,a[f]); printf(")"); printf("+%f=24\n",a[q]); printf("\n"); m=1; break;} e=s[p]-a[q]; if (e==24){ printf("("); jieguo2(p,a[f]); printf(")"); printf("-%f=24\n",a[q]); printf("\n"); m=1; break;} e=a[q]-s[p]; if (e==24){ printf("%f-",a[q]); printf("("); jieguo2(p,a[f]); printf(")=24\n"); printf("\n"); m=1; break;} e=s[p]*a[q]; if (e==24){ printf("("); jieguo2(p,a[f]); printf(")"); printf("*%f=24\n",a[q]); printf("\n"); m=1; break;} if (a[q]!=0){ e=s[p]/a[q]; if (e==24){ printf("("); jieguo2(p,a[f]); printf(")"); printf("/%f=24\n",a[q]); printf("\n"); m=1; break; }} if (s[p]!=0){ e=a[q]/s[p]; if (e==24){ printf("%f/",a[q]); printf("("); jieguo2(p,a[f]); printf(")=24\n"); printf("\n"); m=1; break; }} if (a[q]!=0){ e=s[p]/a[q]; e=e-24; if (e<=0.00001&&e>0){ printf("("); jieguo2(p,a[f]); printf(")"); printf("/%f=24\n",a[q]); printf("\n"); m=1; break; }} if (a[q]!=0){ e=s[p]/a[q]; e=e-24; if (e>=-0.00001&&e<0){ printf("("); jieguo2(p,a[f]); printf(")"); printf("/%f=24\n",a[q]); printf("\n"); m=1; break; }} if (s[p]!=0){ e=a[q]/s[p]; e=e-24; if (e<=0.00001&&e>0){ printf("%f/",a[q]); printf("("); jieguo2(p,a[f]); printf(")=24\n"); printf("\n"); m=1; break; }} if (s[p]!=0){ e=a[q]/s[p]; e=e-24; if (e>=-0.00001&&e<0){ printf("%f/",a[q]); printf("("); jieguo2(p,a[f]); printf(")=24\n"); printf("\n"); m=1; break; }} } } } } } } if(m==0) printf("i can not work out it"); return 0; } float faze(float x,float y){ int i; s[0]=x+y; s[1]=x-y; s[2]=y-x; s[3]=x*y; if(y!=0){ s[4]=x/y;} else { s[4]=s[1]; } if(x!=0){ s[5]=y/x;} else { s[5]=s[1]; } for (i=0;i<=5;++i){ return s[i]; } return 0; } void jieguo1(int w,float x,float y){ switch(w){ case 0: printf("%f+%f",x,y); break; case 1: printf("%f-%f",x,y); break; case 2: printf("%f-%f",y,x); break; case 3: printf("%f*%f",x,y); break; case 4: printf("%f/%f",x,y); break; case 5: printf("%f/%f",y,x); break; } } void jieguo2(int w,float x){ switch(w){ case 0: printf("%f+",x); printf("("); jieguo1(k,a[i],a[l]); printf(")"); break; case 1: printf("("); jieguo1(k,a[i],a[l]); printf(")"); printf("-%f",x); break; case 2: printf("%f-",x); printf("("); jieguo1(k,a[i],a[l]); printf(")"); break; case 3: printf("%f*",x); printf("("); jieguo1(k,a[i],a[l]); printf(")"); break; case 4: printf("("); jieguo1(k,a[i],a[l]); printf(")"); printf("/%f",x); break; case 5: printf("%f/",x); printf("("); jieguo1(k,a[i],a[l]); printf(")"); break; } }