0

我正在尝试编写一个代码,该代码将采用格式类似于

(((p∨q)∧((q→r)⊕(p∧r)))↔(r∧q))→(p∨r)

它需要为它打印出真值表,看起来像

p q r  (p V q)   (q→r) etc... until it gets to (((p∨q)∧((q→r)⊕(p∧r)))↔(r∧q))→(p∨r)
t t t     t         t    
t t f     t         f
t f t     t         t
t f f     t         t
f t t     t         t
f t f     t         f
f f t     f         t
f f f     f         t

我设计了一种处理 XOR 和隐含运算符的方法,但我意识到它仅在运算符位于内括号内时才有效,而不是当运算符位于两组括号之间时...

我已经为此工作了太多小时,有人可以告诉我我可以使用什么来代替吗?

现在我想多了,我是否可以尝试读取第一个(),然后是第二个()等等作为单独的变量,将它们转换为计算机可以理解的布尔值,然后将它们注入正确的格式正确吗?

我现在正在处理的源代码是

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>

int main()
{
    bool p[8] = { true, true, true, true, false, false, false, false };         // Declare the init T/F values
    bool q[8] = { true, true, false, false, true, true, false, false };
    bool r[8] = { true, false, true, false, true, false, true, false };

    std::string expression;                                     
    std::cout << "Enter expression (using ^ for AND, V for OR, X for XOR, and I for Implies (-->): \n";
    std::getline(std::cin, expression);


    int i = 0;
    int j = 0;
    std::vector<std::string> exprVector;
    for (std::string::iterator it = expression.begin; it != expression.end(); it++)
    {
        if (expression.at(i) == 'p')
        {
            exprVector[i] = "p[i]";
        }
        else if (expression.at(i) == 'q')
        {
            exprVector[i] = "q[i]";
        }
        else if (expression.at(i) == 'r')
        {
            exprVector[i] = "r[i]";
        }

        else if (expression.at(i) == '(')
        {
            exprVector[i] = "(";
        }
        else if (expression.at(i) == ')')
        {
            exprVector[i] = ") ";
        }
        else if (expression.at(i) == '^')
        {
            exprVector[i] = "&&";
        }
        else if (expression.at(i) == 'V')
        {
            exprVector[i] = "||";
        }
        else if (expression.at(i) == 'X')
        {
            char temp1;
            char temp2;
            i--;
            if (exprVector[i] == "p[i]")                                        // (p XOR q)
            {
                i++;
                i++;
                if (exprVector[i] == "q[i]")
                {

                    i--;
                    //clear vector[i] from exprVector
                    i--;
                    //clear vector[i] from exprVector

                    exprVector[i] = "((p[i] || q[i]) && ((p[i] && q[i]) == false))";

                }
                else if (exprVector[i] == "r")                                  // (p XOR r)
                {
                    i--;
                    //clear vector[i] from exprVector
                    i--;
                    //clear vector[i] from exprVector

                    exprVector[i] = "((p[i] || r[i]) && ((p[i] && r[i]) == false))";
                }
            }
            else if (exprVector[i] == "q")                                      // (q XOR p)
            {
                i++;
                i++;
                if (exprVector[i] == "p")
                {
                    i--;
                    //clear vector[i] from exprVector
                    i--;
                    //clear vector[i] from exprVector

                    exprVector[i] = "((q[i] || p[i]) && ((q[i] && p[i]) == false))";

                }
                else if (exprVector[i] == "r")                                  // (q XOR r)
                {
                    i--;
                    //clear vector[i] from exprVector
                    i--;
                    //clear vector[i] from exprVector

                    exprVector[i] = "((q[i] || r[i]) && ((q[i] && r[i]) == false))";
                }
            }
            else if (exprVector[i] == "r")                                      
            {
                i++;
                i++;
                if (exprVector[i] == "p")                                       // (r XOR p)
                {
                    i--;
                    //clear vector[i] from exprVector
                    i--;
                    //clear vector[i] from exprVector

                    exprVector[i] = "((r[i] || p[i]) && ((r[i] && p[i]) == false)";
                }
                if (exprVector[i] == "q")                                       // (r XOR q)
                {
                    i--;
                    //clear vector[i] from exprVector
                    i--;
                    //clear vector[i] from exprVector

                    exprVector[i] = "((r[i] || q[i]) && ((r[i] && q[i]) == false)";
                }
            }


        }
        else if (expression.at(i) == 'I')
        {
            if (exprVector[i] == "p[i]")                                        // (p ---> q)
            {
                i++;
                i++;
                if (exprVector[i] == "q[i]")
                {

                    i--;
                    //clear vector[i] from exprVector
                    i--;
                    //clear vector[i] from exprVector

                    exprVector[i] = "((p[i] == true) || (q[i] == false)";

                }
                else if (exprVector[i] == "r")                                  // (p ---> r)
                {
                    i--;
                    //clear vector[i] from exprVector
                    i--;
                    //clear vector[i] from exprVector

                    exprVector[i] = "((p[i] == true) || (r[i] == false)";
                }
            }
            else if (exprVector[i] == "q")                                      // (q ---> p)
            {
                i++;
                i++;
                if (exprVector[i] == "p")
                {
                    i--;
                    //clear vector[i] from exprVector
                    i--;
                    //clear vector[i] from exprVector

                    exprVector[i] = "((q[i] == true) || (p[i] == false))";

                }
                else if (exprVector[i] == "r")                                  // (q ---> r)
                {
                    i--;
                    //clear vector[i] from exprVector
                    i--;
                    //clear vector[i] from exprVector

                    exprVector[i] = "((q[i] == true) || (r[i] == false))";
                }
            }
            else if (exprVector[i] == "r")
            {
                i++;
                i++;
                if (exprVector[i] == "p")                                       // (r ---> p)
                {
                    i--;
                    //clear vector[i] from exprVector
                    i--;
                    //clear vector[i] from exprVector

                    exprVector[i] = "((r[i] == true) || (p[i] == false))";
                }
                if (exprVector[i] == "q")                                       // (r ---> q)
                {
                    i--;
                    //clear vector[i] from exprVector
                    i--;
                    //clear vector[i] from exprVector

                    exprVector[i] = "((r[i] == true) || (q[i] == false))";
                }

        }
        else if (expression.at(i) == '!')
        {
            i++
            if (exprVector[i] == "p")
            {
                //clear vector[i] from exprVector
                i--;
                //clear vector[i] from exprVector
                exprVector[i] == "(p[i] == false)"
            }
            if (exprVector[i] == "q")
            {
                //clear vector[i] from exprVector
                i--;
                //clear vector[i] from exprVector
                exprVector[i] == "(p[i] == false)"
            }
            if (exprVector[i] == "r")
            {
                //clear vector[i] from exprVector
                i--;
                //clear vector[i] from exprVector
                exprVector[i] == "(p[i] == false)"
            }

        }
        else if (expression.at(i) == ' ')
        {
        }

        i++;
        j++;

    }

}
4

1 回答 1

0

您是否考虑过递归下降解析器? http://en.wikipedia.org/wiki/Recursive_descent_parser

可能是某种其他类型的解析实现。然后您可以生成一个表示表达式的树,然后您可以针对所有可能的输入评估该树。

于 2015-01-27T05:00:34.043 回答