Android Example Calculator - RPN Calculator

This is an attempt to create a RPN calculator (Reverse Polish Notation - as per HP calculators). You can store (STO) and recall (RCL) values of register X. It will store and retrieve last value of registers (X,Y,Z,T) when you quit and restart. If you have used HP calculators (RPN type) before then you will understand how to use various functions i.e what needs to be in the Y register and what needs to be in the X register before you use/execute certain functions (405,401)
The 4th button from the top-left toggles between DEG and RAD. Use it accordingly when executing SIN, COS, and TAN.
Version 1 and 2 differ only by additional designer scripts that was added to version 2 - it now fits on a Samsung S4 mini cell phone. The functionality of the two versions is however identical.
EDIT:
Version 3 attached. Have added a "2nd function" button that will toggle the SIN, COS, and TAN buttons to allow for ASIN, ACOS, ATAN to be calculated. Use correctly in conjunction with DEG/RAD toggle button.
Some of the other functions and their inverse functions (eg kg --> lb and lb --> kg) can be converted in a similar way whereby the code of a function and its inverse function can reside in a single button click event by toggling it with the 2nd function key. I leave the exercise up to whoever wants to do the additional changes. Added additional designer scripts that will hopefully make the views fit to various devices.

pic1.png
 

Attachments

  • JHS_RPN_CALC_V2.zip
    387 KB · Views: 784
  • JHS_RPN_CALC_V1.zip
    387 KB · Views: 657
  • JHS_RPN_CALC_V3.zip
    389.6 KB · Views: 901
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
Johan,

Looks good! It brings back memories of the HP35 and the Jupiter Ace [Forth base computer].
One question. How do I get the inverse trig functions?

Regards Roger

Hi Roger

Perhaps add an additional button that can serve as the 2nd function button and when pressed will change the description of the SIN button to ASIN (same for COS and TAN) and then add additional code that will execute ASIN, ACOS, and ATAN. Will need to add another flag/tracker to check if the 2nd function button has been pressed so that ACOS rather than COS function will be executed. And then reset the additional flag/tracker in the ACOS, ASIN, and ATAN functions. Will do some mods and then post it. But if you have it done before me then feel free to post it in this thread.

Rgds

Johan
 

Johan Schoeman

Expert
Licensed User
Longtime User

Roger Daley

Well-Known Member
Licensed User
Longtime User
Johan,

As I've only been doing this for about 6 months I am still a newbie so anything I say is probably poor quality advice but for what it's worth.
Try setting autoscale in the designer script to 1.

For the text size in the edittexts have a look at this code. This scales the text in labels but the same can apply to edittexts. The figure of 5.625 is the ratio of the width one character to the width of the label [all in pixels] in the original emulator. This is used to give a scale to obtain the same ratio in whatever device it is loaded.

Also I've seen something posted by Klaus that had a code module for "Size", can't remember what/where but could be worth a look.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Portrait")
    Dim LBLcharwidth As Int
    Dim LBLTextScale As Double
    Canvas1.Initialize(Activity)   
    LBLcharwidth = Canvas1.MeasureStringWidth("9", Typeface.DEFAULT, lblABS.TextSize)
    LBLTextScale = (lblABS.Width / LBLcharwidth) / 5.625   
   
    ' Set padding on all views to zero and reset text size in Labels.
    For Each v As View In Activity.GetAllViewsRecursive
        If v Is Button Then
             Dim jo As JavaObject = v
             jo.RunMethod("setPadding", Array As Object(0, 0, 0, 0))
        End If
       If v Is EditText Then
             Dim jo As JavaObject = v
             jo.RunMethod("setPadding", Array As Object(0, 0, 0, 0))
       End If
       If v Is Label Then
             Dim jo As JavaObject = v
             jo.RunMethod("setPadding", Array As Object(0, 0, 0, 0))
             Dim lbl As Label = v
             Dim LBLTemp As Int
             LBLTemp = lbl.TextSize * LBLTextScale
             lbl.TextSize = LBLTemp
       End If
    Next


Regards Roger

PS [Dim Canvas1 As Canvas in Sub Globals]
 

Johan Schoeman

Expert
Licensed User
Longtime User
Johan,

As I've only been doing this for about 6 months I am still a newbie so anything I say is probably poor quality advice but for what it's worth.
Try setting autoscale in the designer script to 1.

For the text size in the edittexts have a look at this code. This scales the text in labels but the same can apply to edittexts. The figure of 5.625 is the ratio of the width one character to the width of the label [all in pixels] in the original emulator. This is used to give a scale to obtain the same ratio in whatever device it is loaded.

Also I've seen something posted by Klaus that had a code module for "Size", can't remember what/where but could be worth a look.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Portrait")
    Dim LBLcharwidth As Int
    Dim LBLTextScale As Double
    Canvas1.Initialize(Activity)  
    LBLcharwidth = Canvas1.MeasureStringWidth("9", Typeface.DEFAULT, lblABS.TextSize)
    LBLTextScale = (lblABS.Width / LBLcharwidth) / 5.625  
  
    ' Set padding on all views to zero and reset text size in Labels.
    For Each v As View In Activity.GetAllViewsRecursive
        If v Is Button Then
             Dim jo As JavaObject = v
             jo.RunMethod("setPadding", Array As Object(0, 0, 0, 0))
        End If
       If v Is EditText Then
             Dim jo As JavaObject = v
             jo.RunMethod("setPadding", Array As Object(0, 0, 0, 0))
       End If
       If v Is Label Then
             Dim jo As JavaObject = v
             jo.RunMethod("setPadding", Array As Object(0, 0, 0, 0))
             Dim lbl As Label = v
             Dim LBLTemp As Int
             LBLTemp = lbl.TextSize * LBLTextScale
             lbl.TextSize = LBLTemp
       End If
    Next


Regards Roger

PS [Dim Canvas1 As Canvas in Sub Globals]

Thanks Roger. Have added a whole lot of additional designer scripts that will hopefully solve the problem. Also used your sample code (and some related code found somewhere in the forum) to sort out the text size. Thanks. Have reposted V3 above with changes implimented.
 

Roger Daley

Well-Known Member
Licensed User
Longtime User
Johan,

Nice work! I must take a good look at StringUtils, you've made a really neat job of it, my code looks like a bowl of spaghetti.
All appears to be working as it should. Thanks for the upload, it makes learning so much easier.

Regards Roger.
 

Johan Schoeman

Expert
Licensed User
Longtime User
Johan,

Nice work! I must take a good look at StringUtils, you've made a really neat job of it, my code looks like a bowl of spaghetti.
All appears to be working as it should. Thanks for the upload, it makes learning so much easier.

Regards Roger.
The StringUtils part comes from here by @Klaus Matle. I have just added an additional function to cater for the text size of the buttons. Thanks for your post - it pointed me in the right direction!
 
Top