首页 > 代码库 > 24点游戏计算器 (简单四则运算)(c++)

24点游戏计算器 (简单四则运算)(c++)

24点游戏计算器 (简单四则运算)(c++):https://github.com/liuxinig/cpp_1001/blob/master/24dian_siZeIN.txt

技术分享
  1 //24点统计  2   3 #include <iostream>  4 #include <cmath>  5 using namespace std;  6 #define N 14  7 //a数组存四个数字  8 int cixu[3],fuHao[3],p[N],sum = 0;  9 float a0[4],a[4],b[4],b0[4]; 10 float zuhe[24][4]; 11 /*    12 各数组代表含义 13 cixu 记录运算符调用次序 14  15 a数组存初始的四个数 16  17 fuHao 存整合后的运算符顺序 18  19 b数组存整合后的数的计算次序 20  21 b0数组为用户输入的四个数 22  23 P数组各下标位置所代表的含义: 24 0,1,2,3:四个数是否需要阶乘(1),导数(2),或不变(0) 25 4,5,6:三个运算符  0:+    1:-    2:*    3 :/ 26 7,10:括号1位置   0代表没有 27 8,9:括号2位置   0代表没有 28 11:括号1是否需要阶乘或求导 29 12:括号2是否需要阶乘或求导 30 */ 31  32 float qiuZhi(float x,float y,int f) 33 { 34     switch (f) 35     { 36     case 0: 37         return x + y; 38     case 1: 39         return x - y; 40     case 2: 41         return x * y; 42     case 3: 43         if(y == 0) 44             return -1; 45         return x / y; 46     case 4: 47         if(x == 0) 48             return -1; 49         return y / x; 50     case 5: 51         return y - x; 52     } 53 } 54  55 bool jiSuan() 56 { 57     int i,w = 0; 58     float b1,b2; 59     for(i = 0;i < 3;i++) 60     { 61         if(i == 0) 62         { 63             b1 = qiuZhi(b[w],b[w + 1],fuHao[0]); 64             if(b1 == -1) 65                 return false; 66             w += 2; 67         } 68         else 69         { 70             if(abs(cixu[0] - cixu[1]) == 1) 71             { 72                 b1 = qiuZhi(b1,b[w],fuHao[i]); 73                 if(b1 == -1) 74                     return false; 75                 w++; 76             } 77             else 78             { 79                 b2 = qiuZhi(b[2],b[3],fuHao[1]); 80                 if(b2 == -1) 81                     return false; 82                 b1 = qiuZhi(b1,b2,fuHao[2]); 83                 break; 84             } 85         } 86  87     } 88     if(b1 == 24) 89         return true; 90     return false; 91 } 92  93 //返回除数与被除数的顺序,是左边为除数还是右边为除数  4代表左边为除数,3代表右边为除数  通过 94 int fuHaoPD(int t1,int t2) 95 { 96     int t = p[t1 + 3]; 97     if(t == 0 || t == 2) 98         return t; 99     else if(t == 3)100     {101         if(t2 != 0 && cixu[t2] < cixu[t2 - 1] && abs(cixu[0] - cixu[1]) != 2)102             return 4;103         return 3;104     }105     else if(t == 1)106     {107         if(t2 != 0 && cixu[t2] < cixu[t2 - 1] && abs(cixu[0] - cixu[1]) != 2)108             return 5;109         return 1;110     }111 }112 113 //构造表达式 (根据找到的计算次序构造出从左到右的计算式)   通过114 void zhengHe()115 {116     int w1 = 0,w2 = 0;117     for(int i = 0;i < 3;i++)118     {119         switch (cixu[i])120         {121         case 1:122             {123                 if(a[0] != -1)124                 {125                     b[w1] = a[0];126                     w1++;127                     a[0] = -1;128                 }129                 if(a[1] != -1)130                 {131                     b[w1] = a[1];132                     w1++;133                     a[1] = -1;134                 }135                 fuHao[w2] = fuHaoPD(1,i);136                 w2++;137             }break;138 139         case 2:140             {141                 if(a[1] != -1)142                 {143                     b[w1] = a[1];144                     w1++;145                     a[1] = -1;146                 }147                 if(a[2] != -1)148                 {149                     b[w1] = a[2];150                     w1++;151                     a[2] = -1;152                 }153                 fuHao[w2] = fuHaoPD(2,i);154                 w2++;155             }break;156 157         case 3:158             {159                 if(a[2] != -1)160                 {161                     b[w1] = a[2];162                     w1++;163                     a[2] = -1;164                 }165                 if(a[3] != -1)166                 {167                     b[w1] = a[3];168                     w1++;169                     a[3] = -1;170                 }171                 fuHao[w2] = fuHaoPD(3,i);172                 w2++;173             }break;174 175         }176     }177 }178 179 //查找计算顺序(即三个运算符的运算次序   类似123 213 321 ...)   通过180 void ciXu()181 {182     int i,j,k,l,w = 0;183     //若有括号184     if(p[8] != 0 || p[7] != 0)185     {186         if(p[8] != 0)   //如果有括号1187         {188             cixu[w] = p[8];  //第一计算次序为括号1的表达式189             w++;190             if(p[7] != 0)    //如果还有括号2191             {192                 if(p[10] - p[7] > 1)   //如果括号2为大括号193                 {//大括号中没在小括号的运算符为第二计算次序194                     if(p[7] == p[8])    //如果两括号起始位置相同,则第二运算次序为小括号加一195                         cixu[w] = p[8] + 1;196                     else                //否则为减一197                         cixu[w] = p[8] - 1;198                     w++;199 200                 }201                 else  //如果括号2为小括号202                 {203                     cixu[w] = p[7];204                     w++;205                 }206                 cixu[w] = 6 - cixu[0] - cixu[1];  //最后一个没在括号中的运算符   运算符顺序分别为 1,2,3 和为6    最后一个为 6 - (第一个) - (第二个)207             }208             else     //如果没有括号2209             {210 211                 if(cixu[0] == 2)   212                 {213                     if(p[6] > 1)214                     {215                         cixu[1] = 3;216                         cixu[2] = 1;217                     }218                     else219                     {220                         cixu[1] = 1;221                         cixu[2] = 3;222                     }223                 }224                 else if(cixu[0] == 1)225                 {226                     if(p[6] > 1)227                     {228                         cixu[1] = 3;229                         cixu[2] = 2;230                     }231                     else232                     {233                         cixu[1] = 2;234                         cixu[2] = 3;235                     }236                 }237                 else238                 {239                     if(p[4] > 1)240                     {241                         cixu[1] = 1;242                         cixu[2] = 2;243                     }244                     else245                     {246                         cixu[1] = 2;247                         cixu[2] = 1;248                     }249                 }250 251             }252         }253         else     //如果只有大括号     通过254         {255             for(i = p[7] - 1;i < p[10] - 1;i++)//括号中256                 if(p[4 + i] > 1)257                 {258                     cixu[w] = i + 1;259                     w++;260                 }261                 for(i = p[7] - 1;i < p[10] - 1;i++)//括号中262                     if(p[4 + i] < 2)263                     {264                         cixu[w] = i + 1;265                         w++;266                     }267                     if(p[7] == 1)268                         cixu[w] = 3;269                     else270                         cixu[w] = 1;271         }272     }273     else    // 没有括号    通过274     {275         for(i = 0;i < 3;i++)276             if(p[4 + i] > 1)277             {278                 cixu[w] = i + 1;279                 w++;280             }281             if(w == 0)282             {283                 cixu[0] = 1;284                 cixu[1] = 2;285                 cixu[2] = 3;286             }287             else if(w == 1)288             {289                 if(cixu[0] == 2)   290                 {291                     if(p[6] > 1)292                     {293                         cixu[1] = 3;294                         cixu[2] = 1;295                     }296                     else297                     {298                         cixu[1] = 1;299                         cixu[2] = 3;300                     }301                 }302                 else if(cixu[0] == 1)303                 {304                     if(p[6] > 1)305                     {306                         cixu[1] = 3;307                         cixu[2] = 2;308                     }309                     else310                     {311                         cixu[1] = 2;312                         cixu[2] = 3;313                     }314                 }315                 else316                 {317                     if(p[4] > 1)318                     {319                         cixu[1] = 1;320                         cixu[2] = 2;321                     }322                     else323                     {324                         cixu[1] = 2;325                         cixu[2] = 1;326                     }327                 }328             }329             else if(w == 2)330                 cixu[w] = 6 - cixu[1] - cixu[0];331     }332 }333 334 //判断某表达式是否符合335 bool panDuan()336 {337     ciXu();338     zhengHe();339     if(jiSuan() == true)340         return true;341     return false;342 }343 344 void chuShiHua()345 {346     int i;347     for(i = 0;i < 3;i++)348     {349         cixu[i] = 0;350         a[i] = a0[i];351     }352     a[i] = a0[i];353 }354 355 //输出表达式到文件356 void outPut()357 {358     int i;359     for(i = 0;i < 4;i++)360     {361         if(p[7] == i + 1)362             cout<<"(";363         if(p[8] == i + 1)364             cout<<"(";365         cout<<a0[i];366 367         if(p[9] == i + 1)368         {369             cout<<")";370         }371         if(p[10] == i + 1)372         {373             cout<<")";374         }375         if(i != 3)376         {377             switch (p[i + 4])378             {379             case 0:380                 cout<<+;break;381             case 1:382                 cout<<-;break;383             case 2:384                 cout<<*;break;385             case 3:386                 cout<</;break;387             }388         }389 390     }391     cout<<"=24"<<endl;392 }393 394 void chuLi()395 {396     int i,i1,i2,i3,i4,l1,l2,l3,l4,m,n,q,r,t;397     for(i = 0;i < N;i++)            //初始化p数组为0398         p[i] = 0;399     for(l1 = 0;l1 < 4;l1++)400         for(l2 = 0;l2 < 4;l2++)401             for(l3 = 0;l3 < 4;l3++)402             {403                 p[0] = 0;404                 p[1] = 0;405                 p[2] = 0;406                 p[3] = 0;407                 p[4] = l1;408                 p[5] = l2;409                 p[6] = l3;410                 for(m = 0;m < 4;m++)411                 {412                     if(m == 1)413                     {414                         p[8] = 1;415                         p[9] = 2;416                     }417                     else if(m == 2)418                     {419                         p[8] = 2;420                         p[9] = 3;421                     }422                     else if(m == 3)423                     {424                         p[8] = 3;425                         p[9] = 4;426                     }427                     else428                     {429                         p[8] = 0;430                         p[9] = 0;431                     }432                     for(n = 0;n < 4;n++)433                     {434                         if(n == 1 && m != 3)435                         {    436                             p[7] = 1;437                             p[10] = 3;438                         }439                         else if( n == 2 && m != 1)440                         {441                             p[7] = 2;442                             p[10] = 4;443                         }444                         else if(n == 3 && m == 1)445                         {446                             p[7] = 3;447                             p[10] = 4;448                         }449                         else if(n == 0)450                         {451                             p[7] = 0;452                             p[10] = 0;453                         }454                         else455                             continue;456 457 458 459                         chuShiHua();460                         if(panDuan() == true)461                         {462                             outPut();463                             sum++;464                         }465 466 467 468 469                     }470 471                 }472             }473 }474 475 bool chongZhi(int &w)476 {477     int i,j;478     bool flag;479     for(i = 0;i < w;i++)480     {481         flag = true;482         for(j = 0;j < 4;j++)483             if(zuhe[i][j] != zuhe[w][j])484                 flag = false;485         if(flag == true)486             return true;487     }488     return false;489 }490 491 void zuHe()492 {493     int i,j,k,l,w = 0,s;494     for(i = 0;i < 4;i++)495         for(j = 0;j < 4;j++)496             for(k = 0;k < 4;k++)497                 for(l = 0;l < 4;l++)498                 {499                     if(i != j && i != k && i != l && j != k && j != l && k != l)500                     {501                         zuhe[w][0] = b0[i];502                         zuhe[w][1] = b0[j];503                         zuhe[w][2] = b0[k];504                         zuhe[w][3] = b0[l];505                         if(chongZhi(w) == true)506                             continue;507                         for(s = 0;s < 4;s++)508                         {509                             a0[s] = zuhe[w][s];510                         }511                         w++;512                         chuLi();513                     }514                 }515 }516 517 void main()518 {519     cout<<"请输入四个数:";520     cin>>b0[0]>>b0[1]>>b0[2]>>b0[3];521     zuHe();522     cout<<""<<sum<<""<<endl;523 }
24dian_siZe

 

24点游戏计算器 (简单四则运算)(c++)