adding cs142 code

This commit is contained in:
dm
2016-04-06 20:45:34 -07:00
parent 552d456d53
commit 8e52ce1982
174 changed files with 14064 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
include $(GOROOT)/src/Make.inc
TARG=binary
GOFILES=\
binary.go
include $(GOROOT)/src/Make.cmd
+26
View File
@@ -0,0 +1,26 @@
package main
import (
"fmt"
)
func itob(i int) string {
var buf [64]byte
j := len(buf)
b := 2
for i > 0 {
j--
buf[j] = "0123456789abcdefghipqrstuvwxyz"[i%b]
i /= b
}
return string(buf[j:])
}
func main() {
var i int
fmt.Print("please enter a number: ")
fmt.Scan(&i)
fmt.Printf("%d as a binary: %s\n", i, itob(i))
}
+7
View File
@@ -0,0 +1,7 @@
include $(GOROOT)/src/Make.inc
TARG=seconds
GOFILES=\
seconds.go
include $(GOROOT)/src/Make.cmd
+36
View File
@@ -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)))))
}
@@ -0,0 +1,7 @@
include $(GOROOT)/src/Make.inc
TARG=temp
GOFILES=\
temp.go
include $(GOROOT)/src/Make.cmd
@@ -0,0 +1,21 @@
package main
import (
"fmt"
)
func f2c(temp_F float32) float32 {
return 5.0/9.0 * (temp_F-32);
}
func main() {
var temp_F float32
fmt.Print("please enter a temperature: ")
fmt.Scan(&temp_F)
temp_C := f2c(temp_F)
fmt.Printf("%0.4fF as:\n", temp_F)
fmt.Printf("an int: %dF\n", int(temp_F))
fmt.Printf("a celsius float32: %0.4fC\n", temp_C)
fmt.Printf("a celcius int: %dC\n", int(temp_C))
}