#!/bin/sh # This line continues for Tcl, but is a single line for 'sh' \ exec wish "$0" ${1+"$@"} wm title . "Markup Calculator" pack [frame .top] -side top -padx 4 -pady 4 pack [frame .fill -height 1 -background #73f773f773f7] -side top -fill x -padx 2 -pady 4 pack [frame .btm] -side top -padx 4 -pady 4 pack [frame .top.left] -side left -fill both -pady 4 -padx 4 pack [frame .top.fill -width 1 -background #73f773f773f7] -side left -fill y -pady 2 -padx 4 pack [frame .top.right] -side left -fill both -pady 4 -padx 4 # Left frame is "amount / % / amount2" # Right frame is "amount1 / amount2 / % markup" # British Pound set currSign [format %c 163] # Dollar sign #set currSign {$} pack [frame .top.left.top] -side top pack [label .top.left.top.l -text "Amount ($currSign)" -width 15 -anchor w] -side left -anchor nw pack [entry .top.left.top.e -textvariable leftIn1 -width 12 -validatecommand {chkInput %P 1} -validate key] -side left -anchor nw pack [frame .top.left.mid] -side top pack [label .top.left.mid.l -text "Markup (%)" -width 15 -anchor w] -side left -anchor nw pack [entry .top.left.mid.e -textvariable leftIn2 -width 12 -validatecommand {chkInput %P 0} -validate key] -side left -anchor nw pack [frame .top.left.btm] -side top pack [label .top.left.btm.l -text "New Amount:" -width 15 -anchor w] -side left -anchor nw pack [label .top.left.btm.l2 -textvariable leftAns -width 12 -anchor e -relief sunken] -side left -anchor nw bind . {calcAns} pack [frame .top.right.top] -side top pack [label .top.right.top.l -text "Amount 1 ($currSign)" -width 15 -anchor w] -side left -anchor nw pack [entry .top.right.top.e -textvariable rightIn1 -width 12 -validatecommand {chkInput %P 1} -validate key] -side left -anchor nw pack [frame .top.right.mid] -side top pack [label .top.right.mid.l -text "Amount 2 ($currSign)" -width 15 -anchor w] -side left -anchor nw pack [entry .top.right.mid.e -textvariable rightIn2 -width 12 -validatecommand {chkInput %P 1} -validate key] -side left -anchor nw pack [frame .top.right.btm] -side top pack [label .top.right.btm.l -text "Difference (%)" -width 15 -anchor w] -side left -anchor nw pack [label .top.right.btm.l2 -textvariable rightAns -width 12 -anchor e -relief sunken] -side left -anchor nw pack [frame .btm.top] -side top pack [label .btm.top.l -text "Quick Calc:" -width 15 -anchor w] -side left -anchor nw pack [entry .btm.top.e -textvariable btmIn -width 50] -side left -anchor nw pack [frame .btm.btm] -side top pack [label .btm.btm.l -text "Answer:" -width 15 -anchor w] -side left -anchor nw pack [label .btm.btm.l2 -textvariable btmAns -width 50 -anchor w -relief sunken] -side left -anchor nw proc chkInput {str which} { set reg1 {^[0-9]*(\.[0-9]{0,2})?$} set reg0 {^-?[0-9]*\.?[0-9]*?$} if { [regexp [set reg$which] $str] } { return 1; } return 0; };# chkInput proc calcAns {} { global leftIn1 leftIn2 leftAns global rightIn1 rightIn2 rightAns global btmIn btmAns currSign if { [squish $leftIn1] == "" || [squish $leftIn2] == "" } { set leftAns "" } else { catch {set tmp [expr {($leftIn1/100.0)*$leftIn2}] set tmp [expr {$tmp+$leftIn1}] set leftAns [format "$currSign%1.2f" $tmp] } } if { [squish $rightIn1] == "" || [squish $rightIn2] == "" } { set rightAns "" } else { catch {set tmp [expr {((1.0*$rightIn2)/$rightIn1)}] set tmp [expr {($tmp*100.0)-100}] set rightAns [format "%1.2f%%" $tmp] } } regsub -all {([0-9]+\.?[0-9]*)} $btmIn {double(\1)} btmIn2 if { $btmIn == "" } { set btmAns {} } elseif { [catch {expr "((1.0*1.0)*$btmIn2)"} tmp] } { set btmAns "#-1 BAD SUM" } else { set btmAns $tmp } };# calcAns proc squish {str} { regsub -all {( +)} $str { } str regsub -all {^( +)} $str {} str regsub -all {( +)$} $str {} str return $str; };# squish catch {after idle {console hide}}