bigfloat.demo.tcl

Go to the documentation of this file.
00001 /* */
00002 /*  BigFloat for Tcl*/
00003 /*  Copyright (C) 2003-2005  ARNOLD Stephane*/
00004 /* */
00005 /*  BIGFLOAT LICENSE TERMS*/
00006 /* */
00007 /*  This software is copyrighted by Stephane ARNOLD, (stephanearnold <at> yahoo.fr).*/
00008 /*  The following terms apply to all files associated*/
00009 /*  with the software unless explicitly disclaimed in individual files.*/
00010 /* */
00011 /*  The authors hereby grant permission to use, copy, modify, distribute,*/
00012 /*  and license this software and its documentation for any purpose, provided*/
00013 /*  that existing copyright notices are retained in all copies and that this*/
00014 /*  notice is included verbatim in any distributions. No written agreement,*/
00015 /*  license, or royalty fee is required for any of the authorized uses.*/
00016 /*  Modifications to this software may be copyrighted by their authors*/
00017 /*  and need not follow the licensing terms described here, provided that*/
00018 /*  the new terms are clearly indicated on the first page of each file where*/
00019 /*  they apply.*/
00020 /* */
00021 /*  IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY*/
00022 /*  FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES*/
00023 /*  ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY*/
00024 /*  DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE*/
00025 /*  POSSIBILITY OF SUCH DAMAGE.*/
00026 /* */
00027 /*  THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,*/
00028 /*  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,*/
00029 /*  FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE*/
00030 /*  IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE*/
00031 /*  NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR*/
00032 /*  MODIFICATIONS.*/
00033 /* */
00034 /*  GOVERNMENT USE: If you are acquiring this software on behalf of the*/
00035 /*  U.S. government, the Government shall have only "Restricted Rights"*/
00036 /*  in the software and related documentation as defined in the Federal*/
00037 /*  Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2).  If you*/
00038 /*  are acquiring the software on behalf of the Department of Defense, the*/
00039 /*  software shall be classified as "Commercial Computer Software" and the*/
00040 /*  Government shall have only "Restricted Rights" as defined in Clause*/
00041 /*  252.227-7013 (c) (1) of DFARs.  Notwithstanding the foregoing, the*/
00042 /*  authors grant the U.S. Government and others acting in its behalf*/
00043 /*  permission to use and distribute the software in accordance with the*/
00044 /*  terms specified in this license.*/
00045 /* */
00046 /* */
00047 
00048 package require math::bigfloat
00049 namespace import ::math::bigfloat::*
00050 
00051  nbButtons =  0
00052 ret  addButton (type command) {
00053     global nbButtons
00054     set ::buttons($nbButtons,command) _$command
00055     set ::buttons($nbButtons,texte) $command
00056     incr nbButtons
00057 }
00058 
00059 ret  addButtonTwo (type commande) {
00060     addButton $commande
00061     proc _$commande {} "if {\[catch {pop a} msg\]} {tk_messageBox -message \$msg;return}
00062     if {\[catch {pop b} msg\]} {push \$a
00063         tk_messageBox -message \$msg;return}
00064     if {\[catch {set result \[$commande \$a \$b\]} msg\]} {
00065         push \$b
00066         push \$a
00067         tk_messageBox -message \$msg
00068         return}
00069     push \$result"
00070 }
00071 
00072 
00073 ret  addButtonOne (type commande) {
00074     addButton $commande
00075     proc _$commande {} "if {\[catch {pop a} msg\]} {tk_messageBox -message \$msg;return}
00076     if {\[catch {set result \[$commande \$a\]} msg\]} {push \$a
00077         tk_messageBox -message \$msg
00078         return}
00079     push \$result"
00080 }
00081 
00082 
00083 ret  drawButtons () {
00084     global nbButtons
00085     set nbLines [expr {int(sqrt($nbButtons))}]
00086     for {set i 0} {$i<$nbButtons} {incr i} {
00087         set col [expr {$i%$nbLines}]
00088         set line [expr {$i/$nbLines}]
00089         set commande $::buttons($i,command)
00090         set texte $::buttons($i,texte)
00091         button .functions.$commande -text $texte -command $commande -width 10
00092         grid .functions.$commande -column $col -row $line -in .functions
00093         
00094     }
00095 }
00096 
00097 ret  initStack () {
00098     foreach i {1 2 3 4} {
00099         label .stack.l$i -text "[expr {5-$i}] :" -foreground #079 -width 5
00100         grid .stack.l$i -in .stack -row $i -column 1
00101         label .stack.n$i -text "Empty" -foreground #097 -width 85
00102         grid .stack.n$i -in .stack -row $i -column 2
00103     }
00104     set ::stack [list]
00105 }
00106 
00107 ret  Push () {
00108     set x [fromstr $::bignum]
00109     if {![isInt $x]} {
00110         set x [fromstr $::bignum $::zeros]
00111     }
00112     lappend ::stack $x
00113     set ::bignum 1.00
00114     set ::zeros 0
00115 }
00116 
00117 
00118 ret  toStr (type n) {
00119     set n [math::bigfloat::tostr $n]
00120     set resultat ""
00121     while {[string length $n]>80} {
00122         append resultat "[string range $n 0 79]...\n"
00123         set n [string range $n 80 end]
00124     }
00125     append resultat $n
00126 }
00127 
00128 
00129 ret  drawStack (type args) {
00130     set l [lrange $::stack end-3 end]
00131     for {set i 4} {$i>[llength $l]} {incr i -1} {
00132         .stack.n[expr {5-$i}] configure -text "Empty" -foreground #097
00133     }
00134     for {set i 0} {$i<[llength $l]} {incr i} {
00135         set number [lindex $::stack end-$i]
00136         .stack.n[expr {4-$i}] configure -text [toStr $number] -foreground #000
00137     }
00138 }
00139 
00140 ret  init () {
00141     wm title . "BigFloatDemo 1.2"
00142     # the stack (for RPN)
00143     frame .stack
00144     pack .stack
00145     initStack
00146     # the commands for input
00147     set c [frame .commands]
00148     pack $c -padx 10 -pady 10
00149     set ::bignum 1.00
00150     entry $c.bignum -textvariable ::bignum -width 16
00151     pack $c.bignum -in $c -side left
00152     label $c.labelZero -text "append zeros"
00153     pack $c.labelZero -in $c -side left
00154     set ::zeros 0
00155     entry $c.zeros -textvariable ::zeros -width 4
00156     pack $c.zeros -in $c -side left
00157     button $c.fenter -text "Push" -command Push
00158     pack $c.fenter -in $c -side left
00159     # the functions for numbers
00160     frame .functions
00161     pack .functions
00162     set f .functions
00163     # chaque fonction est associée, d'une part,
00164     # à un bouton portant un libellé, et d'autre part
00165     # à une commande Tcl
00166     # ici nous associons le bouton "add" à la commande "add"
00167     addButtonTwo add
00168     # toutes ces commandes se trouvent à la fin de ce fichier
00169     addButtonTwo sub
00170     addButtonTwo mul
00171     addButtonTwo div
00172     addButtonTwo mod
00173     addButtonOne opp
00174     addButtonOne abs
00175     addButtonOne round
00176     addButtonOne ceil
00177     addButtonOne floor
00178     addButtonTwo pow
00179     addButtonOne sqrt
00180     addButtonOne log
00181     addButtonOne exp
00182     addButtonOne cos
00183     addButtonOne sin
00184     addButtonOne tan
00185     addButtonOne acos
00186     addButtonOne asin
00187     addButtonOne atan
00188     addButtonOne cotan
00189     addButtonOne cosh
00190     addButtonOne sinh
00191     addButtonOne tanh
00192     addButtonOne pi
00193     addButtonOne rad2deg
00194     addButtonOne deg2rad
00195     addButtonOne int2float
00196     addButton del
00197     addButton swap
00198     addButton dup
00199     addButton help
00200     addButton save
00201     addButton exit
00202     drawButtons
00203     raise .
00204 }
00205 
00206 /* */
00207 /*  procedures that corresponds to functions (add,mul,etc.)*/
00208 /* */
00209 
00210 ret  _save () {
00211     set fichier [tk_getSaveFile -filetypes {{{Text Files} {.txt}}} -title "Save the stack as ..."]
00212     if {$fichier == ""} {
00213         error "You should give a name to the file. Aborting saving operation. Sorry."
00214     }
00215     if {[lindex [split $fichier .] end]!="txt"} {
00216         append fichier .txt
00217     }
00218     if {[catch {set file [open $fichier w]}]} {
00219         error "Write impossible on file : '$fichier'"
00220     }
00221     foreach valeur $::stack {
00222         puts $file [::math::bigfloat::tostr $valeur]
00223     }
00224     close $file
00225 }
00226 
00227 ret  ShowFile (type filename , type buttonText) {
00228     if {[catch {toplevel .help}]} {
00229         tk_messageBox -message "Unable to create the window ; please close the current help window"
00230         return
00231     }
00232     frame .help.licence
00233     text .help.licence.t -yscrollcommand {.help.licence.s set}
00234     scrollbar .help.licence.s -command {.help.licence.t yview}
00235     grid .help.licence.t .help.licence.s -sticky nsew
00236     grid columnconfigure .help.licence 0 -weight 1
00237     grid rowconfigure .help.licence 0 -weight 1
00238     
00239     pack .help.licence -in .help
00240     set fd [open $filename]
00241     .help.licence.t insert 0.0 [read $fd]
00242     close $fd
00243     .help.licence.t configure -state disabled
00244     button .help.bouton -text $buttonText -command {destroy .help;raise .}
00245     pack .help.bouton -in .help
00246     focus -force .help
00247 }
00248 
00249 ret  _help (type args) {
00250     # display some help
00251     ShowFile bigfloat.help Close
00252 }
00253 
00254 ret  _del () {
00255     if {[llength $::stack]<=1} {
00256         set ::stack {}
00257     } else  {
00258         set ::stack [lrange $::stack 0 end-1]
00259     }
00260 }
00261 
00262 ret  _swap () {
00263     set last [lindex $::stack end]
00264     lset ::stack end [lindex $::stack end-1]
00265     lset ::stack end-1 $last
00266 }
00267 
00268 /*  duplicate the last value*/
00269 ret  _dup () {
00270     lappend ::stack [lindex $::stack end]
00271 }
00272 
00273 
00274 
00275 ret  pop (type varname) {
00276     if {[llength $::stack]==0} {
00277         error "too few arguments in the stack"
00278     }
00279     upvar $varname out
00280     set out [lindex $::stack end]
00281     set ::stack [lrange $::stack 0 end-1]
00282     return
00283 }
00284 
00285 
00286 ret  push (type x) {
00287     lappend ::stack $x
00288 }
00289 
00290 ret  _exit () {
00291     update
00292     exit
00293 }
00294 
00295 
00296 
00297 /*  initialize the calculator and create the widgets (GUI)*/
00298 init
00299 /*  chaque fois qu'une commande modifie la pile de nombres,*/
00300 /*  la commande drawStack sera appelée pour la réactualiser*/
00301 trace add variable ::stack write drawStack
00302 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1