How to Make a Calculator

Материал из Поле цифровой дидактики

This tutorial explains how to make a calculator. Шаблон:Note

What is a calculator?

A calculator is a (usually small) machine used to calculate math problems.<ref>wikipedia:Calculator</ref>

What will be needed:

Variables:

  • (Answer to !)
  • (Answer to ! p2)
  • (Answer to ^)
  • (Answer to ^ p2)
  • (Answers:)
  • (Current Equation:)
  • (Number: e)
  • (MTEP Number)
  • (MTTPO)
  • (Number before !)
  • (Number before ! p2)
  • (Pi)
  • (TTPO)
  • (TTPO Num 1)
  • (TTPO Num 2)

Lists:

  • (Answers 1::list)
  • (Answers 2::list)
  • (Answers 3::list)
  • (Answers 4::list)
  • (Calculating::list)
  • (Equations::list)
  • (Key::list)
  • (Making the equation::list)

Custom blocks:

  • Calculate::custom
  • Make the Equation::custom
  • Key::custom

Code

In the Stage

The code in the Stage is the following:

when green flag clicked
set [Current Equation: v] to ()
set [Answers: v] to ()
show list [Equations: v]
show list [Key: v]
show variable [Answer: v]
show variable [Current Equation: v]
Key::custom
define Key
delete all of [Key: v]
add [Use "*" for multiplication.] to [Key: v]
add [Use "/" for division.] to [Key: v]
add [Use "-" for subtraction.] to [Key: v]
add [Use "+" for addition.] to [Key: v]
add [To easily get the percent of a number, put the percent without the percent sign,] to [Key: v]
add [then press the check mark, then put "% of", then press the check mark, and then put the number you want] to [Key: v]
add [the percent of.] to [Key: v]
add [do not use commas.] to [Key: v]
add [Remember that you will have to put in a number then press the check mark then] to [Key: v]
add [put a symbol then press the check mark and then put a number.] to [Key: v]
add [You can use decimals as one number. Here is an example of a equation with a] to [Key: v]
add [number that has a decimal: 5395.58320349 (clicks the check mark) *] to [Key: v]
add [(clicks the check mark) 69483 (clicks the check mark) then you will receive the] to [Key: v]
add [answer to the equation, the answer to that one is 374901307.7] to [Key: v]
add [You can only make equations that are 3 clicks of the check mark.] to [Key: v]
add [So, if your equation is longer then just break down the equation into] to [Key: v]
add [smaller sections.] to [Key: v]
add ["square root of" will calculate the square root of a number.] to [Key: v]
add ["sin of" will calculate the sin of a number.] to [Key: v]
add ["cos of" will calculate the cos of a number.] to [Key: v]
add ["tan of" will calculate the tan of a number.] to [Key: v]
add ["asin of" will calculate the asin of a number.] to [Key: v]
add ["acos of" will calculate the acos of a number.] to [Key: v]
add ["atan of" will calculate the atan of a number.] to [Key: v]
add ["abs of" will calculate the abs of a number.] to [Key: v]
add ["floor of" will calculate the floor of a number.] to [Key: v]
add ["ceiling of" will calculate the ceiling of a number.] to [Key: v]
add ["ln of" will calculate the ln of a number.] to [Key: v]
add ["log of" will calculate the log of a number.] to [Key: v]
add ["mod" will calculate the mod.] to [Key: v]
add ["round" will round a number to a whole number.] to [Key: v]

The key could be put in the Instructions page of the project instead, if it is going to be shared.

In the Sprite

After the code contained in the stage, the next part of the code will be part of a Sprite. Once the sprite is made, then the code is as follows:

when green flag clicked
go to x: [0] y: [-140]
set drag mode [not draggable v]
show
delete all of [Calculating v]
delete all of [Making the Equation v]

Set Pi and Euler's number

when green flag clicked
set [Pi v] to [3.14159265358979323846264338327950288419716939937510] // This is Pi.
set [Number: e v] to [2.71828182845904523536028747135266249775724709369995] // This is Euler's number.

