Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Advanced Polish Notation file calculation program
- # Written on 04/28/2014 by Darryl Medley for Coding 101 on the TWiT network
- # This advanced version calculates expressions of as many terms as memory allows
- # This program reads a text file called pnequation_list.txt that is in the same folder
- # as the .py program file. The file must contain one expression per line and be in
- # Polish (aka prefix) notation. Example: +, 12.5, 24.3
- # See end of the program for a sample file
- # Polish Notation Calculation function - based on the sample code in Wikipedia
- # Parameters:
- # termsList = list containing the expression to evaluate, 1 term per list element
- # pncErrMsg = error message list. Pass in an empty list with the same name
- # returns the result if calc is successful, 0 otherwise
- def PNCalc(termsList, pncErrMsg):
- oprnd1 = 0.0
- oprnd2 = 0.0
- stk = [] # Stack list
- # evaluate in reverse order so we can use a simpler code
- for oper in reversed(termsList):
- if oper in "+-*/%":
- # if an operator is found the stack should have a least 2 operands to calc
- if len(stk) < 2:
- pncErrMsg.append("Invalid Polish Notation Expression")
- return 0
- try:
- oprnd1 = float(stk.pop()) # pop operands off of the stack
- oprnd2 = float(stk.pop())
- if (oper in "/%") and (oprnd2 == 0.0):
- pncErrMsg.append("Division by Zero")
- return 0
- else:
- # calculate and put result on the stack
- stk.append(eval(str(oprnd1)+" "+oper+" "+str(oprnd2)))
- except:
- pncErrMsg.append("Invalid Polish Notation Expression")
- return 0
- else: # must be an operand (number)
- stk.append(oper) # put on the stack
- return stk.pop() # the final result is the only thing left on the stack
- # Begin main program
- print "Advanced Polish (prefix) Notation Equation File Processor"
- fname = "./pnequation_list.txt"
- try:
- eqlstFile = open(fname, "r") # open the text file
- except:
- print "Error: File",fname,"does not exist"
- else:
- # The first thing we do is read the file into a Python list. Each line in
- # the file becomes a string element in the list. .splitlines() removes the
- # \n (newline) character from the end of each line.
- eqList = eqlstFile.read().splitlines() # read file into list and strip newline characters
- eqlstFile.close() # We're done with the file now that the lines are in a list
- # Process each equation in the list
- for eqLine in eqList:
- if eqLine.strip(): # skip blank lines - True if eqLine is not empty
- # Parse the equation string into operator and operands by creating a little list using split()
- opList = [s.strip() for s in eqLine.split(',')] # strip blanks and parse sting into a list
- pncErrMsg = []
- calcResult = PNCalc(opList, pncErrMsg)
- if not pncErrMsg: # pncErrMsg is empty. No errors were found with the equation line
- print eqLine,"=",calcResult
- else:
- print eqLine,"= Error:",pncErrMsg[0]
- print
- raw_input("(press Enter to close)")
- # Sample Test File (save as pnequation_list.txt):
- # +, 12.5, 24.3
- # x, 1, 2
- # -, 23, zz
- # +, 1
- # *, 7, 19
- #
- # /, 33.0, 5.0
- # /, 33, 0
- # %, 5, 3
- # -, *, /, 15, -, 7, +, 1, 1, 3, +, 2, +, 1, 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement