Jump to content

Polish Notation Equation File Calculator


G+_Darryl Medley
 Share

Recommended Posts

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

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

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

 Share

×
×
  • Create New...