Showing posts with label Calculator. Show all posts
Showing posts with label Calculator. Show all posts

Friday, 30 August 2013

The world's smallest spreadsheet program

Mr. Carl Sassenrath is always a genius in coding, look this spreadsheet program:




the source code is only 70 lines!!!! He probably wrote it in 2 minutes...

Here is the source:

REBOL [
    Title: "Rebocalc"
    Date: 19-Jun-2001
    Version: 1.0.0
    File: %rebocalc.r
    Author: "Carl Sassenrath"
    Purpose: {The world's smallest spreadsheet program, but very powerful.}
    Email: carl@rebol.com
]
csize: 100x20
max-x: 8
max-y: 16
pane: []
xy: csize / 2 + 1 * 1x0
yx: csize + 1 * 0x1
layout [
    cell:   field csize edge none [enter face   compute   face/para/scroll: 0x0]
    label: text csize white black bold center
]
;--Headings:
char: #"A"
repeat x max-x [
    append pane make label [offset: xy text: char]
    set in last pane 'offset xy
    xy: csize + 1 * 1x0 + xy
    char: char + 1
]
repeat y max-y [
    append pane make label [offset: yx text: y size: csize * 1x2 / 2]
    yx: csize + 1 * 0x1 + yx
]
xy: csize * 1x2 / 2 + 1
;--Cells:
cells: tail pane
repeat y max-y [
    char: #"A"
    repeat x max-x [
        v: to-word join char y
        set v none
        char: char + 1
        append pane make cell [offset: xy text: none var: v formula: none]
        xy: csize + 1 * 1x0 + xy
    ]
    xy: csize * 1x2 / 2 + 1 + (xy * 0x1)
]
enter: func [face /local data] [
    if empty? face/text [exit]
    set face/var face/text
    data: either face/text/1 = #"=" [next face/text][face/text]
    if error? try [data: load data] [exit]
    if find [integer! decimal! money! time! date! tuple! pair!] type?/word :data [set face/var data exit]
    if face/text/1 = #"=" [face/formula: :data]   ; string case falls thru
]
compute: has [blk] [
    unfocus
    foreach cell cells [
        if cell/formula [ ;probe cell/var
            if error? cell/text: try [do cell/formula] [cell/text: "ERROR!"]
            set cell/var cell/text
            show cell
        ]
    ]
]
lo: layout [
    bx: box second span? pane
    pad 55x0
    text as-is trim/auto {
        Cells can be numbers, times, money, tuples, pairs, etc.
        If a cell is not a scalar value, then it is treated as a string.
        Start formulas with the = character.   Any REBOL expression is valid.
        Remember to put spaces between each item in a formula.   Use ( ) where needed.
        Refer to cells as A1 D8 E10.   Example: =A1 + B1 * length? B8
        Example: in A1 type REBOL, in B1 type =length? a1, in C1 type =reverse copy a1
        Then: in D1 =checksum A1.   Now, change A1 to "Amazing!"
        In A2 type 1 + 2 (no =), in B2 type =A2.   Now change A2 to 3 * 4.
        Try: =(now/time) or =request-date or =checksum read rejoin [http://www. A1 ".com"]
        Computation moves from top to bottom. It is non-iterative.
    }

]
bx/pane: pane
view lo

Friday, 19 April 2013

Mini calculator

Here is the shorter calculator code on earth and intelligible!

Here is the source code:
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 "+"
]                                              

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