際際滷

際際滷Share a Scribd company logo
Calculator
Day 4 :
Math
Create It With
in cooperation with
1
Calculator App
Many of you are thinking - oh no not math. This is
why I can never be a programmer because it is all
about math, bit and bytes.
Well the good news is, that LiveCode just makes it
simple and easy. Today we will code the heart of
the calculator app: Equals =.
You are probably thinking that it only gets
processed when the user presses the = button.
But actually if you think about it, the calculator
needs to total the number with every operation.
How do you think we are going to do this?
LiveCode + Math
2
Calculator App
Yes, it is that simple. You will create your
own equalsPressed command to do the
calculations. But first, we need to get the
code ready to calculations.
The first issue we need to deal with is
knowing when a number button is
pressed, do we add it to the existing
display or start a new number.
We are going to need to create
lNewNumber, a variable to keep track of
this for us.
Equals
local lCurrentOperator. lNewNumber
Remember that you have to
Edit the Card Script, and go all
the way to the top.
3
Calculator App
lNewNumber will let us know if we
should start with a new number. So the
first place we need to code this is in the
numberPressed command
If you remember yesterday we did a quick
if...then statement to check if the display
value was 0.
1. You just need to change the code to
if lNewNumber = true then
2. Then since it is not a new number
anymore, you will need to put false into
lNewNumber.
lNewNumber
on numberPressed pNumPress
if the text of field "display" = "0" then
if lNewNumber = true then
---just set the Display to the number pressed
put pNumPress into field "display"
put false into lNewNumber --Not a new Num anymore
else
---add the pressed number to the display
put pNumPress after field "display"
End if
end numberPressed
2
1
4
Note
Calculator App
Data Type and Auto Conversion
LiveCode does differentiate between text
and numbers. It will try and convert a
variable as number if you use it as such. If
the string is not numeric , LiveCode will
generate an error.
But if you look at the second block, the
testNumber is actually a text variable of
5. LiveCode detects that it is a number
and will convert it to a number for you.
The third block of code sets testNumber
as a number 5 and just does the math.
put "five" into testNumber
multiply testNumber by 3
-- An error will occur
put "5" into testNumber
multiply testNumber by 3
-- LiveCode will calculate 15 by converting 5
put 5 into testNumber
multiply testNumber by 3
-- LiveCode will calculate 15
5
Calculator App
It might look difficult, but it is as easy as
123.
equalsPressed
on equalsPressed
--get out if no currentOperator, nothing to calculate
if lCurrentOperator is empty then exit equalsPressed
put field "display" into tCurrentValue
switch lCurrentOperator
case "/"
put lCurrentTotal / tCurrentValue into lCurrentTotal
break
case "x"
put lCurrentTotal * tCurrentValue into lCurrentTotal
break
case "-"
put lCurrentTotal - tCurrentValue into lCurrentTotal
break
case "+"
put lCurrentTotal + tCurrentValue into lCurrentTotal
break
end switch
put lCurrentTotal into field "display"
end equalsPressed
1
2
3
1. Put the value in the display into a
temp variable tCurrentValue
2. Based on the current operator,
LiveCode will select one of the four
math calculation options.
3. Once the match is done, then show
the new current total to the user in
the display field.
6
Calculator App
Now that we have finished the
calculations, we need get ready for the
user to enter in more numbers. We only
have 2 more steps to make that happen.
equalsPressed
1
2
1. We need to clear the operator by
setting lCurrentOperator to empty
and hiding the border on the button.
2. If the user starts to press another
number, the code needs to know that
it will be a new number.
3. Now we just add equalsPressed to on
mouseUp of the = button.
on equalsPressed
....
put lCurrentTotal into field "display"
---Now that we performed the operation, clear it
set the ShowBorder of button lCurrentOperator to false
put empty into lCurrentOperator
---add the pressed number to the display
put true into lNewNumber
end equalsPressed
on mouseUp
equalsPressed
end mouseUp
3
7
Calculator App
We also need to add an update to
lCurrentTotal to the end of these routines:
operatorPressed
percentPressed
You can add this on the bottom of the
commands.
lNewNumber
---update the calculation variable
put field "display" into lCurrentTotal
8
LiveCode follows standard mathematical order of
operations.
For example, in mathematics and most computer languages
multiplication is done before addition; in the expression 2 +
3  4, the answer is 14. Brackets, "( .. )", which have their
own rules, may be used to avoid confusion, thus the
preceding expression may also be rendered 2 + (3  4). But
is you did (2 + 3)  4 the answer would be 20.
We recommend that you ALWAYS use brackets as much
as possible to avoid any misconceptions. It is cleaner code
and will help you avoid costly mistakes and bugs in the
future.
Check out the Wikipedia page for full details.
Order of Operations
Order of Operations
Calculator App
9
Calculator App
Lets finish with coding the togglePressed.
The calculator uses the 賊 button to toggle
the number in the display between positive
and negative.
Edit Card Script and create a new command
togglePressed. The code is simple and
straightforward.
Then in call the command on mouseUp
from the 賊 button
on togglePressed on togglePressed
----just toggle between positive and negative
put field "display" into tCurrentValue
put tCurrentValue * -1 into field "display"
put field "display" into lCurrentTotal
end togglePressed
on mouseUp
togglePressed
end mouseUp
10
If you wish to learn more Visit LiveCode
Congrats on completing:
Math
Dont forget to save your LiveCode Project!
Calculator App
11

