You are given time $T in the format hh:mm.
Write a script to find the smaller angle formed by the hands of an analog clock at a given time.
#!/usr/bin/env tclsh
if {$argc >= 1} {
set timestring [lindex $argv 0]
} elseif {$argc == 0} {
puts "Please input a string"
exit
}
set clockstring [split $timestring ":"]
set hour [lindex $clockstring 0]
set second [lindex $clockstring 1]
# Remove leading zero to let expr work correctly
scan $hour %d hour
scan $second %d second
if {$hour < 0 || $hour >= 12} {
puts "Invalid hour data."
}
if {$second < 0 || $second >= 60} {
puts "Invalid second data."
}
set secvalue [expr $second * 6]
set hourvalue [expr ($hour * 30) + ($secvalue / 12)]
if {$hourvalue > $secvalue} {
set value1 [expr 360 - $hourvalue + $secvalue]
set value2 [expr $hourvalue - $secvalue]
if {$value1 > 0 && $value1 < $value2} {
puts "$value1 degree"
} else {
puts "$value2 degree"
}
} else {
set value1 [expr 360 - $secvalue + $hourvalue]
set value2 [expr $secvalue - $hourvalue]
if {$value1 > 0 && $value1 < $value2} {
puts "$value1 degree"
} else {
puts "$value2 degree"
}
}
沒有留言:
張貼留言