Ads

Ads

Translate

Friday 9 November 2012

Simple Delphi Calculator

By Fajar  |  November 09, 2012 No comments


 Pascal is readable enough that browsing the code will be a more understandable description of the process than anything I can write in natural language.  Just recognize that Windows is a messaging system so typically programs just hang out doing nothing until they get a message.  When you see routines like DigitBtnClickPlusBtnClick, etc.  that routine is the one that was called when that button was clicked.  

There is not much difference in processing the numeric keys, so I wrote an AddDigit procedure to display the digit and add in on to the string version of the number being built.   Similarly, most of the operation handling is similar, as long as we remember to add, subtract, multiply or divide when the time comes.   So we have a HandleOp procedure for those things.  Note that we can't really do anything when the user enters the operation because we don't have the second value yet.  So, we'll just hold on to it until we need it.   We also know the the user has finished entering a value when we see the operation code, so we can process it according the operator that preceded it and get ready to build the next number.   

There's a Reset procedure that's called to clear out the displays and reset internal flags, etc.  We have to do this on initial input, or if the user presses Clear, or when the user presses the next digit after he has pressed the = key.
The program has 55 or so user written lines of code, more than I would have guessed.   But there area few tricky things.  


We need to keep the user from entering more than one decimal point in the number.

We need to handle the display intelligently when * or / are mixed with + or -.  In your normal calculator and in this one, if you enter 1, +, 2, *, 3, the result is 9  because 1+2 = 3 and 3* 3 is 9.  But because of the precedence rules of arithmetic, multiplication and division in expressions are performed before additions ,and subtractions, so 1+2*3 written as an expression should = 7.  In this program, we solve the problem by inserting parentheses in the expression, so the above example is displayed as (1+2)*3=9. 

To reduce the number of lines of code, I used a trick that I probably would not use in a more complex project:  Rather than have 10 different routines to process the 0 to 9 buttons, I named them Btn0, Btn1, Btn2, etc and extract the 4th character of the name as the desired digit  in a common routine. 
I think that should explain enough of the program logic, so go ahead and take look.

Running/Exploring the Program 


Download source

Download  executable

Author: Fajar

Hello, I am Author, decode to know more: In commodo magna nisl, ac porta turpis blandit quis. Lorem ipsum dolor sit amet, consectetur adipiscing elit. In commodo magna nisl, ac porta turpis blandit quis. Lorem ipsum dolor sit amet.

0 comments:

Best Post This Year

Install Fortesreport community Delphi 7 dan RX Berlin

Download  Pertama2 kita harus punya file installernya terlebih dahulu, download  https://github.com/fortesinformatica/fortesrepo...

Total Pageviews

© 2014 Fajar Priyadi. WP themonic converted by Bloggertheme9. Published By Gooyaabi Templates | Powered By Blogger
TOP