2015-01-09 32 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。恕我直言,這只是另一個零參數功能。