首页 > 代码库 > 24点游戏计算器 (加、减、乘、除、阶乘、导数)(c++)

24点游戏计算器 (加、减、乘、除、阶乘、导数)(c++)

24点游戏计算器 (加、减、乘、除、阶乘、导数)(c++):https://github.com/liuxinig/cpp_1001/blob/master/24dian_fuZaIN.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 //计算阶乘 33 int jieCheng(int t) 34 { 35     int ft = 1; 36     for(int i = 2;i <= t;i++) 37         ft = ft * i; 38     return ft; 39 } 40 //计算 通过 41  42 float qiuZhi(float x,float y,int f) 43 { 44     switch (f) 45     { 46     case 0: 47         return x + y; 48     case 1: 49         return x - y; 50     case 2: 51         return x * y; 52     case 3: 53         if(y == 0) 54             return -1; 55         return x / y; 56     case 4: 57         if(x == 0) 58             return -1; 59         return y / x; 60     case 5: 61         return y - x; 62     } 63 } 64  65 bool jiSuan() 66 { 67     int i,w = 0; 68     float b1,b2; 69     for(i = 0;i < 3;i++) 70     { 71         if(i == 0) 72         { 73             b1 = qiuZhi(b[w],b[w + 1],fuHao[0]); 74             if(b1 == -1) 75                 return false; 76  77             if(p[11] == 1) 78                 b1 = jieCheng(b1); 79             else if(p[11] == 2) 80                 b1 = 0; 81             w += 2; 82         } 83         else 84         { 85             if(abs(cixu[0] - cixu[1]) == 1) 86             { 87                 b1 = qiuZhi(b1,b[w],fuHao[i]); 88                 if(b1 == -1) 89                     return false; 90                 if(p[12] == 1) 91                     b1 = jieCheng(b1); 92                 else if(p[12] == 2) 93                     b1 == 0; 94                 w++; 95             } 96             else 97             { 98                 b2 = qiuZhi(b[2],b[3],fuHao[1]); 99                 if(b2 == -1)100                     return false;101                 if(p[12] == 1)102                     b2 = jieCheng(b2);103                 else if(p[12] == 2)104                     b2 == 0;105                 b1 = qiuZhi(b1,b2,fuHao[2]);106                 break;107             }108         }109         110     }111     if(p[13] == 1)112         b1 = jieCheng(b1);113     if(b1 == 24)114         return true;115     return false;116 }117 //返回除数与被除数的顺序,是左边为除数还是右边为除数  4代表左边为除数,3代表右边为除数  通过118 int fuHaoPD(int t1,int t2)119 {120     int t = p[t1 + 3];121     if(t == 0 || t == 2)122         return t;123     else if(t == 3)124     {125         if(t2 != 0 && cixu[t2] < cixu[t2 - 1] && abs(cixu[0] - cixu[1]) != 2)126             return 4;127         return 3;128     }129     else if(t == 1)130     {131         if(t2 != 0 && cixu[t2] < cixu[t2 - 1] && abs(cixu[0] - cixu[1]) != 2)132             return 5;133         return 1;134     }135 }136 //构造表达式 (根据找到的计算次序构造出从左到右的计算式)   通过137 void zhengHe()138 {139     int w1 = 0,w2 = 0;140     for(int i = 0;i < 3;i++)141     {142         switch (cixu[i])143         {144         case 1:145             {146                 if(a[0] != -1)147                 {148                     b[w1] = a[0];149                     w1++;150                     a[0] = -1;151                 }152                 if(a[1] != -1)153                 {154                     b[w1] = a[1];155                     w1++;156                     a[1] = -1;157                 }158                 fuHao[w2] = fuHaoPD(1,i);159                 w2++;160             }break;161 162         case 2:163             {164                 if(a[1] != -1)165                 {166                     b[w1] = a[1];167                     w1++;168                     a[1] = -1;169                 }170                 if(a[2] != -1)171                 {172                     b[w1] = a[2];173                     w1++;174                     a[2] = -1;175                 }176                 fuHao[w2] = fuHaoPD(2,i);177                 w2++;178             }break;179 180         case 3:181             {182                 if(a[2] != -1)183                 {184                     b[w1] = a[2];185                     w1++;186                     a[2] = -1;187                 }188                 if(a[3] != -1)189                 {190                     b[w1] = a[3];191                     w1++;192                     a[3] = -1;193                 }194                 fuHao[w2] = fuHaoPD(3,i);195                 w2++;196             }break;197 198         }199     }200 }201 //查找计算顺序(即三个运算符的运算次序   类似123 213 321 ...)   通过202 void ciXu()203 {204     int i,j,k,l,w = 0;205 206     //若有括号207     if(p[8] != 0 || p[7] != 0)208     {209         if(p[8] != 0)   //如果有括号1210         {211             cixu[w] = p[8];  //第一计算次序为括号1的表达式212             w++;213             if(p[7] != 0)    //如果还有括号2214             {215                 if(p[10] - p[7] > 1)   //如果括号2为大括号216                 {//大括号中没在小括号的运算符为第二计算次序217                     if(p[7] == p[8])    //如果两括号起始位置相同,则第二运算次序为小括号加一218                         cixu[w] = p[8] + 1;219                     else                //否则为减一220                         cixu[w] = p[8] - 1;221                     w++;222                     223                 }224                 else  //如果括号2为小括号225                 {226                     cixu[w] = p[7];227                     w++;228                 }229                 cixu[w] = 6 - cixu[0] - cixu[1];  //最后一个没在括号中的运算符   运算符顺序分别为 1,2,3 和为6    最后一个为 6 - (第一个) - (第二个)230             }231             else     //如果没有括号2232             {233                 234                 if(cixu[0] == 2)   235                 {236                     if(p[6] > 1)237                     {238                         cixu[1] = 3;239                         cixu[2] = 1;240                     }241                     else242                     {243                         cixu[1] = 1;244                         cixu[2] = 3;245                     }246                 }247                 else if(cixu[0] == 1)248                 {249                     if(p[6] > 1)250                     {251                         cixu[1] = 3;252                         cixu[2] = 2;253                     }254                     else255                     {256                         cixu[1] = 2;257                         cixu[2] = 3;258                     }259                 }260                 else261                 {262                     if(p[4] > 1)263                     {264                         cixu[1] = 1;265                         cixu[2] = 2;266                     }267                     else268                     {269                         cixu[1] = 2;270                         cixu[2] = 1;271                     }272                 }273                 274             }275         }276         else     //如果只有大括号     通过277         {278             for(i = p[7] - 1;i < p[10] - 1;i++)//括号中279                 if(p[4 + i] > 1)280                 {281                     cixu[w] = i + 1;282                     w++;283                 }284             for(i = p[7] - 1;i < p[10] - 1;i++)//括号中285                 if(p[4 + i] < 2)286                 {287                     cixu[w] = i + 1;288                     w++;289                 }290             if(p[7] == 1)291                 cixu[w] = 3;292             else293                 cixu[w] = 1;294         }295     }296     else    // 没有括号    通过297     {298         for(i = 0;i < 3;i++)299             if(p[4 + i] > 1)300             {301                 cixu[w] = i + 1;302                 w++;303             }304         if(w == 0)305         {306             cixu[0] = 1;307             cixu[1] = 2;308             cixu[2] = 3;309         }310         else if(w == 1)311         {312             if(cixu[0] == 2)   313             {314                 if(p[6] > 1)315                 {316                     cixu[1] = 3;317                     cixu[2] = 1;318                 }319                 else320                 {321                     cixu[1] = 1;322                     cixu[2] = 3;323                 }324             }325             else if(cixu[0] == 1)326             {327                 if(p[6] > 1)328                 {329                     cixu[1] = 3;330                     cixu[2] = 2;331                 }332                 else333                 {334                     cixu[1] = 2;335                     cixu[2] = 3;336                 }337             }338             else339             {340                 if(p[4] > 1)341                 {342                     cixu[1] = 1;343                     cixu[2] = 2;344                 }345                 else346                 {347                     cixu[1] = 2;348                     cixu[2] = 1;349                 }350             }351         }352         else if(w == 2)353             cixu[w] = 6 - cixu[1] - cixu[0];354     /*    for(i = 0;i < 3;i++)355             if(p[4 + i] < 2)356             {357                 cixu[w] = i + 1;358                 w++;359             }*/360     }361 362 363 }364 //处理阶乘或求导365 void jcds()366 {367     for(int i = 0;i < 4;i++)368         if(p[i] == 1)369             a[i] = jieCheng(a[i]);370         else if(p[i] == 2)371             a[i] = 0;372 }373 //判断某表达式是否符合374 bool panDuan()375 {376     jcds();377     ciXu();378     zhengHe();379     if(jiSuan() == true)380         return true;381     return false;382 }383 //输出表达式到文件384 void outPut()385 {386     int i;387     if(p[13] == 1)388         cout<<"(";389     for(i = 0;i < 4;i++)390     {391         if(p[7] == i + 1)392             cout<<"(";393         if(p[8] == i + 1)394             cout<<"(";395         cout<<a0[i];396     397         if(p[i] == 1)398             cout<<!;399         else if(p[i] == 2)400             cout<<`;401         if(p[9] == i + 1)402         {403             cout<<")";404             if(p[11] == 1)405                 cout<<"!";406             else if(p[11] == 2)407                 cout<<"`";408         }409         if(p[10] == i + 1)410         {411                 cout<<")";412             if(p[12] == 1)413                 cout<<"!";414             else if(p[12] == 2)415                 cout<<"`";416         }417         if(i != 3)418         {419             switch (p[i + 4])420             {421             case 0:422                 cout<<+;break;423             case 1:424                 cout<<-;break;425             case 2:426                 cout<<*;break;427             case 3:428                 cout<</;break;429             }430         }431         432     }433     if(p[13] == 1)434         cout<<")!";435     cout<<"=24"<<endl;436 }437 438 void chuShiHua()439 {440     int i;441     for(i = 0;i < 3;i++)442     {443         cixu[i] = 0;444         a[i] = a0[i];445     }446     a[i] = a0[i];447 }448 449 void chuLi()450 {451     int i,i1,i2,i3,i4,l1,l2,l3,l4,m,n,q,r,s = 0,t;452     for(i = 0;i < N;i++)            //初始化p数组为0453         p[i] = 0;454     for(i1 = 0;i1 < 3;i1++)455         for(i2 = 0;i2 < 3;i2++)456             for(i3 = 0;i3 < 3;i3++)457                 for(i4 = 0;i4 < 3;i4++)458                     for(l1 = 0;l1 < 4;l1++)459                         for(l2 = 0;l2 < 4;l2++)460                             for(l3 = 0;l3 < 4;l3++)461                                 {462                                     p[0] = i1;463                                     p[1] = i2;464                                     p[2] = i3;465                                     p[3] = i4;466                                     p[4] = l1;467                                     p[5] = l2;468                                     p[6] = l3;469                                     for(m = 0;m < 4;m++)470                                     {471                                         if(m == 1)472                                         {473                                             p[8] = 1;474                                             p[9] = 2;475                                         }476                                         else if(m == 2)477                                         {478                                             p[8] = 2;479                                             p[9] = 3;480                                         }481                                         else if(m == 3)482                                         {483                                             p[8] = 3;484                                             p[9] = 4;485                                         }486                                         else487                                         {488                                             p[8] = 0;489                                             p[9] = 0;490                                         }491                                         for(n = 0;n < 4;n++)492                                         {493                                             if(n == 1 && m != 3)494                                             {    495                                                 p[7] = 1;496                                                 p[10] = 3;497                                             }498                                             else if( n == 2 && m != 1)499                                             {500                                                 p[7] = 2;501                                                 p[10] = 4;502                                             }503                                             else if(n == 3 && m == 1)504                                             {505                                                 p[7] = 3;506                                                 p[10] = 4;507                                             }508                                             else if(n == 0)509                                             {510                                                 p[7] = 0;511                                                 p[10] = 0;512                                             }513                                             else514                                                 continue;515                                             for(q = 0;q < 3;q++)516                                             {517                                                 if(p[9] != 0)518                                                     p[11] = q;519                                                 else520                                                     p[11] = 0;521 522                                                 for(r = 0;r < 3;r++)523                                                 {524                                                     if(p[10] != 0)525                                                         p[12] = r;526                                                     p[12] = 0;527                                                     for(t = 0;t < 2;t++)528                                                     {529                                                         p[13] = t;530                                                         chuShiHua();531                                                         if(panDuan() == true)532                                                         {533                                                             sum++;534                                                             outPut();535                                                         }536                                                     }537                                                 }538                                                 539                                             }540                                         }541 542                                     }543                                 }544 545 }546 547 bool chongZhi(int &w)548 {549     int i,j;550     bool flag;551     for(i = 0;i < w;i++)552     {553         flag = true;554         for(j = 0;j < 4;j++)555             if(zuhe[i][j] != zuhe[w][j])556                 flag = false;557         if(flag == true)558             return true;559     }560     return false;561 }562 563 void zuHe()564 {565     int i,j,k,l,w = 0,s;566     for(i = 0;i < 4;i++)567         for(j = 0;j < 4;j++)568             for(k = 0;k < 4;k++)569                 for(l = 0;l < 4;l++)570                 {571                     if(i != j && i != k && i != l && j != k && j != l && k != l)572                     {573                         zuhe[w][0] = b0[i];574                         zuhe[w][1] = b0[j];575                         zuhe[w][2] = b0[k];576                         zuhe[w][3] = b0[l];577                         if(chongZhi(w) == true)578                             continue;579                         for(s = 0;s < 4;s++)580                         {581                             a0[s] = zuhe[w][s];582                         }583                         w++;584                         chuLi();585                     }586                 }587 }588 589 void main()590 {591     cout<<"请输入四个数:";592     cin>>b0[0]>>b0[1]>>b0[2]>>b0[3];593     zuHe();    594     cout<<""<<sum<<"种组合方式"<<endl;595 }
24dian_fuZa

 

24点游戏计算器 (加、减、乘、除、阶乘、导数)(c++)