سلام من تمرین eval رو انجام دادم
طرز کار تابع myEval به این شکل است که:
input string
delete the spaces
put members in a list and put "**" instead of "*" , "*"
make the numbers
while prant in list:
find innest prant
if just number in prant
delete prants
else
solve the phrase with priroties
delete prants
solve the phrase with priroties
go to input
و کد برنامه به این شکل است:
(خروجی و ورودی توابع برای مثال با کامنت مشخص شده است)
def inputting():
inputtedString = input("Please Enter Your Math Phrase Here: ")
if inputtedString == "":
inputting()
return inputtedString
def makeList(inputtedString): # like "( 2 ** 10 ) / ( 2 * 16 )"
inputtedString = inputtedString.replace(" " , "")
inputtedList = []
for chr in inputtedString:
inputtedList.append(chr)
return inputtedList # will be ['(', '2', '*', '*', '1', '0', ')', '/', '(', '2', '*', '1', '6', ')']
def makeSign(inputtedList): # like ['(', '2', '*', '*', '1', '0', ')', '/', '(', '2', '*', '1', '6', ')'] notice the "*" , "*"
newInputtedList = []
lastChr = ""
for chr in inputtedList:
if chr == "*" and lastChr != "*":
newInputtedList.append("*")
elif chr == "*" and lastChr == "*":
newInputtedList.pop()
newInputtedList.append("**")
elif chr == "+" or chr == "-" or chr == "/" or chr == "(" or chr == ")" or chr.isdigit():
newInputtedList.append(chr)
lastChr = chr
return newInputtedList # will be ['(', '2', '**', '1', '0', ')', '/', '(', '2', '*', '1', '6', ')'] notice the "**"
def makeNumbers(newInputtedList): # like ['(', '2', '**', '1', '0', ')', '/', '(', '2', '*', '1', '6', ')'] notice the "1" , "0" and "1" , "6"
classifiedList = []
chrBefore = ""
myNum = 0
lenList = len(newInputtedList)
for chr in newInputtedList:
if chr.isdigit():
myNum = (myNum * 10) + int(chr)
if chr.isdigit() == False and chrBefore.isdigit():
classifiedList.append(myNum)
myNum = 0
if chr == "+" or chr == "-" or chr == "/" or chr == "(" or chr == ")" or chr == "**" or chr == "*":
classifiedList.append(chr)
if lenList == 1 and chr.isdigit():
classifiedList.append(myNum)
myNum = 0
chrBefore = chr
lenList -= 1
return classifiedList # will be ['(', 2, '**', 10, ')', '/', '(', 2, '*', 16, ')'] notice the 10 and 16
def solveInnest(classifiedList): # like ['(', 2, '**', 10, ')', '/', '(', 2, '*', 16, ')']
chrCount = 0
chr2Count = 0
lPrant = 0
rPrant = 0
innestPrant = []
classifiedList_copy = classifiedList.copy()
classifiedList_copy.reverse()
for chr in classifiedList:
if chr == ")":
rPrant = chrCount
for chr2 in classifiedList_copy[(len(classifiedList) - rPrant - 1) : ]:
if chr2 == "(":
lPrant = rPrant - chr2Count
innestPrant = classifiedList[lPrant + 1 : rPrant]
solvedPhrase = solvePhrase(innestPrant)
classifiedList[lPrant : rPrant + 1] = solvedPhrase #replacing
chr2Count += 1
if innestPrant != []:
break
chrCount += 1
return classifiedList # will be [ 1024, '/', '(', 2, '*', 16, ')'] this will again go to the solveInnest() in while loop
def solvePhrase(phrase): # like [ 2, "**", 10]
while "**" in phrase or "*" in phrase or "/" in phrase or "+" in phrase or "-" in phrase:
if "**" in phrase:
solveTavans(phrase)
if "*" in phrase or "/" in phrase:
solveZarbs(phrase)
if "+" in phrase or "-" in phrase:
solveJams(phrase)
return phrase # will be [1024]
def solvePrants(classifiedList):
if "(" in classifiedList:
classifiedList = solveInnest(classifiedList)
solvePrants(classifiedList)
solvedPhrase = solvePhrase(classifiedList)
else:
solvedPhrase = solvePhrase(classifiedList)
print(f"The Result is : {solvedPhrase}")
inputtedString = inputting()
myEval(inputtedString)
def solveTavans(phrase):
chrCount = 0
for chr in phrase:
if chr == "**":
optResult = phrase[chrCount - 1] ** phrase[chrCount + 1]
phrase[ chrCount - 1 : chrCount + 2] = [optResult] #replacing
chrCount += 1
solvePhrase(phrase)
def solveZarbs(phrase):
chrCount = 0
for chr in phrase:
if chr == "*":
optResult = phrase[chrCount - 1] * phrase[chrCount + 1]
phrase[ chrCount - 1 : chrCount + 2] = [optResult]
elif chr == "/":
optResult = phrase[chrCount - 1] / phrase[chrCount + 1]
phrase[ chrCount - 1 : chrCount + 2] = [optResult]
chrCount += 1
solvePhrase(phrase)
def solveJams(phrase):
chrCount = 0
for chr in phrase:
if chr == "+":
optResult = phrase[chrCount - 1] + phrase[chrCount + 1]
phrase[ chrCount - 1 : chrCount + 2] = [optResult]
elif chr == "-":
optResult = phrase[chrCount - 1] - phrase[chrCount + 1]
phrase[ chrCount - 1 : chrCount + 2] = [optResult]
chrCount += 1
solvePhrase(phrase)
def myEval(inputtedString):
inputtedList = makeList(inputtedString)
newInputtedList = makeSign(inputtedList)
classifiedList = makeNumbers(newInputtedList)
solvePrants(classifiedList)
inputtedString = inputting()
myEval(inputtedString)