- // Calc24.cpp : Defines the entry point for the console application.
- // 算24小游戏。
- #include "stdafx.h"
- #include<iostream>
- #include <ctime>
- #include <string>
- #include <set>
- #include <list>
- #include <stack>
- #include <algorithm>
- using namespace std;
- stack< int > sum;
- stack< char> opr;
- int Precedence(char sign)
- {
- switch(sign)
- {
- case '+':
- case '-':
- return 1;
- case '*':
- case '/':
- case '%':
- return 2;
- case '^':
- return 3;
- case '(':
- case '@':
- default:
- return 0;
- }
- }
- void Change(const char* s1,char* s2)
- {
- stack<char> T;
- int i=0,j=0;
- char ch;
- while(!T.empty())
- T.pop();
- T.push('@');
- ch=s1[i];
- while(ch!='\\0')
- {
- if(ch==' ')
- ch=s1[++i];
- else if(ch=='(')
- {
- T.push(ch);
- ch=s1[++i];
- }
- else if(ch==')')
- {
- while(T.top() != '(')
- {
- s2[j++]=T.top();
- T.pop();
- }
- T.pop();
- ch=s1[++i];
- }
- else if(ch=='+' || ch=='-' ||ch=='*' ||ch=='/' ||ch=='^' ||ch=='%')
- {
- char w=T.top();
- while(Precedence(w)>=Precedence(ch))
- {
- s2[j++]=w;
- T.pop();
- w=T.top();
- }
- T.push(ch);
- ch=s1[++i];
- }
- else
- {
- while((ch>='0' && ch<='9')||ch=='.')
- {
- s2[j++]=ch;
- ch=s1[++i];
- }
- s2[j++]=' ';
- }
- }
- ch=T.top();
- while(ch!='@')
- {
- s2[j++]=ch;
- T.pop();
- ch=T.top();
- }
- s2[j++]='\\0';
- }
- void calc()
- {
- if (opr.empty())
- {
- return;
- }
- char c = opr.top();
- opr.pop();
- int b = sum.top();
- sum.pop();
- int a = sum.top();
- sum.pop();
- switch (c)
- {
- case '+':
- a += b;
- break;
- case '-':
- a -= b;
- break;
- case '*':
- a *= b;
- break;
- case '/':
- a /= b;
- break;
- }
- sum.push(a);
- }
- bool isV(string s, int a, int b, int c, int d)
- {
- set<char> fh;
- fh.insert('+');
- fh.insert('-');
- fh.insert('*');
- fh.insert('/');
- fh.insert('(');
- fh.insert(')');
- fh.insert('\\0');
- list<int> data;
- data.push_back(a);
- data.push_back(b);
- data.push_back(c);
- data.push_back(d);
- char ch, cl = '+' ;
- int sums = 0;
- int x;
- sum.push(0);
- char buf[100], *p=buf, *q= buf;
- memset(buf,0,100);
- sprintf(buf, "%s", s.c_str());
- while(*p == ' ')p++;
- q = p;
- while (*p)
- {
- ch = *q;
- if (fh.find(ch) != fh.end())
- {
- opr.push(ch);
- calc();
- p = q+1;
- }
- else if (*q == ' ')
- {
- *q = 0;
- x= atoi(p);
- list<int>::iterator pe = find(data.begin(),data.end(),x);
- if (pe == data.end())
- {
- return false;
- }
- data.erase(pe);
- sum.push(x);
- p = q+1;
- }
- q++;
- }
- sums = sum.top();
- if (sums == 24)
- {
- return true;
- }
- return false;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- cout<<"欢迎来到24点"<<endl;
- char s = 'Y';
- while (s == 'Y'|| s == 'y')
- {
- srand(time(0));
- int a = rand() % 10 + 1;
- int b = rand() % 10 + 1;
- int c = rand() % 10 + 1;
- int d = rand() % 10 + 1;
- while(!sum.empty()) sum.pop();
- while(!opr.empty()) opr.pop();
- cout<<a<<'\\t'<<b<<'\\t'<<c<<'\\t'<<d<<endl;
- cout<<"请输入运算方法"<<endl;
- string vs ;
- cin >> vs;
- char buf[100];
- memset(buf,0,100);
- Change(vs.c_str(), buf);
- if (isV(buf, a, b, c, d))
- {
- cout<<"方法正确 OK"<<endl;
- }
- else
- {
- cout<< "此计算不正确" << endl;
- }
- cout<<"是否继续(y/n):";
- cin >> s;
- }
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/040920135605.html
来源: http://www.codesnippet.cn/detail/040920135605.html