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

  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 }
时间: 2024-10-17 15:36:48

24点游戏_简单界面_可直接运行的相关文章

3_Jsp标签_简单标签_防盗链和转义标签的实现

一概念 1防盗链 在HTTP协议中,有一个表头字段叫referer,采用URL的格式来表示从哪儿链接到当前的网页或文件,通过referer,网站可以检测目标网页访问的来源网页.有了referer跟踪来源就好办了,这时就可以通过技术手段来进行处理,一旦检测到来源不是本站即进行阻止或者返回指定的页面. 2页面中的转义字符 在HTML中,定义转义字符串的原因有两个:第一个原因是像“<”和“>”这类符号已经用来表示HTML标签,因此就不能直接当作文本中的符号来使用.为了在HTML文档中使用这些符号,就

迷宫问题(输出路径)_友好界面_音乐播放_彩色字体

1 /* 2 8 6 3 1 1 1 1 1 1 1 1 4 1 0 0 1 0 0 1 1 5 1 1 0 0 0 0 0 1 6 1 0 0 1 0 1 0 1 7 1 0 0 0 0 0 0 1 8 1 1 1 1 1 1 1 1 9 */ 10 #include <iostream> //////////-static-libgcc 11 #include <iomanip> 12 #include <ctime> 13 #include <stack&g

解决问题1:可以从桌面显示到FORM MFC/HALCON混合编程系列一_打开图像_简单处理_

没法爱上新浪  转载:http://blog.sina.com.cn/s/blog_812e326f010110og.html 从图1到图2 图1(在桌面显示了) open_window(0,0, Width/2, Height/2,0,"visible","",&WindowHandle); 更改为: Hlong MainWndID =(Hlong) m_hWnd; open_window(0,0, Width/2, Height/2,MainWndID,

排序_简单排序_插入排序

插入排序有三个记录值,其中一个记录着取出来的一个值,也是需要插入的值,从第二个位置开始获取.另外两个初始位置是一样的,从第二个数值开始记录.这个的特点是每一次比较之前,当前假设的数组都是有序的. public class ArrayIns { private long a[]; private int nElems; public ArrayIns(int maxSize) { a=new long[maxSize]; nElems=0; } //插入 public void insert(lo

大话设计模式_简单工厂模式(Java代码)

简单的描述:一个父类.多个子类,实例化那个子类由一个单独的工厂类来进行 图片摘自大话设计模式: 运算类: 1 package com.longsheng.simpleFactory; 2 3 public class Calculate { 4 5 private double firstNum; 6 private double secondNum; 7 8 public double getFirstNum() { 9 return firstNum; 10 } 11 12 public v

iOS_10_tableView的简单使用_红楼十二钗

终于效果图: 方式1,用字典数组 BeyondViewController.h // // BeyondViewController.h // 10_tableView // // Created by beyond on 14-7-25. // Copyright (c) 2014年 com.beyond. All rights reserved. // #import <UIKit/UIKit.h> @interface BeyondViewController : UIViewContr

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

反汇编逆向实例_简单函数调用

反汇编逆向实例_简单函数调用 by:比方 逆向反汇编第一章,基本函数调用 示例代码: 1 #include"stdio.h" 2 int function(int a,int b) 3 { 4 int c=a+b; 5 return c; 6 } 7 void main() 8 { 9 function(1,2); 10 getchar(); 11 } 反汇编结果: 1 #include "stdio.h" 2 3 int function(int a,int b)

机器学习_深度学习_入门经典(永久免费报名学习)

机器学习_深度学习_入门经典(博主永久免费教学视频系列) https://study.163.com/course/courseMain.htm?courseId=1006390023&share=2&shareId=400000000398149 作者座右铭---- 与其被人工智能代替,不如主动设计机器为我们服务. 长期以来机器学习很多教材描述晦涩难懂,大量专业术语和数学公式让学生望而止步.生活中机器学习就在我们身边,谷歌,百度,Facebook,今日头条都运用大量机器学习算法,实现智能