File: bigfloat.demo.tcl

package info (click to toggle)
tcllib 1.20%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 68,064 kB
  • sloc: tcl: 216,842; ansic: 14,250; sh: 2,846; xml: 1,766; yacc: 1,145; pascal: 881; makefile: 107; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (308 lines) | stat: -rwxr-xr-x 9,332 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env tclsh
## -*- tcl -*-

set scriptDir [file dirname [info script]]

########################################################################
# BigFloat for Tcl
# Copyright (C) 2003-2005  ARNOLD Stephane
#
# BIGFLOAT LICENSE TERMS
#
# This software is copyrighted by Stephane ARNOLD, (stephanearnold <at> yahoo.fr).
# The following terms apply to all files associated
# with the software unless explicitly disclaimed in individual files.
#
# The authors hereby grant permission to use, copy, modify, distribute,
# and license this software and its documentation for any purpose, provided
# that existing copyright notices are retained in all copies and that this
# notice is included verbatim in any distributions. No written agreement,
# license, or royalty fee is required for any of the authorized uses.
# Modifications to this software may be copyrighted by their authors
# and need not follow the licensing terms described here, provided that
# the new terms are clearly indicated on the first page of each file where
# they apply.
#
# IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
# FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
# ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
# DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE
# IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
# NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
# MODIFICATIONS.
#
# GOVERNMENT USE: If you are acquiring this software on behalf of the
# U.S. government, the Government shall have only "Restricted Rights"
# in the software and related documentation as defined in the Federal
# Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2).  If you
# are acquiring the software on behalf of the Department of Defense, the
# software shall be classified as "Commercial Computer Software" and the
# Government shall have only "Restricted Rights" as defined in Clause
# 252.227-7013 (c) (1) of DFARs.  Notwithstanding the foregoing, the
# authors grant the U.S. Government and others acting in its behalf
# permission to use and distribute the software in accordance with the
# terms specified in this license.
#
########################################################################

package require Tk

package require math::bigfloat
namespace import ::math::bigfloat::*

set nbButtons 0
proc addButton {command} {
    global nbButtons
    set ::buttons($nbButtons,command) _$command
    set ::buttons($nbButtons,texte) $command
    incr nbButtons
}

proc addButtonTwo {commande} {
    addButton $commande
    proc _$commande {} "if {\[catch {pop a} msg\]} {tk_messageBox -message \$msg;return}
    if {\[catch {pop b} msg\]} {push \$a
        tk_messageBox -message \$msg;return}
    if {\[catch {set result \[$commande \$a \$b\]} msg\]} {
        push \$b
        push \$a
        tk_messageBox -message \$msg
        return}
    push \$result"
}


proc addButtonOne {commande} {
    addButton $commande
    proc _$commande {} "if {\[catch {pop a} msg\]} {tk_messageBox -message \$msg;return}
    if {\[catch {set result \[$commande \$a\]} msg\]} {push \$a
        tk_messageBox -message \$msg
        return}
    push \$result"
}


proc drawButtons {} {
    global nbButtons
    set nbLines [expr {int(sqrt($nbButtons))}]
    for {set i 0} {$i<$nbButtons} {incr i} {
        set col [expr {$i%$nbLines}]
        set line [expr {$i/$nbLines}]
        set commande $::buttons($i,command)
        set texte $::buttons($i,texte)
        button .functions.$commande -text $texte -command $commande -width 10
        grid .functions.$commande -column $col -row $line -in .functions
        
    }
}

proc initStack {} {
    foreach i {1 2 3 4} {
        label .stack.l$i -text "[expr {5-$i}] :" -foreground #079 -width 5
        grid .stack.l$i -in .stack -row $i -column 1
        label .stack.n$i -text "Empty" -foreground #097 -width 85
        grid .stack.n$i -in .stack -row $i -column 2
    }
    set ::stack [list]
}

proc Push {} {
    set x [fromstr $::bignum]
    if {![isInt $x]} {
        set x [fromstr $::bignum $::zeros]
    }
    lappend ::stack $x
    set ::bignum 1.00
    set ::zeros 0
}


proc toStr {n} {
    set n [math::bigfloat::tostr $n]
    set resultat ""
    while {[string length $n]>80} {
        append resultat "[string range $n 0 79]...\n"
        set n [string range $n 80 end]
    }
    append resultat $n
}


proc drawStack {args} {
    set l [lrange $::stack end-3 end]
    for {set i 4} {$i>[llength $l]} {incr i -1} {
        .stack.n[expr {5-$i}] configure -text "Empty" -foreground #097
    }
    for {set i 0} {$i<[llength $l]} {incr i} {
        set number [lindex $::stack end-$i]
        .stack.n[expr {4-$i}] configure -text [toStr $number] -foreground #000
    }
}

