Boolean Expressions遞歸解題

The objective of the program you are going to produce is to evaluate boolean expressions as the one shown next:
Expression: ( V | V ) & F & ( F | V )

where V is for True, and F is for False. The expressions may include the following operators: ! for not , & for and, | for or , the use of parenthesis for operations grouping is also allowed.

To perform the evaluation of an expression, it will be considered the priority of the operators, the not having the highest, and the or the lowest. The program must yield V or F , as the result for each expression in the input file.
輸入
The expressions are of a variable length, although will never exceed 100 symbols. Symbols may be separated by any number of spaces or no spaces at all, therefore, the total length of an expression, as a number of characters, is unknown.

The number of expressions in the input file is variable and will never be greater than 20. Each expression is presented in a new line, as shown below.
輸出
For each test expression, print "Expression " followed by its sequence number, ": ", and the resulting value of the corresponding test expression. Separate the output for consecutive test expressions with a new line.

Use the same format as that shown in the sample output shown below.
描述
你將產生的程序的目標是計算布爾表達式如下所示:
表達式:(V | V) & F & (F | V)
其中V代表真,F代表假。表達式可能包括以下操作符:!for not, & for and, | for or,也允許使用圓括號對操作分組。
要執行表達式的計算,它將被認爲是運算符的優先級,沒有最高、最低或最低的運算符。程序必須爲輸入文件中的每個表達式生成V或F。
輸入
表達式的長度是可變的,但不會超過100個符號。符號可以由任意數量的空格分隔,也可以完全沒有空格,因此,表達式的總長度(作爲字符的數量)是未知的。
輸入文件中的表達式數量是可變的,並且永遠不會大於20。每個表達式都顯示在一個新行中,如下所示。
輸出
對於每個測試表達式,打印“表達式”及其序列號“:”,以及相應測試表達式的結果值。用新行分隔連續測試表達式的輸出。
使用與下面示例輸出中所示相同的格式。
樣例輸入
( V | V ) & F & ( F| V)
!V | V & V & !F & (F | V ) & (!F | F | !V & V)
(F&F|V|!V&!F&!(F|F&V))
樣例輸出
Expression 1: F
Expression 2: V
Expression 3: V

這裏的名字會長一些,但看起來很好理解,一個學姐告訴我的,一個命名很重要,好的命名會加快你的代碼速度

#include<bits/stdc++.h>
using namespace std;
stack <char> stack_str;
queue <char> queue_str;
string c;

int print1(char c)
{
    if(c == '(')
        return 4;
    if(c == '!')
        return 3;
    if(c == '&')
        return 2;
    if(c == '|')
        return 1;
}

int judge(char c)
{
    if(c=='F')
        return 0;
    else
        return 1;
}

void routine()
{
    while(stack_str.size())
    {
        stack_str.pop();
    }
    for(int i=0; i<c.size(); i++)
    {
        if(c[i]!=' ')
        {
            if(c[i]=='F'||c[i]=='V')
                queue_str.push(c[i]);
            else if(c[i]=='!'&&stack_str.size()&&stack_str.top()=='!')
            {
                stack_str.pop();
            }
            else if(!stack_str.size())
                stack_str.push(c[i]);
            else if(c[i]==')')
            {
                while(stack_str.top()!='(')
                {
                    queue_str.push(stack_str.top());
                    stack_str.pop();
                }
                stack_str.pop();
                continue;
            }
            else if(print1(stack_str.top())==4||(print1(c[i])>print1(stack_str.top())))
                stack_str.push(c[i]);
            else if(print1(stack_str.top())!=4&&print1(c[i])<=print1(stack_str.top()))
            {
                queue_str.push(stack_str.top());
                stack_str.pop();
                while(stack_str.size()&&print1(stack_str.top())!=4&&print1(c[i])<=print1(stack_str.top()))
                {
                    queue_str.push(stack_str.top());
                    stack_str.pop();
                }
                stack_str.push(c[i]);
            }
        }
    }
    while(stack_str.size())
    {
        queue_str.push(stack_str.top());
        stack_str.pop();
    }
}

void value()
{
    bool r=1;
    char x,y;
    while(queue_str.size())
    {
        if(queue_str.front()=='V'||queue_str.front()=='F')
        {
            stack_str.push(queue_str.front());
            queue_str.pop();
        }
        else
        {
            if(queue_str.front()=='&')
            {
                x=stack_str.top();
                stack_str.pop();
                y=stack_str.top();
                stack_str.pop();
                r=judge(x)&&judge(y);
                if(r==1)
                    stack_str.push('V');
                else
                    stack_str.push('F');
            }
            else if(queue_str.front()=='|')
            {
                x=stack_str.top();
                stack_str.pop();
                y=stack_str.top();
                stack_str.pop();
                r=judge(x)||judge(y);
                if(r==1)
                    stack_str.push('V');
                else
                    stack_str.push('F');
            }
            else
            {
                x=stack_str.top();
                stack_str.pop();
                if(judge(x)==1)
                    stack_str.push('F');
                else
                    stack_str.push('V');
            }
            queue_str.pop();
        }
    }
}

int main()
{
    int ans=0;
    while(getline(cin,c))
    {
        ans++;
        routine();
        value();
        cout<<"Expression "<<ans<<": "<<stack_str.top()<<endl;
    }
    return 0;
} 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章