adding cs142 code
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
include $(GOROOT)/src/Make.inc
|
||||
|
||||
TARG=seconds
|
||||
GOFILES=\
|
||||
seconds.go
|
||||
|
||||
include $(GOROOT)/src/Make.cmd
|
||||
@@ -0,0 +1,36 @@
|
||||
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)))))
|
||||
}
|
||||
Reference in New Issue
Block a user