The BFOIT Introduction to Programming Class
Code for Calculator Program


Following is a good start at the calculator program, including the procedures in the structure drawing.  By the time that you are working on this program you should have already written procedures which are similar to these.  I'm giving you these so that we can work on functionality that you haven't yet used.

; global variable declarations
; ----------------------------
; need to add global variable definitions

; constant definitions
; --------------------
to black
  output 0
  end
to blue
  output 1
  end
to red
  output 4
  end
to white
  output 7
  end

to frameAt :x :y :height :width :penSiz
  setpensize :penSiz
  penup setxy :x :y pendown setheading 0
  repeat 2 [ forward :height right 90 forward :width right 90 ]
  end

to solidRect :height :width
  setpensize 1 setheading 0
  repeat :width [fd :height bk :height rt 90 pu fd 1 pd lt 90 ]
  end

to coloredRectAt :x :y :height :width :color
  penup setxy :x :y pendown
  setc :color
  solidRect :height :width
  end

to charsAt :x :y :text
  pu setxy :x :y pd
  setpc blue
  label :text
  end

to digitAt :x :y :digit
  penup setxy :x :y pendown
  setc blue
  label :digit
  end

to keyAt :x :y :size :label
  setc black
  frameAt :x :y :size :size 5
  penup setx sum :x 15 sety sum :y 15 pendown
  setc blue
  label :label
  end

to drawKeypad
  keyAt -100 -100 50 "0
  keyAt -100  -50 50 "1
  keyAt -100    0 50 "4
  keyAt -100   50 50 "7
  keyAt  -50 -100 50 "
  keyAt  -50  -50 50 "2
  keyAt  -50    0 50 "5
  keyAt  -50   50 50 "8
  keyAt    0 -100 50 "=
  keyAt    0  -50 50 "3
  keyAt    0    0 50 "6
  keyAt    0   50 50 "9
  keyAt   50 -100 50 "+
  keyAt   50  -50 50 "-
  keyAt   50    0 50 "X
  keyAt   50   50 50 "/
  end

to clearDisplay
  coloredRectAt -95 105 40 190 white
  end

to displayError
  clearDisplay
  charsAt -35 115 "E
  charsAt -10 115 "R
  charsAt  15 115 "R
  charsAt  40 115 "O
  charsAt  65 115 "R
  end

to displayMinusSign
  setc red
  penup setxy -85 115 pendown
  label [-]
  end

to displayNum :num
  ; need to add code to display :num
  end

to digit :num
  end

to doDivide
  end

to doMathOp
  end

to divideKey
  end

to equalsKey
  end

to minusKey
  end

to plusKey
  end

to timesKey
  end

to mouseInRect? :x :y :height :width
  if less? mousex :x [output "false]
  if less? mousey :y [output "false]
  if greater? mousex sum :x :width [output "false]
  if greater? mousey sum :y :height [output "false]
  output "true
  end

to mouseclick
  if mouseInRect? -100 -100 50 50 [digit 0]
  if mouseInRect? -100  -50 50 50 [digit 1]
  if mouseInRect? -100    0 50 50 [digit 4]
  if mouseInRect? -100   50 50 50 [digit 7]
  if mouseInRect?  -50  -50 50 50 [digit 2]
  if mouseInRect?  -50    0 50 50 [digit 5]
  if mouseInRect?  -50   50 50 50 [digit 8]
  if mouseInRect?    0  -50 50 50 [digit 3]
  if mouseInRect?    0    0 50 50 [digit 6]
  if mouseInRect?    0   50 50 50 [digit 9]
  if mouseInRect?    0 -100 50 50 [equalsKey]
  if mouseInRect?   50 -100 50 50 [plusKey]
  if mouseInRect?   50  -50 50 50 [minusKey]
  if mouseInRect?   50    0 50 50 [timesKey]
  if mouseInRect?   50   50 50 50 [divideKey]
  end

to init
  home clean hideturtle
  setheading 0 setc black setpensize 2
  setlabelheight 36
  drawKeypad
  setc black
  frameAt -100 100 50 200 5
  ;initialize all of the global variables
  ;display the current number - zero
  end

init


Table of Contents