मैं वर्तमान में जावा से पायथन तक संक्रमण कर रहा हूं और एक कैलकुलेटर बनाने की कोशिश करने के कार्य को ले लिया है जो सिम्पी जैसे कस्टम मॉड्यूल का उपयोग किये बिना इंफिक्स-नोटेड गणितीय अभिव्यक्तियों () पर प्रतीकात्मक संचालन कर सकता है। वर्तमान में, यह उन तारों को स्वीकार करने के लिए बनाया गया है जो अंतरिक्ष सीमित हैं और केवल (,), +, -, *, और/ऑपरेटरों को ही कर सकते हैं। दुर्भाग्य से, मैं प्रतीकात्मक अभिव्यक्ति को सरल बनाने के लिए मूल एल्गोरिदम नहीं समझ सकता।मैं एक समारोह अजगर 2.7 में प्रतीकात्मक गणना किया जाता है कि लिख सकते हैं?
उदाहरण के लिए, स्ट्रिंग '2 * ((9/6) + 6 * x)', मेरे कार्यक्रम के लिए निम्न चरणों को पूरा करना चाहिए दिया:
- 2 * (1,5 + 6 * x)
- 3 + 12 * एक्स
लेकिन जब 2.
इसके अलावा वितरण मैं इस कार्यक्रम एक्स अनदेखी करने के लिए नहीं मिल सकता है, मैं कैसे संभाल कर सकते हैं 'एक्स * 6/एक्स' तो यह रिटर्न ' 6 'सरलीकरण के बाद?
संपादित करें: "प्रतीकात्मक" द्वारा स्पष्ट करने के लिए मेरा मतलब था कि यह शेष गणना करने के दौरान आउटपुट में "ए" और "एफ" जैसे अक्षरों को छोड़ देगा।
संपादित करें 2: मैं (अधिकतर) कोड समाप्त हो गया। अगर मैं भविष्य में इस पोस्ट पर ठोकर खा रहा हूं, या आप में से कोई भी उत्सुक था, तो मैं इसे यहां पोस्ट कर रहा हूं।
def reduceExpr(useArray):
# Use Python's native eval() to compute if no letters are detected.
if (not hasLetters(useArray)):
return [calculate(useArray)] # Different from eval() because it returns string version of result
# Base case. Returns useArray if the list size is 1 (i.e., it contains one string).
if (len(useArray) == 1):
return useArray
# Base case. Returns the space-joined elements of useArray as a list with one string.
if (len(useArray) == 3):
return [' '.join(useArray)]
# Checks to see if parentheses are present in the expression & sets.
# Counts number of parentheses & keeps track of first (found.
parentheses = 0
leftIdx = -1
# This try/except block is essentially an if/else block. Since useArray.index('(') triggers a KeyError
# if it can't find '(' in useArray, the next line is not carried out, and parentheses is not incremented.
try:
leftIdx = useArray.index('(')
parentheses += 1
except Exception:
pass
# If a KeyError was returned, leftIdx = -1 and rightIdx = parentheses = 0.
rightIdx = leftIdx + 1
while (parentheses > 0):
if (useArray[rightIdx] == '('):
parentheses += 1
elif (useArray[rightIdx] == ')'):
parentheses -= 1
rightIdx += 1
# Provided parentheses pair isn't empty, runs contents through again; else, removes the parentheses
if (leftIdx > -1 and rightIdx - leftIdx > 2):
return reduceExpr(useArray[:leftIdx] + [' '.join(['(',reduceExpr(useArray[leftIdx+1:rightIdx-1])[0],')'])] + useArray[rightIdx:])
elif (leftIdx > -1):
return reduceExpr(useArray[:leftIdx] + useArray[rightIdx:])
# If operator is + or -, hold the first two elements and process the rest of the list first
if isAddSub(useArray[1]):
return reduceExpr(useArray[:2] + reduceExpr(useArray[2:]))
# Else, if operator is * or /, process the first 3 elements first, then the rest of the list
elif isMultDiv(useArray[1]):
return reduceExpr(reduceExpr(useArray[:3]) + useArray[3:])
# Just placed this so the compiler wouldn't complain that the function had no return (since this was called by yet another function).
return None
मुझे लगता है कि आप Sympy के आधार पर देखने के लिए :-) मुझे लगता है कि आप, गणित भाव के लिए एक पूर्ण पुनरावर्ती वंश पार्सर के निर्माण पर देख रहे हैं के लिए हल करने के लिए डेटा पेड़ के हेरफेर के बाद शुरू कर रहे हैं एक्स – wberry
हाँ !!! आप कर सकते हैं;) – Drakosha
@ li.davidm यह अभी भी तर्क चरणों में है। मैं समझ नहीं सकता कि पहले ठोकरें ब्लॉक को कैसे कार्यान्वित किया जाए। – Edwin