when this sprite clicked
hide
Make the Equation::custom

Make the Equation

define Make the Equation
set [MTEP Number: v] to [1]
repeat until <<(length of [Making the Equation v]) = [3]> or <[Making the Equation v] contains [!] ?>>
  ask (join [New Equation: (p] (join (MTEP Number:) [)])) and wait
  add (answer) to [Making the Equation: v]
  change [MTEP Number v] by [1]
end
if <<(length of [Making the Equation v]) = [3]> or <[Making the Equation v] contains [!] ?>> then
  show
  Calculate::custom
end

Calculating

define Calculate
set [Current Equation: v] to (Making the Equation)
if <[Making the Equation v] contains [ans] ?> then
  replace item (item # of [ans] in [Making the Equation v]) of [Making the Equation v] with (Answers:)
end
if <<[Making the Equation v] contains [Pi] ?> or <[Making the Equation v] contains [π] ?>> then
  replace item <(item # of [Pi] in [Making the Equation v]) or (item # of [π] in [Making the Equation v])> of [Making the Equation v] with (Pi)
end
if <<[Making the Equation v] contains [e] ?> or <[Making the Equation v] contains [E] ?>> then
  replace item <(item # of [e] in [Making the Equation v]) or (item # of [E] in [Making the Equation v])> of [Making the Equation v] with (Number: e)
end
if <[Making the Equation v] contains [*] ?> then
  add ((item ((item # of [*] in [Making the Equation v]) - [1]) of [Making the Equation v]) * (item ((item # of [*] in [Making the Equation v]) + [1]) of [Making the Equation v])) to [Calculating v]
end
if <[Making the Equation v] contains [/] ?> then
  add ((item ((item # of [/] in [Making the Equation v]) - [1]) of [Making the Equation v]) / (item ((item # of [/] in [Making the Equation v]) + [1]) of [Making the Equation v])) to [Calculating v]
end
if <[Making the Equation v] contains [-] ?> then
  add ((item ((item # of [-] in [Making the Equation v]) - [1]) of [Making the Equation v]) - (item ((item # of [-] in [Making the Equation v]) + [1]) of [Making the Equation v])) to [Calculating v]
end
if <[Making the Equation v] contains [+] ?> then
  add ((item ((item # of [+] in [Making the Equation v]) - [1]) of [Making the Equation v]) + (item ((item # of [+] in [Making the Equation v]) + [1]) of [Making the Equation v])) to [Calculating v]
end
if <[Making the Equation v] contains [% of] ?> then
  add ((item ((item # of [% of] in [Making the Equation v]) - [1]) of [Making the Equation v]) * ((item ((item # of [% of] in [Making the Equation v]) + [1]) of [Making the Equation v]) / [100])) to [Calculating v]
end
if <[Making the Equation v] contains [^] ?> then
  set [TTPO Num 1 v] to (item ((item # of [^] in [Making the Equation v]) - [1]) of [Making the Equation v])
  set [TTPO Num 2 v] to (item ((item # of [^] in [Making the Equation v]) + [1]) of [Making the Equation v])
  set [MTTPO v] to (TTPO Num 2)
  set [TTPO v] to (TTPO Num 1)
  if <(TTPO Num 2) = [0]> then
    set [Answer to ^ p2 v] to [1]
  else
    repeat until <(MTTPO) = [1]>
      set [Answer to ^ v] to ((TTPO) * (TTPO Num 1))
      set [Answer to ^ p2 v] to (Answer to ^)
      set [TTPO v] to (Answer to ^)
      change [MTTPO v] by ([0] - [1])
    end
  end
  add (Answer to ^ p2) to [Calculating v]
end
if <[Making the Equation v] contains [!] ?> then
  set [Number before ! v] to (item ((item # of [!] in [Making the Equation v]) - [1]) of [Making the Equation v])
  set [Number before ! p2 v] to (item ((item # of [!] in [Making the Equation v]) - [1]) of [Making the Equation v])
  repeat until <(Number before !) = [1]>
    set [Answer to ! v] to ((Number before ! p2) * ((Number before !) - [1]))
    set [Answer to ! p2 v] to (Number before !)
    change [Number before ! v] by ([0] - [1])
    change [Number before ! p2 v] by (Answer to !)
  end
  add (Answer to ! p2) to [Calculating v]
end
if <[Making the Equation v] contains [square root of] ?> then
  add ([sqrt v] of (item ((item # of [square root of] in [Making the Equation v]) + [1]) of [Making the Equation v])) to [Calculating v]
end
if <[Making the Equation v] contains [sin of] ?> then
  add ([sin v] of (item ((item # of [sin of] in [Making the Equation v]) + [1]) of [Making the Equation v])) to [Calculating v]
end
if <[Making the Equation v] contains [cos of] ?> then
  add ([cos v] of (item ((item # of [cos of] in [Making the Equation v]) + [1]) of [Making the Equation v])) to [Calculating v]
end
if <[Making the Equation v] contains [tan of] ?> then
  add ([tan v] of (item ((item # of [tan of] in [Making the Equation v]) + [1]) of [Making the Equation v])) to [Calculating v]
end
if <[Making the Equation v] contains [asin of] ?> then
  add ([asin v] of (item ((item # of [asin of] in [Making the Equation v]) + [1]) of [Making the Equation v])) to [Calculating v]
end
if <[Making the Equation v] contains [acos of] ?> then
  add ([acos v] of (item ((item # of [acos of] in [Making the Equation v]) + [1]) of [Making the Equation v])) to [Calculating v]
end
if <[Making the Equation v] contains [atan of] ?> then
  add ([atan v] of (item ((item # of [atan of] in [Making the Equation v]) + [1]) of [Making the Equation v])) to [Calculating v]
end
if <[Making the Equation v] contains [abs of] ?> then
  add ([abs v] of (item ((item # of [abs of] in [Making the Equation v]) + [1]) of [Making the Equation v])) to [Calculating v]
end
if <[Making the Equation v] contains [floor of] ?> then
  add ([floor v] of (item ((item # of [floor of] in [Making the Equation v]) + [1]) of [Making the Equation v])) to [Calculating v]
end
if <[Making the Equation v] contains [ceiling of] ?> then
  add ([ceiling v] of (item ((item # of [ceiling of] in [Making the Equation v]) + [1]) of [Making the Equation v])) to [Calculating v]
end
if <[Making the Equation v] contains [ln of] ?> then
  add ([ln v] of (item ((item # of [ln of] in [Making the Equation v]) + [1]) of [Making the Equation v])) to [Calculating v]
end
if <[Making the Equation v] contains [log of] ?> then
  add ([log v] of (item ((item # of [log of] in [Making the Equation v]) + [1]) of [Making the Equation v])) to [Calculating v]
end
if <[Making the Equation v] contains [mod] ?> then
  add ((item ((item # of [mod] in [Making the Equation v]) - [1]) of [Making the Equation v]) mod (item ((item # of [mod] in [Making the Equation v]) + [1]) of [Making the Equation v])) to [Calculating v]
end
if <[Making the Equation v] contains [round] ?> then
  add (round (item ((item # of [round] in [Making the Equation v]) + [1]) of [Making the Equation v])) to [Calculating v]
end
set [Answer: v] to (Calculating)
add (join (Current Equation:) (join [ = ] (Answers:))) to [Equations: v]
if <not <(Answers 1:) = [200000]>> then
  add (Answers:) to [Answers 1: v]
end
if <<not <(Answers 2:) = [200000]>> and <(Answers 1:) = [200000]>> then
  add (Answers:) to [Answers 2: v]
end
if <<not <(Answers 3:) = [200000]>> and <(Answers 1:) = [200000]>> then
  add (Answers:) to [Answers 3: v]
end
if <<not <(Answers 4:) = [200000]>> and <(Answers 1:) = [200000]>> then
  add (Answers:) to [Answers 4: v]
end
delete all of [Calculating v]
delete all of [Making the Equation v]

References