#! /bin/tclsh -f # calculator.tcl - RJM Programming - April, 2018 # thanks to # http://zetcode.com/lang/tcl/flowcontrol/ # http://dbaspot.com/shell/362549-tcsh-argv-loses-quotes-messes-argument-grouping-up.html # http://bin-co.com/tcl/articles/variables_in_arrays.php # http://docstore.mik.ua/orelly/unix3/upt/ch37_05.htm # http://books.google.com.au/books?id=BJpkh3RdG_UC&pg=PA45&lpg=PA45&dq=%22tclsh+split%22&source=bl&ots=PpQlwTydXv&sig=NIqU0g5LTutl9QEmakxbRCxVhpw&hl=en&sa=X&ei=OSk_VJXRLMLCmQWms4DACA&ved=0CB8Q6AEwAA#v=onepage&q=%22tclsh%20split%22&f=false # http://tmml.sourceforge.net/doc/tcl/split.html # http://zetcode.com/lang/tcl/flowcontrol/ # http://en.wikipedia.org/wiki/Country_code_top-level_firstnum # http://www.thrall.org/firstnums.htm # http://www.tutorialspoint.com/tcl-tk/pdf/tcl_strings.pdf set firstnum "" set oper "" set secnum "" foreach arg $argv: { set doms [split $arg ":"] set i 0 foreach dom $doms { if { $i == 0 } { set firstnum $dom } if { $i == 1 } { set oper $dom } if { $i == 2 } { set secnum $dom } incr i } } if { $firstnum == "" } { puts -nonewline "Type first number " flush stdout gets stdin firstnum } if { $oper == "" } { puts -nonewline "Type operator " flush stdout gets stdin oper } if { $secnum == "" } { puts -nonewline "Type second number " flush stdout gets stdin secnum } switch [string toupper $oper] { ~ { set vv [expr { $firstnum ~ $secnum }] } ! { set vv [expr { $firstnum ! $secnum }] } % { set vv [expr { $firstnum % $secnum }] } ** { set vv [expr { $firstnum ** $secnum }] } / { set vv [expr { $firstnum / $secnum }] } * { set vv [expr { $firstnum * $secnum }] } + { set vv [expr { $firstnum + $secnum }] } - { set vv [expr { $firstnum - $secnum }] } << { set vv [expr { $firstnum << $secnum }] } >> { set vv [expr { $firstnum >> $secnum }] } & { set vv [expr { $firstnum & $secnum }] } ^ { set vv [expr { $firstnum ^ $secnum }] } || { set vv [expr { $firstnum || $secnum }] } && { set vv [expr { $firstnum && $secnum }] } < { set vv [expr { $firstnum < $secnum }] } > { set vv [expr { $firstnum > $secnum }] } <= { set vv [expr { $firstnum <= $secnum }] } >= { set vv [expr { $firstnum >= $secnum }] } default { set vv "" } } puts "" puts $vv