二十四點遊戲

來源:互聯網
上載者:User

標籤:out   cin.get   遊戲   nal   base   push   top   turn   highlight   

#include<iostream>#include<string.h>#include<string>#include<stack>using namespace std;bool lessthan(char a, char b){    if(b==‘*‘ || b==‘/‘ )    {        return true;    }    if(a==‘+‘ || a==‘-‘ )    {        return true;    }    return false;}void compute(stack<int> & s_num, char op){    int b = s_num.top();    s_num.pop();    int a = s_num.top();    s_num.pop();    if(op==‘*‘)    {        s_num.push(a*b);    }    else if(op==‘/‘)    {        s_num.push(a/b);    }    else if(op==‘+‘)    {        s_num.push(a+b);    }    else if(op==‘-‘)    {        s_num.push(a-b);    }    else    {    }}int expression(char *str, int num){    int res = 0;    stack<int> s_num;    stack<char> s_op;    for(int i=0; i<num; i++)    {        if(str[i]>=‘0‘ && str[i]<=‘9‘)        {            int tmp = 0;            int base = 1;            tmp = base*(str[i]-‘0‘);            while(str[i+1]>=‘0‘ && str[i+1]<=‘9‘)            {                tmp = tmp*10 + (str[i+1]-‘0‘);                i++;            }            s_num.push(tmp);        }        else if(str[i] == ‘(‘)        {            s_op.push(str[i]);        }        else if(str[i] == ‘[‘)        {            s_op.push(str[i]);        }        else if(str[i] == ‘{‘)        {            s_op.push(str[i]);        }        else if(str[i] == ‘)‘)        {            char tmp = s_op.top();            while(tmp!=‘(‘)            {                s_op.pop();                //                compute(s_num, tmp);                tmp = s_op.top();            }            s_op.pop();        }        else if(str[i] == ‘]‘)        {            char tmp = s_op.top();            while(tmp!=‘[‘)            {                s_op.pop();                //                compute(s_num, tmp);                tmp = s_op.top();            }            s_op.pop();        }        else if(str[i] == ‘}‘)        {            char tmp = s_op.top();            while(tmp!=‘{‘)            {                s_op.pop();                //                compute(s_num, tmp);                tmp = s_op.top();            }            s_op.pop();        }        //*/+-        else if( str[i]==‘-‘ )        {            if( i==0 || str[i-1]==‘(‘ ||str[i-1]==‘{‘ ||str[i-1]==‘[‘ ||str[i-1]==‘+‘ ||str[i-1]==‘-‘ ||str[i-1]==‘*‘ ||str[i-1]==‘/‘ )            {                i++;                if(str[i]>=‘0‘ && str[i]<=‘9‘)                {                    int tmp = 0;                    int base = 1;                    tmp = base*(str[i]-‘0‘);                    while(str[i+1]>=‘0‘ && str[i+1]<=‘9‘)                    {                        tmp = tmp*10 + (str[i+1]-‘0‘);                        i++;                    }                    s_num.push(0-tmp);                }                else                {                    cout<<"error"<<endl;                }            }            else            {                while(!s_op.empty())                {                    char tmp = s_op.top();                    if( tmp!=‘(‘ && tmp!=‘[‘ && tmp!=‘{‘ && lessthan(str[i], tmp))                    {                        s_op.pop();                        //                        compute(s_num, tmp);                    }                    else                    {                        break;                    }                }                s_op.push(str[i]);            }        }        else if(str[i]==‘*‘ || str[i]==‘/‘ || str[i]==‘+‘)        {            while(!s_op.empty())            {                char tmp = s_op.top();                if( tmp!=‘(‘ && tmp!=‘[‘ && tmp!=‘{‘ && lessthan(str[i], tmp))                {                    s_op.pop();                    //                    compute(s_num, tmp);                }                else                {                    break;                }            }            s_op.push(str[i]);        }        else        {        }    }    //final    while(!s_op.empty())    {        char tmp = s_op.top();        compute(s_num, tmp);        s_op.pop();    }    return s_num.top();}void solve(int count, string *four, string *plusx, bool &flag){    string fuhao[4] = {"+","-", "*", "/"};    if(flag)    {        return;    }    else    {        if(count == 3)        {            string total;            if(plusx[0]=="/" && plusx[1]=="*" && plusx[2]=="*")// /**            {                total = four[0]  + plusx[1] + four[2] + plusx[2] + four[3]+ plusx[0] + four[1];            }            else if(plusx[0]=="/" && plusx[1]=="*")            {                total = four[0]  + plusx[1] + four[2] + plusx[0] + four[1]+ plusx[2] + four[3];            }            else if(plusx[1]=="/" && plusx[2]=="*")            {                total = four[0]  + plusx[0] + four[1]+ plusx[2] + four[3]+ plusx[1] + four[2] ;            }            else            {                total = four[0]  + plusx[1] + four[1]+ plusx[2]+ four[2] + plusx[0]  + four[3];            }            char expr[100];            int len = total.length();            for(int i=0; i<len; i++)            {                expr[i] = total[i];            }            expr[len] = ‘\0‘;            //cout<<expr<<"=";            float tmp = expression(expr, len);            //cout<<tmp<<endl;            if(tmp - 24 < 0.001 && tmp - 24>-0.001)            {                flag = true;            }            else            {            }        }        else        {            for(int i=0; i<4; i++)            {                if(flag==false)                {                    plusx[count] = fuhao[i];                    solve(count+1, four, plusx, flag);                }            }        }    }    if(count==0 && flag==false)    {        cout<<"NONE";    }}string trans(string str){    if(str=="J")    {        str="11";        return str;    }    else if(str=="Q")    {        str="12";        return str;    }    else if(str=="K")    {        str="13";        return str;    }    else if(str=="A")    {        str="1";        return str;    }    else    {        return str;    }}int main(){/*    char str[100];    cin.get(str, 100);    int N = strlen(str);    cout<<expression(str, N)<<endl;    cout<<"true"<<endl;*/    string four[4];    string tran[4];    for(int i=0; i<4; i++)    {        cin>>four[i];    }    for(int i=0; i<4; i++)    {        if(four[i]=="joker" || four[i]=="JOKER")        {            cout<<"ERROR";            return 0;        }        else        {            tran[i] = trans(four[i]);        }    }    string plusx[3];    bool flag = false;    solve(0, tran, plusx, flag);    if(flag)        cout<<four[0]  + plusx[1] + four[1]+ plusx[2]+ four[2] + plusx[0]  + four[3];    return 0;}

  

二十四點遊戲

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.