More Related Content

Calculator 4

  • 1. Calculator Day 4 : Math Create It With in cooperation with 1
  • 2. Calculator App Many of you are thinking - oh no not math. This is why I can never be a programmer because it is all about math, bit and bytes. Well the good news is, that LiveCode just makes it simple and easy. Today we will code the heart of the calculator app: Equals =. You are probably thinking that it only gets processed when the user presses the = button. But actually if you think about it, the calculator needs to total the number with every operation. How do you think we are going to do this? LiveCode + Math 2
  • 3. Calculator App Yes, it is that simple. You will create your own equalsPressed command to do the calculations. But first, we need to get the code ready to calculations. The first issue we need to deal with is knowing when a number button is pressed, do we add it to the existing display or start a new number. We are going to need to create lNewNumber, a variable to keep track of this for us. Equals local lCurrentOperator. lNewNumber Remember that you have to Edit the Card Script, and go all the way to the top. 3
  • 4. Calculator App lNewNumber will let us know if we should start with a new number. So the first place we need to code this is in the numberPressed command If you remember yesterday we did a quick if...then statement to check if the display value was 0. 1. You just need to change the code to if lNewNumber = true then 2. Then since it is not a new number anymore, you will need to put false into lNewNumber. lNewNumber on numberPressed pNumPress if the text of field "display" = "0" then if lNewNumber = true then ---just set the Display to the number pressed put pNumPress into field "display" put false into lNewNumber --Not a new Num anymore else ---add the pressed number to the display put pNumPress after field "display" End if end numberPressed 2 1 4
  • 5. Note Calculator App Data Type and Auto Conversion LiveCode does differentiate between text and numbers. It will try and convert a variable as number if you use it as such. If the string is not numeric , LiveCode will generate an error. But if you look at the second block, the testNumber is actually a text variable of 5. LiveCode detects that it is a number and will convert it to a number for you. The third block of code sets testNumber as a number 5 and just does the math. put "five" into testNumber multiply testNumber by 3 -- An error will occur put "5" into testNumber multiply testNumber by 3 -- LiveCode will calculate 15 by converting 5 put 5 into testNumber multiply testNumber by 3 -- LiveCode will calculate 15 5
  • 6. Calculator App It might look difficult, but it is as easy as 123. equalsPressed on equalsPressed --get out if no currentOperator, nothing to calculate if lCurrentOperator is empty then exit equalsPressed put field "display" into tCurrentValue switch lCurrentOperator case "/" put lCurrentTotal / tCurrentValue into lCurrentTotal break case "x" put lCurrentTotal * tCurrentValue into lCurrentTotal break case "-" put lCurrentTotal - tCurrentValue into lCurrentTotal break case "+" put lCurrentTotal + tCurrentValue into lCurrentTotal break end switch put lCurrentTotal into field "display" end equalsPressed 1 2 3 1. Put the value in the display into a temp variable tCurrentValue 2. Based on the current operator, LiveCode will select one of the four math calculation options. 3. Once the match is done, then show the new current total to the user in the display field. 6
  • 7. Calculator App Now that we have finished the calculations, we need get ready for the user to enter in more numbers. We only have 2 more steps to make that happen. equalsPressed 1 2 1. We need to clear the operator by setting lCurrentOperator to empty and hiding the border on the button. 2. If the user starts to press another number, the code needs to know that it will be a new number. 3. Now we just add equalsPressed to on mouseUp of the = button. on equalsPressed .... put lCurrentTotal into field "display" ---Now that we performed the operation, clear it set the ShowBorder of button lCurrentOperator to false put empty into lCurrentOperator ---add the pressed number to the display put true into lNewNumber end equalsPressed on mouseUp equalsPressed end mouseUp 3 7
  • 8. Calculator App We also need to add an update to lCurrentTotal to the end of these routines: operatorPressed percentPressed You can add this on the bottom of the commands. lNewNumber ---update the calculation variable put field "display" into lCurrentTotal 8
  • 9. LiveCode follows standard mathematical order of operations. For example, in mathematics and most computer languages multiplication is done before addition; in the expression 2 + 3 4, the answer is 14. Brackets, "( .. )", which have their own rules, may be used to avoid confusion, thus the preceding expression may also be rendered 2 + (3 4). But is you did (2 + 3) 4 the answer would be 20. We recommend that you ALWAYS use brackets as much as possible to avoid any misconceptions. It is cleaner code and will help you avoid costly mistakes and bugs in the future. Check out the Wikipedia page for full details. Order of Operations Order of Operations Calculator App 9
  • 10. Calculator App Lets finish with coding the togglePressed. The calculator uses the 賊 button to toggle the number in the display between positive and negative. Edit Card Script and create a new command togglePressed. The code is simple and straightforward. Then in call the command on mouseUp from the 賊 button on togglePressed on togglePressed ----just toggle between positive and negative put field "display" into tCurrentValue put tCurrentValue * -1 into field "display" put field "display" into lCurrentTotal end togglePressed on mouseUp togglePressed end mouseUp 10
  • 11. If you wish to learn more Visit LiveCode Congrats on completing: Math Dont forget to save your LiveCode Project! Calculator App 11