码迷,mamicode.com
首页 > 其他好文 > 详细

24点游戏_简单界面_可直接运行

时间:2016-06-04 01:51:49      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:

  1 #include<Windows.h>
  2 #include<iostream>
  3 #include<cstdio>
  4 #include<cstdlib>
  5 #include<cstring>
  6 #include<string>
  7 #include<vector>
  8 #include<ctime>
  9 #include<cmath>
 10 using namespace std;
 11 
 12 #define Jia 100001
 13 #define Jian 100002
 14 #define Cheng 100003
 15 #define Chu 100004
 16 #define Qian 100005
 17 #define Hou 100006
 18 double card[4] = { 0 };
 19 string charexp[4];    
 20 bool yonanswer = 0;   
 21 unsigned countnum = 0;
 22 //---------------------------------
 23 void Start();
 24 void Interface();
 25 
 26 void Pos(int, int);
 27 double Count(double, double, double);
 28 int Judge(string);
 29 int Key();
 30 void Exchange(vector<double>&, string);
 31 void Search(int);
 32 double Calculate_Single(vector<double>);
 33 //---------------------------------
 34 void Start() {
 35     system("mode con cols=80 lines=40"); //设置控制台大小
 36     srand((unsigned)time(0));
 37     Interface();
 38 }
 39 
 40 void Interface() {
 41     Pos(4, 34);cout << "本轮四个数字:";
 42     Pos(5, 28);cout << card[0] << "      " << card[1] << "      " << card[2] << "      " << card[3];
 43     Pos(7, 12);cout << "┌──────────────────────────┐";
 44     Pos(8, 12);cout << "│                                                    │";
 45     Pos(9, 12);cout << "└──────────────────────────┘";
 46     Pos(10, 24);cout << "请在上方输入你的算式,按回车确定";
 47 }
 48 
 49 void Pos(int x, int y) {
 50     COORD p;
 51     p.X = y;p.Y = x;
 52     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), p);
 53 }
 54 double Count(double a, double b, double c) {
 55     if (c == Jia)     return a + b;
 56     if (c == Jian)    return a - b;
 57     if (c == Cheng)    return a * b;
 58     if (c == Chu)     return a / b;
 59     Pos(50, 0);    cerr << "运算出错,c不为加减乘除.";exit(1);
 60 }
 61 
 62 int Judge(string s) {
 63     int a[4] = { (int)card[0],(int)card[1], (int)card[2],(int)card[3] };
 64     int k = 0;//检测括号数
 65     for (char* p = &s[0];(*p);++p) {
 66         if ((*p > 57 || *p < 48) && (*p != * && *p != - && *p != + && *p != /&& *p != (&& *p != )))
 67             return 1;               
 68         if (*p == () {
 69             ++k;
 70             if (*(p + 1) == * || *(p + 1) == - || *(p + 1) == + || *(p + 1) == / || *(p + 1) == ))
 71                 return 1;
 72             if (*(p - 1) <= 57 && *(p - 1) >= 48) return 1;
 73         }
 74         else if (*p == )) {
 75             --k;
 76             if ((*(p + 1) <= 57 && *(p + 1) >= 48) || *(p + 1) == ()
 77                 return 1;
 78         }
 79         else if (*p == * || *p == - || *p == + || *p == /)
 80             if (*(p + 1) == * || *(p + 1) == - || *(p + 1) == + || *(p + 1) == / || *(p + 1) == ))
 81                 return 1;
 82         if (*p <= 57 && *p >= 48) {           //两位数的时候 
 83             int t = *p - 48;
 84             if (*(p + 1) <= 57 && *(p + 1) >= 48) {
 85                 ++p;
 86                 t = t * 10 + *p - 48;
 87             }
 88             int i;
 89             for (i = 0;i < 4;++i)
 90                 if (t == a[i]) { a[i] = 0; break; }
 91             if (i == 4)return 1;
 92         }
 93     }
 94     if (k) return 1;
 95     for (int i = 0;i < 4;++i) if (a[i]) return 1;
 96     return 0;
 97 }
 98 int Key() {
 99     if (GetAsyncKeyState(VK_ESCAPE)) return 3;
100     else if (GetAsyncKeyState(0x52)) return 1;
101     else if (GetAsyncKeyState(0x53)) return 2;
102     return 0;
103 }
104 
105 void Exchange(vector<double>& v, string s) {     //  
106     for (char* p = &s[0];*p;++p) {
107         if (*p <= 57 && *p >= 48) {     //是数字 
108             int re = *p - 48;           //获得这个数字 
109             if (*(p + 1) <= 57 && *(p + 1) >= 48) { //是数字 
110                 re = re * 10 + *(p + 1) - 48;       //组成一个二位数 
111                 ++p;
112             }
113             v.push_back((double)re);    //添加数字 
114             continue;
115         }
116         if (*p == +) { v.push_back(Jia);continue; }
117         if (*p == -) { v.push_back(Jian);continue; }
118         if (*p == *) { v.push_back(Cheng);continue; }
119         if (*p == /) { v.push_back(Chu);continue; }
120         if (*p == () { v.push_back(Qian);continue; }
121         if (*p == )) { v.push_back(Hou);continue; }
122     }
123 }
124 void Search(int n = 4) {       
125     if (n == 1) {
126         if (card[0] == 24) {
127             cout << charexp[0] << ", ";    //答案表达式 
128             yonanswer = true;              //置答案为true 
129             countnum++;                    //三个式子一行 
130             if ((countnum % 3) == 0)
131                 cout << endl;
132         }
133     }
134     for (int i = 0; i < n; i++) {
135         for (int j = i + 1; j < n; j++) {
136             double a, b;
137             string charexpa, charexpb;
138             a = card[i];
139             b = card[j];
140             card[j] = card[n - 1];           //最后的牌 
141             charexpa = charexp[i];           //前面的表达式 
142             charexpb = charexp[j];           //后面的表达式 
143             charexp[j] = charexp[n - 1];     //
144             charexp[i] = "( " + charexpa + "+ " + charexpb + ") ";
145             card[i] = a + b;
146             Search(n - 1);
147             charexp[i] = "( " + charexpa + "- " + charexpb + ") ";
148             card[i] = a - b;
149             Search(n - 1);
150             charexp[i] = "( " + charexpb + "- " + charexpa + ") ";
151             card[i] = b - a;
152             Search(n - 1);
153             charexp[i] = "( " + charexpa + "* " + charexpb + ") ";
154             card[i] = a*b;
155             Search(n - 1);
156             if (b != 0) {
157                 charexp[i] = "( " + charexpa + "/ " + charexpb + ") ";
158                 card[i] = a / b;
159                 Search(n - 1);
160             }
161             if (a != 0) {
162                 charexp[i] = "( " + charexpb + "/ " + charexpa + ") ";
163                 card[i] = b / a;
164                 Search(n - 1);
165             }
166             card[i] = a;
167             card[j] = b;
168             charexp[i] = charexpa;
169             charexp[j] = charexpb;
170         }
171     }
172 }
173 
174 double Calculate_Single(const vector<double> v) {
175     unsigned jia = 0, jian = 0, cheng = 0, chu = 0, qian = 0, hou = 0;
176     for (auto t : v) {
177         if (t == Jia) ++jia;
178         else if (t == Jian) ++jian;
179         else if (t == Cheng) ++cheng;
180         else if (t == Chu) ++chu;
181         else if (t == Qian) ++qian;
182         else if (t == Hou) ++hou;
183     }
184     if (jia + jian + cheng + chu == 1) {//最后一步
185         if (jia == 1) return v[0] + v[2];
186         if (jian == 1) return v[0] - v[2];
187         if (cheng == 1) return v[0] * v[2];
188         if (chu == 1) return v[0] / v[2];
189     }
190     if (qian + hou == 0) {//无括号情况
191         if (cheng + chu == 0) {//无乘除情况
192             vector<double> vv;
193             double re = Count(v[0], v[2], v[1]);
194             vv.push_back(re);
195             for (auto i = v.begin() + 3;i != v.end();++i)
196                 vv.push_back(*i);
197             //for (auto i = vv.begin();i != vv.end();++i) cout << *i << ‘ ‘;cout << endl;//------
198             return Calculate_Single(vv);
199         }
200         else {
201             int t;
202             vector<double> vv;
203             for (t = 0;v[t] != Cheng&&v[t] != Chu;++t);
204             double re = Count(v[t - 1], v[t + 1], v[t]);
205             for (int i = 0;i < t - 1;++i)
206                 vv.push_back(v[i]);
207             vv.push_back(re);
208             for (auto i = v.begin() + t + 2;i != v.end();++i)
209                 vv.push_back(*i);
210             //for (auto i = vv.begin();i != vv.end();++i) cout << *i << ‘\t‘;cout << endl;//------
211             return Calculate_Single(vv);
212         }
213     }
214     else {//有括号
215         vector<double> vv, vvv;
216         int q, h;
217         double re;
218         for (h = 0;v[h] != Hou;++h);
219         for (q = h;v[q] != Qian;--q);
220         for (int i = q + 1;i < h;++i) vvv.push_back(v[i]);
221         re = Calculate_Single(vvv);
222         for (int i = 0;i < q;++i)vv.push_back(v[i]);
223         vv.push_back(re);
224         for (auto i = v.begin() + h + 1;i != v.end();++i)
225             vv.push_back(*i);
226 //        for (auto i = vv.begin();i != vv.end();++i) cout << *i << ‘\t‘;cout << endl;//------
227         return Calculate_Single(vv);
228     }
229     Pos(49, 0);cerr << "Bug:Calculate_Single 失败";
230     return 0;
231 }
232 
233 int main()
234 {
235     Start();
236     time_t ts, te;
237     string line;
238     int fail_flag;
239     while (1) {
240         Pos(12, 33);cout << "              ";
241         Pos(13, 36);cout << "         ";
242         Pos(14, 34);cout << "              ";
243         Pos(15, 28);cout << "                                      ";
244         Pos(16, 18);cout << "                                              ";
245         for (int i = 0;i < 4;++i) card[i] = rand() % 13 + 1;
246         for (int i = 0; i < 4; i++) {
247             char ch[10];               
248             itoa((int)card[i], ch, 10);    //
249             charexp[i] = ch;
250         }//ckb
251         Pos(5, 28);cout << card[0] << "      " << card[1] << "      " << card[2] << "      " << card[3]<<"  ";
252         time(&ts);
253         do {
254             double result;             //答案 
255             vector<double> data;       //处理数据 
256             fail_flag = 0;
257             Pos(8, 14);cout << "                                                    ";
258             Pos(8, 14);cin >> line;
259             Pos(13, 35);cout << "         ";
260             time(&te);
261             if (fail_flag = Judge(line)) {           //判断式子的正确 
262                 Pos(12, 33);cout << "              ";
263                 Pos(13, 35);cout << "         ";
264                 Pos(14, 34);cout << "              ";
265                 Pos(15, 28);cout << "                                      ";
266                 Pos(16, 18);cout << "                                              ";
267                 Pos(13, 35);cout << "输入错误!";
268                 continue; 
269             }
270             fail_flag = 0;                   //失败标志 
271             Exchange(data, line);            //处理输入数据 
272             result = Calculate_Single(data); //计算结果 
273             Pos(12, 36);
274             if (result == 24) {
275                 cout << "答案:" << 24;
276                 HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
277                 SetConsoleTextAttribute(hOutput, 10);
278                 Pos(13, 35);cout << "答案正确!  ";
279                 SetConsoleTextAttribute(hOutput, 15);
280                 Pos(14, 34);cout << "本次用时:" << te - ts << s;
281                 Pos(16, 18);cout << "                                                  ";
282                 Pos(16, 18);cout << "         要开新局,";system("pause");
283                 break;
284             }
285             else {
286                 fail_flag = 1;             //错误标志  
287                 cout << "答案:" << result; //输出自己的答案 
288                 HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);  
289                 SetConsoleTextAttribute(hOutput, 12);    //设置字体颜色为红色 
290                 Pos(13, 35);cout << "答案错误!  ";       //答案错误为红色 
291                 SetConsoleTextAttribute(hOutput, 15);    //高亮白色 
292                 Pos(14, 34);cout << "已经用时:" << te - ts << s; //计算用时 
293             }
294             Pos(16, 18);cout << "按R重新输入,按ESC开新局,按S显示所有答案。";
295             int k;
296             do {
297                 k = Key();        //根据返回值来选择操作,k==0时重新操作 
298             } while (k == 0);     
299             if (k == 1) { cin.clear(); continue; }  //重新输入---清空当前输入 
300             else if (k == 3) { cin.clear(); break; }//重开新局 
301             else if (k == 2) {                      //显示答案 
302                 Pos(17, 0);                         
303                 Pos(16, 18);cout << "                                                \n";
304                 cout << "没有答案。";
305                 Pos(17, 0);Search();     //如果有答案就能把原来“没有答案”的字符串覆盖掉了 
306                 Pos(16, 18);cout << "         要开新局,";system("pause");
307                 Pos(17, 0);cout << "                                                                                ";
308                 Pos(18, 0);cout << "                                                                                ";
309                 Pos(19, 0);cout << "                                                                                ";
310                 Pos(20, 0);cout << "                                                                                ";
311                 Pos(21, 0);cout << "                                                                                ";
312                 Pos(22, 0);cout << "                                                                                ";
313                 Pos(23, 0);cout << "                                                                                ";
314                 Pos(24, 0);cout << "                                                                                ";
315                 Pos(25, 0);cout << "                                                                                ";
316                 Pos(26, 0);cout << "                                                                                ";
317                 Pos(27, 0);cout << "                                                                                ";
318                 Pos(28, 0);cout << "                                                                                ";
319                 Pos(29, 0);cout << "                                                                                ";
320                 Pos(30, 0);cout << "                                                                                ";
321                 Pos(31, 0);cout << "                                                                                ";
322                 Pos(32, 0);cout << "                                                                                ";
323                 Pos(33, 0);cout << "                                                                                ";
324                 Pos(34, 0);cout << "                                                                                ";
325                 Pos(35, 0);cout << "                                                                                ";
326                 break;
327             }
328         } while (fail_flag);
329     }
330     return 0;
331 }

 

24点游戏_简单界面_可直接运行

标签:

原文地址:http://www.cnblogs.com/douzujun/p/5557971.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!