proc init {} {
    wm title . "BigFloatDemo 1.2"
    # the stack (for RPN)
    frame .stack
    pack .stack
    initStack
    # the commands for input
    set c [frame .commands]
    pack $c -padx 10 -pady 10
    set ::bignum 1.00
    entry $c.bignum -textvariable ::bignum -width 16
    pack $c.bignum -in $c -side left
    label $c.labelZero -text "append zeros"
    pack $c.labelZero -in $c -side left
    set ::zeros 0
    entry $c.zeros -textvariable ::zeros -width 4
    pack $c.zeros -in $c -side left
    button $c.fenter -text "Push" -command Push
    pack $c.fenter -in $c -side left
    # the functions for numbers
    frame .functions
    pack .functions
    set f .functions
    # chaque fonction est associée, d'une part,
    # à un bouton portant un libellé, et d'autre part
    # à une commande Tcl
    # ici nous associons le bouton "add" à la commande "add"
    addButtonTwo add
    # toutes ces commandes se trouvent à la fin de ce fichier
    addButtonTwo sub
    addButtonTwo mul
    addButtonTwo div
    addButtonTwo mod
    addButtonOne opp
    addButtonOne abs
    addButtonOne round
    addButtonOne ceil
    addButtonOne floor
    addButtonTwo pow
    addButtonOne sqrt
    addButtonOne log
    addButtonOne exp
    addButtonOne cos
    addButtonOne sin
    addButtonOne tan
    addButtonOne acos
    addButtonOne asin
    addButtonOne atan
    addButtonOne cotan
    addButtonOne cosh
    addButtonOne sinh
    addButtonOne tanh
    addButtonOne pi
    addButtonOne rad2deg
    addButtonOne deg2rad
    addButtonOne int2float
    addButton del
    addButton swap
    addButton dup
    addButton help
    addButton save
    addButton exit
    drawButtons
    raise .
}

################################################################################
# procedures that corresponds to functions (add,mul,etc.)
################################################################################

proc _save {} {
    set fichier [tk_getSaveFile -filetypes {{{Text Files} {.txt}}} -title "Save the stack as ..."]
    if {$fichier == ""} {
        error "You should give a name to the file. Aborting saving operation. Sorry."
    }
    if {[lindex [split $fichier .] end]!="txt"} {
        append fichier .txt
    }
    if {[catch {set file [open $fichier w]}]} {
        error "Write impossible on file : '$fichier'"
    }
    foreach valeur $::stack {
        puts $file [::math::bigfloat::tostr $valeur]
    }
    close $file
}

proc ShowFile {filename buttonText} {
    if {[catch {toplevel .help}]} {
        tk_messageBox -message "Unable to create the window ; please close the current help window"
        return
    }
    frame .help.licence
    text .help.licence.t -yscrollcommand {.help.licence.s set}
    scrollbar .help.licence.s -command {.help.licence.t yview}
    grid .help.licence.t .help.licence.s -sticky nsew
    grid columnconfigure .help.licence 0 -weight 1
    grid rowconfigure .help.licence 0 -weight 1
    
    pack .help.licence -in .help
    set fd [open $filename]
    .help.licence.t insert 0.0 [read $fd]
    close $fd
    .help.licence.t configure -state disabled
    button .help.bouton -text $buttonText -command {destroy .help;raise .}
    pack .help.bouton -in .help
    focus -force .help
}

proc _help {args} {
    # display some help
    ShowFile [file join $::scriptDir bigfloat.help] Close
}

proc _del {} {
    if {[llength $::stack]<=1} {
        set ::stack {}
    } else  {
        set ::stack [lrange $::stack 0 end-1]
    }
}

proc _swap {} {
    set last [lindex $::stack end]
    lset ::stack end [lindex $::stack end-1]
    lset ::stack end-1 $last
}

# duplicate the last value
proc _dup {} {
    lappend ::stack [lindex $::stack end]
}



proc pop {varname} {
    if {[llength $::stack]==0} {
        error "too few arguments in the stack"
    }
    upvar $varname out
    set out [lindex $::stack end]
    set ::stack [lrange $::stack 0 end-1]
    return
}


proc push {x} {
    lappend ::stack $x
}

proc _exit {} {
    update
    exit
}



# initialize the calculator and create the widgets (GUI)
init
# chaque fois qu'une commande modifie la pile de nombres,
# la commande drawStack sera appelée pour la réactualiser
trace add variable ::stack write drawStack