school/cs142/smcquay/go/lab02/seconds/seconds.go
2016-04-06 20:45:34 -07:00

37 lines
731 B
Go

package main
import (
"fmt"
"math"
)
const SECONDS_PER_HOUR = 3600
const SECONDS_PER_MINUTE = 60
type time struct {
hours int
minutes int
seconds int
}
func (t time) String() string {
return fmt.Sprintf("%dH:%dM:%dS", t.hours, t.minutes, t.seconds);
}
func int2time(i int) time {
var r_secs int
hours := i / SECONDS_PER_HOUR
r_secs = i % SECONDS_PER_HOUR
mins := r_secs / SECONDS_PER_MINUTE
r_secs = r_secs % SECONDS_PER_MINUTE
return time{hours, mins, r_secs}
}
func main() {
var seconds int
fmt.Print("please enter total seconds: ")
fmt.Scan(&seconds)
fmt.Printf("%v\n", int2time(seconds))
fmt.Printf("%v\n", int2time(int(math.Sqrt(float64(seconds)))))
}