G+_Darryl Medley Posted April 25, 2014 Share Posted April 25, 2014 Polish Notation Equation File Calculator. https://github.com/Darryl-Medley/Coding-101/blob/master/FileCalcDM.py Here's a program that reads a text file containing simple equations in Polish (prefix) notation format (just to make it interesting). Polish notation expressions have the format: Operator Operand1 Operand2. For example "+ 1 2" is the same as "1 + 2". The program reads a file called equation_list.txt that is in the same folder as the .py file. There should be one equation per line with the format: Operator, Operand1, Operand2. The program has full error checking so put bad values in the file for fun. Here's my test file: +, 12.5, 24.3 x, 1, 2 -, 23, 17 +, 1 *, 7, 19 /, 33.0, 5.0 /, 33, 0 %, 5, 3 Note: Valid operators are +, -, *, /, and % https://github.com/Darryl-Medley/Coding-101/blob/master/FileCalcDM.py Link to comment Share on other sites More sharing options...
G+_Shannon Morse Posted April 25, 2014 Share Posted April 25, 2014 It worked! This is exactly what we are looking for for Ep 15. I'll share this on the show :) Link to comment Share on other sites More sharing options...
G+_Darryl Medley Posted April 28, 2014 Author Share Posted April 28, 2014 Thanks for trying my program Shannon! Since one of my programs was featured on the show a few weeks ago I won't be offended if you change your mind and feature some others. I wrote this one primarily to show how to process a file with multiple records, which is extremely useful, as opposed to just a single record which is of limited use. I'm been a professional coder for 30 years so if you (or anyone else in the Community) would like any help, program suggestions, etc., feel free to ask. Link to comment Share on other sites More sharing options...
G+_Darryl Medley Posted April 29, 2014 Author Share Posted April 29, 2014 Advanced Version: http://pastebin.com/tUg1sdgc The purpose of my original program was to show beginners how to process a multi-line file. The simple Polish Notation calc logic was just an afterthought. Since I'm in the habit of suggesting improvements to other people's code, I thought it was only fair to do the same to myself. So I've created a new version that shows how I would "really" write it. It has a stack-based function that evaluates Polish Notation expressions of almost unlimited complexity. Link to comment Share on other sites More sharing options...
Recommended Posts