Wednesday, 21 December 2011

Calculator

There are a lot of calculator scripts, let's see them (in alphabetic order).


Calculator
It's a tiny calculator with just 30 lines of code:
Here the source:
REBOL [
    Title: "Calculator"
    Date: 2-Apr-2001
    Version: 1.2.0
    File: %calculator.r
    Author: "Jeff Kreis"
    Purpose: "Simple numeric calculator."
]
auto-clear: true
calculate: does [
    if error? try [text-box/text: form do text-box/text][
        text-box/text: "Error"
        text-box/color: red
    ]
    auto-clear: true
    show text-box
]
calculator: layout [
    style btn button 40x24
    style kc btn brick [text-box/text: copy "0" auto-clear: true show text-box]
    style k= btn [calculate]
    style k   btn [
        if auto-clear [clear text-box/text text-box/color: snow auto-clear: false]
        append text-box/text face/text
        show text-box
    ]
    origin 10
    backcolor rebolor
    space 4
    text-box: field "0" 172x24 bold snow right feel none
    pad 4
    across
    kc "C" keycode [#"C" #"c" page-down]
    k "(" #"("   k ")" #")"   k " / " #"/" return
    k "7" #"7"   k "8" #"8"   k "9" #"9"   k " * " #"*" return
    k "4" #"4"   k "5" #"5"   k "6" #"6"   k " - " #"-" return
    k "1" #"1"   k "2" #"2"   k "3" #"3"   k " + " #"+" return
    k "0" #"0"   k "-"       k "." #"."
    k= "=" keycode [#"=" #"^m"] return
    key keycode [#"^(ESC)" #"^q"] [quit]
]
view center-face calculator

Calculator tutorial
It's a demo made by Nick Antonaccio:
Here the source (always no more than 30 lines of code):
REBOL [
    title: "calculator"
    date: 28-feb-2009
    file: %calculator-tutorial.r
    purpose: {
        A little GUI calculator example, with printout.  
        Taken from the tutorial at http://musiclessonz.com/rebol_tutorial.html
    }
]
prev-val: cur-val: 0 cur-eval: "+" display-flag: false
print "0"
view center-face layout/tight [
    size 300x350 space 0x0 across
    display: field 300x50 font-size 28 "0" return
    style butn button 100x50   [
        if display-flag = true [display/text: "" display-flag: false]
        if display/text = "0" [display/text: ""]
        display/text: rejoin [display/text value]
        show display
        cur-val: display/text
    ]
    style eval button 100x50 brown font-size 13 [
        prev-val: cur-val
        display/text: "" show display
        cur-eval: value
    ]
    butn "1"   butn "2"   butn "3"   return
    butn "4"   butn "5"   butn "6"   return
    butn "7"   butn "8"   butn "9"   return
    butn "0"   butn "."   eval "+" return
    eval "-" eval "*" eval "/" return
    button 300x50 gray font-size 16 "=" [
        if display-flag <> true [
            if ((cur-eval = "/") and (cur-val = "0")) [
                alert "Division by 0 is not allowed." break
            ]
            prin rejoin [prev-val " " cur-eval " " cur-val " = "]
            print display/text: cur-val: do rejoin [prev-val " " cur-eval " " cur-val ]
            show display
            display-flag: true
        ]
    ]
]

Desktop Calculator
It's a small calculator:
Here the source (70 lines):
REBOL [
    Title: "Desk Calculator"
    Date: 4-Oct-2004
    Version: 1.3.0
    File: %desk-calc.r
    Author: "Ryan S. Cole"
    Purpose: "A tool for simple calculations."
    Comment: "Standard function calculator."
    Email: ryan@skurunner.com
]
acc:         ; Accumulator
op:         ; Selected operation
mem:         ; Memory storage
err: none   ; Error state
reg: []     ; Register stack

; For working with the displayed number...
cur-str: does [any [reg/1 acc form 0]]
cur-num: does [to-decimal cur-str]
cur-set: func [val] [
    either not either op [reg/2][reg/1] [insert reg form val] [reg/1: form val]
]
; Updates the screen...
display: does [
    if not find lcd/text: copy cur-str "." [append lcd/text "."]
    err-flag/font/color: either err [yellow][gray]
    mem-flag/font/color: either mem [green][gray]
    show [lcd err-flag mem-flag]
]
; Does the equation...
solve: does [
    if not reg/2 [insert reg reg/1]
    acc: none
    if op [err: error? try [acc: form do reform [reg/2 op 'to-decimal reg/1]]]
    reg: copy []
    op: none
]
; Handles keypresses
press: func [key] [
    err: no
    if find ".0123456789" key [
        if not either op [reg/2][reg/1] [insert reg copy ""]
        if all ["." = key   find reg/1 key] [exit]
        either all [reg/1 = "0"   key <> "."] [reg/1: copy key] [append reg/1 key]
    ]
    if find "+-*/" key [
        if reg/2 [solve]
        if not reg/1 [insert reg cur-str]
        op: key
    ]
    switch key [
        "C" [acc: op: none reg: copy []]
        "E" [remove reg op: acc: none]
        "±" [cur-set negate cur-num]
        "MC" [mem: none]
        "MR" [cur-set any [mem 0]]
        "M+" [mem: add any [mem 0] cur-num]
        "M-" [mem: subtract any [mem 0] cur-num]
        "pi" [cur-set pi]
        "=" [solve]
    ]
]
; Construct the screen...
view layout compose [
    backdrop effect [gradient 0x1 85.155.205 80.130.180]
    origin 10x10 space 0x0 pad 0x10 across
    style text text 15x20 bold gray  
    mem-flag: text "M"
    err-flag: text "E"
    pad 5x0   space 5x5
    lcd: field "0." silver 170x20 right bold feel none
    return
    style k (pick [btn button] link?) 30x20 [press face/text display]
    k #"C" "C"   k "MC"   k #"7" "7"   k #"8" "8"   k #"9" "9"   k #"/" "/" return
    k #"E" "E"   k "MR"   k #"4" "4"   k #"5" "5"   k #"6" "6"   k #"*" "*" return
    k #"p" "pi"   k "M+"   k #"1" "1"   k #"2" "2"   k #"3" "3"   k #"+" "+" return
    k #"i" "±"   k "M-"   k #"0" "0"   k #"." "."   k #"=" "="   k #"-" "-"
    keycode #"^M" [press "=" display]
]                        

Mini calc
It's really a mini calculator:
Here the source (30 lines):
REBOL [
    Title: "Mini-Calculator"
    Date: 6-Mar-2002
    Version: 1.1.3
    File: %mini-calc.r
    Author: "Ryan Cole"
    Purpose: "Tiny calculator example."
    Email: ryancole@usa.com
]
reg: []
op: no

do solve: does [
    acc: none
    if op [error? try [acc: do reform [any [reg/2 acc 0] op 'to-decimal reg/1]]]
    reg: copy []
    op: no
]
calc: func [key] [
    if find ".0123456789" key [
        if none? pick reg not op [insert reg copy ""]
        if not all ["." = key   find reg/1 key] [append reg/1 key]
    ]
    if find "+-*/" key [
        if reg/2 [solve]
        any [reg/1 insert reg any [acc 0]]
        op: key
    ]
    if key = "=" [solve]
]
view layout [
    origin 0x0 space 0x0 across
    lcd: field "0." silver 140x22 right feel none
    return
    style k button 35x25 [
        calc face/text
        if not find lcd/text: form any [reg/1 acc 0] "." [append lcd/text "."]
        show lcd
    ]
    k "7" k "8" k "9" k "/" return
    k "4" k "5" k "6" k "*" return
    k "1" k "2" k "3" k "-" return
    k "0" k "." k "=" k "+"
]                                              

Scientific calculator
It's a scientific calculator, it reduce conde lenght because it uses the calculase dialect/script:
Here the source (40 lines):
REBOL [
    Title: "Scientific Calculator"
    Date: 16-Mar-2002
    Version: 0.9.5
    File: %sci-calc.r
    Author: ["Ryan S. Cole" "Massimiliano Vessi"]
    Purpose: {For scientific calculations.   Currently in beta, so dont use it to figure out critical information just yet.}
    Email: [ryanc@iesco-dms.com   maxint@tiscali.it]
]
if not exists? %calculese.r [
    temp: read http://www.rebol.org/download-a-script.r?script-name=calculese.r
    write temp %calculese.r
    ]
   
do %calculese.r
; depth of stack shown in parens
depth: has [fathoms] [
    fathoms: copy ""
    loop length? calc-engine/stack [append fathoms "'"]
    return fathoms
]
view layout [
    backdrop effect [gradient 0x1 74.74.74 32.32.32]
    origin 6x6 space 3x3
    lcd: field "0." 262x32 right bold silver feel none font-size 22
    across
    style k button gray 50x20 [
        lcd/text: calculese face/text
        lcd/effect: compose/deep [pen 0.0.0 draw [text 2x19 (depth)]]
        show lcd
    ]
    style r k brick
    style g k leaf
    style o k orange
    style s k sienna
    style t k teal
    style a k aqua
    style b k tan
    r "CE" b "and" b "or" b "xor" b "not" return
    r "AC" a "arcsin" a "arccos" a "arctan" a #"p" "pi" return
    g "M÷" a "sin" a "cos" a "tan" a "abs" return
    g "M×" a "exp-e" a "log-10" a "log-2" a "log-e" return
    g "M-" a "mod" a "sqr" a "exp" a "¹/x" return
    g "M+" a #"±" "±" a #"r" "rnd" a "²" a "³" return
    g "MR" k #"7" "7" k #"8" "8" k #"9" "9" t #"/" "÷" return
    g "MC" k #"4" "4" k #"5" "5" k #"6" "6" t #"*" "×" return
    s #"(" "(" k #"1" "1" k #"2" "2" k #"3" "3" t #"-" "-" return
    s #")" ")" k #"0" "0" k #"." "." o #"^M" "="   t #"+" "+"
]                                                

Supercalculator
It's a scientific calculator with history, configuration and session. All it's stored in ".supercalculator.conf" file:
Suorce code is about 200 lines, because it doesn't use any calculus lib, but it uses the RebGUI lib. You can download the script here:
http://www.rebol.org/view-script.r?script=supercalculator.r

1 comment:

  1. also check out this amazing calculator.
    http://www.limitcalculators.com/

    ReplyDelete