简体   繁体   English

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

[英]Reading logical operators expression for truth table C++

I am trying to write a code that will take an expression that is formatted similar to 我正在尝试编写一个代码,该代码将采用类似于以下格式的表达式

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

and it needs to print out the truth table for it which looks something like 它需要为它打印出真值表,看起来像

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

i devised a way to deal with the XOR and implies operators, but I realized that it only works when the operators are inside the inner parentheses, not when the operators are between two sets of parentheses... 我设计了一种处理XOR并暗含运算符的方法,但我意识到只有在运算符位于内部括号内时才起作用,而不是在运算符位于两组括号之间时才起作用...

I have been working on this for way too many hours, can someone give me insight on what I can use instead? 我已经花了太多时间从事这项工作,有人可以让我了解我可以使用什么吗?

Now that I think about it more, could I just make try to read in the first (), then the second () and so forth as individual variables, convert them to booleans the computer will understand, and then inject those back into the proper format in the right order? 现在,我考虑得更多了,我可以试着先读取第一个(),然后读取第二个()等等,作为单个变量,将它们转换为计算机可以理解的布尔值,然后再将其注入适当的值中。格式正确吗?

The source code I am working on right now is 我现在正在处理的源代码是

#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++;

    }

}

Have you considered a recursive descent parser? 您是否考虑过递归下降解析器? http://en.wikipedia.org/wiki/Recursive_descent_parser http://en.wikipedia.org/wiki/Recursive_descent_parser

Of possibly some other sort of parsing implementation. 其他可能的解析实现方式。 Then you can generate a tree which represents the expression and next you can evaluate the tree for all the possible inputs. 然后,您可以生成代表该表达式的树,然后可以为所有可能的输入求值该树。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM