读取真值表C ++的逻辑运算符表达式

时间:2015-01-27 04:57:52

标签: c++ boolean operators parentheses truthtable

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

的表达式
(((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++;

    }

}

1 个答案:

答案 0 :(得分:0)

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

可能还有其他一些解析实现。然后,您可以生成一个表示表达式的树,然后您可以为所有可能的输入计算树。