24点游戏

来源:互联网 发布:信长野望长野业正数据 编辑:程序博客网 时间:2024/05/16 19:33

输入四个数1-13,加减乘除得到24:

#include <iomanip>#include<string>#include <vector> #include"cal.h"using namespace std;bool isp(char c){if (c == '+' || c == '-' || c == '*' || c == '/')return true;else return false;}void find_it(string s);double cacul(string s);void swap(vector<int> &lst, int i, int j){int tmp = lst[i];lst[i] = lst[j];lst[j] = tmp;}void perm(vector<int> &lst, int start, int end, vector<vector<int> > &dst){if (start >= end) {dst.push_back(lst);}else {for (int i = start; i<end; i++) {swap(lst, start, i);perm(lst, start + 1, end, dst);swap(lst, start, i);}}}int main(){char a[4] = { '+', '-', '*', '/' };int n1;cout << "请输入四张牌:" << endl;string temp;char op1, op2, op3;vector<vector<int> > dst;vector<int> lst;for (int i = 0; i<4; i++) {cin >> n1;lst.push_back(n1);}perm(lst, 0, 4, dst);for (int i = 0; i < (int)dst.size(); i++){for (int j = 0; j < 4; j++){op1 = a[j];for (int k= 0; k < 4; k++){op2 = a[k];for (int t = 0; t < 4; t++){op3= a[t];temp+=to_string(dst[i][0]);temp.push_back(op1);temp += to_string(dst[i][1]);temp.push_back(op2);temp += to_string(dst[i][2]);temp.push_back(op3);temp += to_string(dst[i][3]);find_it(temp);temp = "";}}}}system("pause");return 0;}void find_it(string s){vector<int>ii;for (int i = 0; i < (int)s.size(); i++){if (isp(s[i]))ii.push_back(i);}vector<string>ss;string temp=s;ss.push_back(temp); temp = s;temp.insert(0, 1, '(');temp.insert(ii[1]+1, 1, ')');temp.insert(ii[1]+3, 1, '(');temp.insert(temp.size(), 1, ')');ss.push_back(temp); temp = s;temp.insert(0, 1, '(');temp.insert(ii[1]+1, 1, ')');ss.push_back(temp); temp = s;temp.insert(ii[1]+1, 1, '(');temp.insert(temp.size(), 1, ')');ss.push_back(temp); temp = s;temp.insert(0, 1, '(');temp.insert(ii[2]+1, 1, ')');ss.push_back(temp); temp = s;temp.insert(ii[0]+1, 1, '(');temp.insert(temp.size(), 1, ')');ss.push_back(temp); temp = s; temp.insert(ii[0] + 1, 1, '(');temp.insert(ii[2]+1, 1, ')');ss.push_back(temp);bool tt;for (int i = 0; i < 7; i++){tt=(24== cacul(ss[i]));if(tt)cout << ss[i] << endl;}}
cal.h

#include <cstdlib>#include <iomanip>#include<string>#include<vector>#include<stack>using namespace std;const int MAX = 50;struct node{double i;char c;bool t;};bool isdigit(char c){if ((c >= '0'&&c <= '9') || c == '.')return true;elsereturn  false;}int compare(char stack_o, char infix_o){//在中序表示法队列和暂存堆栈中,运算符的优先级表,//其优先级值为INDEX/2                               char infix_priority[9];char stack_priority[8];int index_s = 0, index_i = 0;infix_priority[0] = 'q'; infix_priority[1] = ')';infix_priority[2] = '+'; infix_priority[3] = '-';infix_priority[4] = '*'; infix_priority[5] = '/';infix_priority[6] = '^'; infix_priority[7] = ' ';infix_priority[8] = '(';stack_priority[0] = 'q'; stack_priority[1] = '(';stack_priority[2] = '+'; stack_priority[3] = '-';stack_priority[4] = '*'; stack_priority[5] = '/';stack_priority[6] = '^'; stack_priority[7] = ' ';while (stack_priority[index_s] != stack_o)index_s++;while (infix_priority[index_i] != infix_o)index_i++;return ((int)(index_s / 2) >= (int)(index_i / 2) ? 1 : 0);}//中序转前序的方法vector<node> infix_to_postfix(string s){vector<node>temp;node a;char infix_q[MAX]; //用来存放中序表示法的队列  //运算符优先级的比较,若输入运算符的优先级小于堆栈中运算符的优先级,//则返回值为1,否则为0                   for (int i = 0; i<MAX; i++)infix_q[i] = '\0';for (int i = 0; i < (int)s.size(); i++)infix_q[i] = s[i];int rear = 0, top = 0, flag = 0, i = 0;char stack_t[MAX];for (i = 0; i<MAX; i++)stack_t[i] = '\0';i = 0;while (infix_q[i] != '\0'){i++;rear++;}infix_q[rear] = 'q';  //在队列加入q为结束符号stack_t[top] = 'q';  //在堆栈加入q为结束符号                                               for (flag = 0; flag <= rear; flag++){switch (infix_q[flag]){//输入为)时,则输出堆栈内的运算符,直到堆栈内为(case ')':while (stack_t[top] != '('){a.t = false;a.c = stack_t[top--];temp.push_back(a);}top--;break;//输入为q,则将堆栈内还未输出的运算符输出case 'q':while (stack_t[top] != 'q'){a.t = false;a.c = stack_t[top--];temp.push_back(a);}break;//输入为运算符,若其优先级小于TOP在堆栈中所指运算符的优先级,//则将堆栈所指运算符输出,若优先级大于等于TOP在堆栈中所指运算符//的优先级,则将输入的运算符压入堆栈。 case '(':case '^':case '*':case '/':case '+':case '-':while (compare(stack_t[top], infix_q[flag]) == 1){a.t = false;a.c = stack_t[top--];temp.push_back(a);}stack_t[++top] = infix_q[flag];break;//输入为操作数,则直接输出default:{a.t = true;string s;for (int i = flag; isdigit(infix_q[flag]); flag++){s.push_back(infix_q[flag]);}flag--;double te = stod(s, 0);a.i = te;temp.push_back(a);}break;}}return temp;}double caculate(vector<node>a);double caculate(vector<node>a){double t = 0;stack<node>b;for (int i = 0; i < (int)a.size(); i++){if (a[i].t)b.push(a[i]);else{double t1, t2;node temp1, temp2;char c = a[i].c;temp2 = b.top();b.pop();temp1 = b.top();b.pop();t1 = temp1.i; t2 = temp2.i;switch (c){case '+':t1 = t1 + t2; break;case '-':t1 = t1 - t2; break;case '*':t1 = t1 * t2; break;case '/':t1 = t1 / t2; break;default:break;}temp1.i = t1;b.push(temp1);}}node ii = b.top();t = ii.i;return t;}double cacul(string s){vector<node>temp=infix_to_postfix(s);double t = caculate(temp);return t;}





原创粉丝点击