2015-01-09 29 views
0

我发现了一些我正在编写的代码的问题;当我从我的JSON文件中提取数据并将其置于POSTFIX顺序时,遇到诸如Power,Sum,IF,NOT之类的函数时,它不会将它们置于正确的后缀顺序中。 我一直在试图解决这个问题,但我无法理解如何解决这个问题。 这里是JSON文件的一些例子:来自JSON文件的Python Postfix订单

sum(3+4*3*2+2,1,2,3)

{ 
    "arguments": [{ 
      "rightArgument": { 
       "value": "2", 
       "type": "constant" 
      }, 
      "leftArgument": { 
       "rightArgument": { 
        "rightArgument": { 
         "value": "2", 
         "type": "constant" 
        }, 
        "leftArgument": { 
         "rightArgument": { 
          "value": "3", 
          "type": "constant" 
         }, 
         "leftArgument": { 
          "value": "4", 
          "type": "constant" 
         }, 
         "type": "operation", 
         "operator": "*" 
        }, 
        "type": "operation", 
        "operator": "*" 
       }, 
       "leftArgument": { 
        "value": "3", 
        "type": "constant" 
       }, 
       "type": "operation", 
       "operator": "+" 
      }, 
      "type": "operation", 
      "operator": "+" 
     }, { 
      "value": "1", 
      "type": "constant" 
     }, { 
      "value": "2", 
      "type": "constant" 
     }, { 
      "value": "3", 
      "type": "constant" 
     }], 
    "name": "sum", 
    "type": "function" 
} 

又如,power(2,3)+not(2=1)

{ 
    "rightArgument": { 
     "arguments": [{ 
       "rightArgument": { 
        "value": "1", 
        "type": "constant" 
       }, 
       "leftArgument": { 
        "value": "2", 
        "type": "constant" 
       }, 
       "type": "operation", 
       "operator": "=" 
      }], 
     "name": "not", 
     "type": "function" 
    }, 
    "leftArgument": { 
     "arguments": [{ 
       "value": "2", 
       "type": "constant" 
      }, { 
       "value": "3", 
       "type": "constant" 
      }], 
     "name": "power", 
     "type": "function" 
    }, 
    "type": "operation", 
    "operator": "+" 
} 

最后:IF(1,IF(1,2,3),A1)

{ 
    "arguments": [{ 
      "value": "1", 
      "type": "constant" 
     }, { 
      "arguments": [{ 
        "value": "1", 
        "type": "constant" 
       }, { 
        "value": "2", 
        "type": "constant" 
       }, { 
        "value": "3", 
        "type": "constant" 
       }], 
      "name": "IF", 
      "type": "function" 
     }, { 
      "cell": "A1", 
      "value": "", 
      "type": "cell" 
     }], 
    "name": "IF", 
    "type": "function" 
} 

正如你所看到的我必须迎合很多公式

目前我的代码存储的数学问题转化为一个字符串的不同格式是这样的:

def _getCurrentOperator(data): 
    if data["type"] == "operation": 

     _getCurrentOperator(data["rightArgument"])   
     _getCurrentOperator(data["leftArgument"]) 
     valueList.append(data["operator"]) 
    elif data["type"] == "group": 
     _getCurrentOperator(data["argument"]) 
    elif data["type"] == "function": 
     if (data["name"] == "pi"): 
      valueList.append(3.14159265359) 
     else:      
      valueList.append(data["name"]) 

     for i in range(len(data["arguments"])): 
      _getCurrentOperator(data["arguments"][i]) 
    else: 
     if (data["value"]) == '': 
      valueList.append(data["cell"]) 
     else: 
      valueList.append(float(data["value"])) 

输入数据的数学问题 的JSON表示,我相信这是所有你需要的代码复制问题。 目前它不会以非后缀顺序存储函数(power,IF,NOT),但它会按正确的顺序存储这些值。

+1

这不是一个后缀表示法。这只是一个树,每个操作都按正确的评估顺序。 – poke

+0

我真的不知道你想得到什么输出。你说你的函数将这些JSON对象变成一个字符串,但它实际上将它们存储在一个列表中。所以是正常的表达式(例如'sum(3 + 4 * 3 * 2 + 2,1,2,3)')或者后缀表示法(例如'[3,4,3',' 2,'*','+',2,'+',1,2,3,'sum']')?而且,postfix并不能真正处理带有任意数量参数的函数。 – poke

回答

0

如果你想使它成为后缀,你必须在后面加上这个函数的名字。就这样。

def _getCurrentOperator(data): 
    if data["type"] == "operation": 
     _getCurrentOperator(data["rightArgument"])   
     _getCurrentOperator(data["leftArgument"]) 
     valueList.append(data["operator"]) # here you do it right 
    ... 
    elif data["type"] == "function": 
     if (data["name"] == "pi"): 
      valueList.append(3.14159265359) 
     else:      
      for i in range(len(data["arguments"])): # first the arguments... 
       _getCurrentOperator(data["arguments"][i]) 
      valueList.append(data["name"])   # then the operation 
    else: 
     ... 

此外,你可能不应单独处理pi。恕我直言,这只是另一个零参数功能。