oracleoracleformsoracle-fusion-middleware

code for equal to '=' in oracle forms calculator?


I have multiple buttons 0 to 9 and other calculation methods like plus, minus, etc

There are two display items, Memory and Display; Memory item is hidden.

When click on 1 button then display value 1 in Display item. When click on + button then store value 1 in Memory item. When click on = button then add Memory + Display values and show answer on Display item.

Question is how to code multiple calculation in equal to = button?


Solution

  • You have three registers: the button clicked, the display value and the memory value. So the calculation string 2+3=5 looks something like this:

    button Display Memory 
         2       2
         +       2      2
         3       3      2
         =       5      5
    

    As I understand your question, you want to handle a longer calculation, when the user types in several steps without pressing =, for instance 2+3+7/4*5=. There are several ways to do this, but the most intuitive one for the user is to treat the arithmetic operators as having an implicit = operation, calculating the running sum and displaying that value.

    button Display Memory 
         2       2
         +       2      2
         3       3      2
         +       5      5
         7       7      5
         /      12     12
         4       4     12
         *       3      3
         5       5      3
         =      15     15
    

    To make this work you need another register item to track the current operator.

    button Display Memory Operator
         2       2       
         +       2      2        +
         3       3      2        +
         +       5      5        +
         7       7      5        +
         /      12     12        /
         4       4     12        /
         *       3      3        *
         5       5      3        * 
         =      15     15        =
    

    So when the user clicks a triggering button you execute something like this:

    if :operator = '+' then
        :memory := :memory + :display;
    elsif :operator = '-' then
        :memory := :memory - :display;
    elsif :operator = '/' then
        :memory := :memory / :display;
    elsif :operator = '*' then
        :memory := :memory * :display;
    end if;
    :display := :memory;
    :operator := :button_value;
    

    You will need to decide how to handle the situation when the user types two operations in a row e.g. +/. But probably you need to track previous button press too.

    So what is the purpose of =? Well, it depends on what the user types next. If they follow = with another operator then it's just a subtotal and the sum continues....

    button Display Memory 
         2       2
         +       2      2
         3       3      2
         =       5      5
         +       2      5  <-- continue with existing sum
         =       7      7  
    

    ... but if they follow it with a number then we're starting a new sum and we reset the memory:

    button Display Memory 
         2       2
         +       2      2
         3       3      2
         =       5      5
         2       2         <-- start a new sum
         +       2      2
         2       2      2
         =       4      4