Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 表达式树中负号的处理_C++_Expression Trees_Infix Notation - Fatal编程技术网

C++ 表达式树中负号的处理

C++ 表达式树中负号的处理,c++,expression-trees,infix-notation,C++,Expression Trees,Infix Notation,我目前正在制作一个表达式树,可以从中缀->树。然而,由于负号,我很难从树->中缀符号开始 当从expressionTree转换为中缀符号时,如何确定何时为负号加括号 例如,将1-1+1转换为中缀需要在负号后加括号 转换1-1*1+1不需要在负号后面加括号。虽然我可以,但我希望尽量减少括号的数量 我目前确定括号的算法如下: int opValue() //determines the level precedence of current node if (node is addition/sub

我目前正在制作一个表达式树,可以从中缀->树。然而,由于负号,我很难从树->中缀符号开始

当从expressionTree转换为中缀符号时,如何确定何时为负号加括号

例如,将1-1+1转换为中缀需要在负号后加括号

转换1-1*1+1不需要在负号后面加括号。虽然我可以,但我希望尽量减少括号的数量

我目前确定括号的算法如下:

int opValue() //determines the level precedence of current node
if (node is addition/subtraction)      return 0
if (node is multiplication/division)   return 1
if (node is exponentiation)            return 2
else                                   return 4 //implies it is not an operator

void printInfix()
if (leftNode is not empty)
    if (currentNode is an operator and has a greater opVal than leftNode)
        print "("
        leftNode.printInfix()
        print ")"
    else 
        leftNode.printInfix()

print currentNode

if (rightNode is not empty)
    if (currentNode is an operator and has a greater opVal than rightNode
    or if it is a subtraction operator and the next node's opVal is 0)
        print "("
        rightNode.printInfix()
        print ")"
    else 
        right.printInfix()
这种方法不适用于1-1+1+1,因为它无法正确确定何时需要括号。此表达式将转换为1-1+1+1

编辑:最后决定回到这个话题。我决定试试这个方法。如果有效,将向您报告:

if (node is multiplication/addition)
    if (left is an operator of higher precedence)   add parenthesis and output left
    else    output left

    output node

    if (right is an operator of higher precedence)  add parenthesis and output right
    else    output right
/////////Non-commutative operators//////////////////////////
else if (node is subtraction)   
    if (left is an operator of higher precedence)   add parenthesis and output left
    else if (left is not NULL)  output left     

    output node

    if (right is an operator)   add parenthesis and output right
    else    output right    
///////////////////////////////////////////////////////////
else if (node is division/power)
    if (left is an operator of higher precedence)   add parenthesis and output left
    else    output left

    output node

    if (right is an operator)   add parenthesis and output right
    else    output node
else    //it is not a operator, or it is a constant, function, or variable
    output node
return stream

你不能只看优先级,你肯定需要在1-1-1中保留括号。a+b+c怎么样?由于溢出整数类型或舍入浮点类型,它通常不等同于a+b+c。