fix: coding style and file format for chapter 9.

This commit is contained in:
Bo-Yi Wu
2017-02-11 12:30:01 +08:00
parent d9041c7fc3
commit cee64d1429
8 changed files with 207 additions and 205 deletions

View File

@@ -1,28 +1,29 @@
// Q20_linked_list.go // Q20_linked_list.go
package main package main
import ( import (
"fmt" "container/list"
"container/list" "fmt"
) )
func main() { func main() {
lst := list.New() lst := list.New()
lst.PushBack(100) lst.PushBack(100)
lst.PushBack(101) lst.PushBack(101)
lst.PushBack(102) lst.PushBack(102)
// fmt.Println("Here is the double linked list:\n", lst) // fmt.Println("Here is the double linked list:\n", lst)
for e := lst.Front(); e != nil; e = e.Next() { for e := lst.Front(); e != nil; e = e.Next() {
// fmt.Println(e) // fmt.Println(e)
fmt.Println(e.Value) fmt.Println(e.Value)
} }
} }
/* Example output:
&{0x12542bc0 <nil> 0x12547590 1} /* Example output:
&{0x12542ba0 0x12542be0 0x12547590 2} &{0x12542bc0 <nil> 0x12547590 1}
&{<nil> 0x12542bc0 0x12547590 4} &{0x12542ba0 0x12542be0 0x12547590 2}
&{<nil> 0x12542bc0 0x12547590 4}
100
101 100
102 101
*/ 102
*/

View File

@@ -1,6 +1,6 @@
// even.go // even.go
package even package even
func Even(i int) bool { // exported function func Even(i int) bool { // exported function
return i%2 == 0 return i%2 == 0
} }

View File

@@ -1,33 +1,35 @@
package fibo package fibo
/* /*
func Fibonacci(n int) (res int) { func Fibonacci(n int) (res int) {
if n <= 1 { if n <= 1 {
res = 1 res = 1
} else { } else {
res = Fibonacci(n-1) + Fibonacci(n-2) res = Fibonacci(n-1) + Fibonacci(n-2)
} }
return return
} }
*/ */
// accepts a general operation op: // accepts a general operation op:
func Fibonacci(op string, n int) (res int) { func Fibonacci(op string, n int) (res int) {
if n <= 1 { if n <= 1 {
switch op { switch op {
case "+": case "+":
res = 1 res = 1
case "*": case "*":
res = 2 res = 2
default: res = 0 default:
} res = 0
} else { }
switch op { } else {
case "+": switch op {
res = Fibonacci(op, n-1) + Fibonacci(op, n-2) case "+":
case "*": res = Fibonacci(op, n-1) + Fibonacci(op, n-2)
res = Fibonacci(op, n-1) * Fibonacci(op, n-2) case "*":
default: res = 0 res = Fibonacci(op, n-1) * Fibonacci(op, n-2)
} default:
} res = 0
return }
} }
return
}

View File

@@ -1,27 +1,26 @@
package greetings package greetings
import "time" import "time"
func GoodDay(name string) string { func GoodDay(name string) string {
return "Good Day " + name return "Good Day " + name
} }
func GoodNight(name string) string{ func GoodNight(name string) string {
return "Good Night " + name return "Good Night " + name
} }
func IsAM() bool { func IsAM() bool {
localTime := time.Now() localTime := time.Now()
return localTime.Hour() <= 12 return localTime.Hour() <= 12
} }
func IsAfternoon() bool { func IsAfternoon() bool {
localTime := time.Now() localTime := time.Now()
return localTime.Hour() <= 18 return localTime.Hour() <= 18
} }
func IsEvening() bool { func IsEvening() bool {
localTime := time.Now() localTime := time.Now()
return localTime.Hour() <= 22 return localTime.Hour() <= 22
} }

View File

@@ -1,62 +1,61 @@
package main package main
import ( import (
"fmt" "./fibo/fibo"
"./fibo/fibo" "fmt"
) )
var nextFibo int var nextFibo int
var op string var op string
func main() { func main() {
/* /*
result := 0 result := 0
for i:=0; i <= 10; i++ { for i:=0; i <= 10; i++ {
result = fibo.Fibonacci(i) result = fibo.Fibonacci(i)
fmt.Printf("fibonacci(%d) is: %d\n", i, result) fmt.Printf("fibonacci(%d) is: %d\n", i, result)
} }
*/ */
op = "+" op = "+"
calls() calls()
fmt.Println("Change of operation from + to *") fmt.Println("Change of operation from + to *")
nextFibo = 0 nextFibo = 0
op = "*" op = "*"
calls() calls()
} }
func calls() { func calls() {
next() next()
fmt.Println("...") fmt.Println("...")
next() next()
fmt.Println("...") fmt.Println("...")
next() next()
fmt.Println("...") fmt.Println("...")
next() next()
} }
func next() { func next() {
result := 0 result := 0
nextFibo++ nextFibo++
result = fibo.Fibonacci(op, nextFibo) result = fibo.Fibonacci(op, nextFibo)
fmt.Printf("fibonacci(%d) is: %d\n", nextFibo, result) fmt.Printf("fibonacci(%d) is: %d\n", nextFibo, result)
} }
/* *****************************************************************
Output is: /* *****************************************************************
fibonacci(1) is: 1 Output is:
... fibonacci(1) is: 1
fibonacci(2) is: 2 ...
... fibonacci(2) is: 2
fibonacci(3) is: 3 ...
... fibonacci(3) is: 3
fibonacci(4) is: 5 ...
Change of operation from + to * fibonacci(4) is: 5
fibonacci(1) is: 2 Change of operation from + to *
... fibonacci(1) is: 2
fibonacci(2) is: 4 ...
... fibonacci(2) is: 4
fibonacci(3) is: 8 ...
... fibonacci(3) is: 8
fibonacci(4) is: 32 ...
********************************************************************/ fibonacci(4) is: 32
********************************************************************/

View File

@@ -1,22 +1,22 @@
package main package main
import ( import (
"fmt" "./greetings/greetings"
"./greetings/greetings" "fmt"
) )
func main() { func main() {
name := "James" name := "James"
fmt.Println(greetings.GoodDay(name)) fmt.Println(greetings.GoodDay(name))
fmt.Println(greetings.GoodNight(name)) fmt.Println(greetings.GoodNight(name))
if greetings.IsAM() { if greetings.IsAM() {
fmt.Println("Good morning", name) fmt.Println("Good morning", name)
} else if greetings.IsAfternoon(){ } else if greetings.IsAfternoon() {
fmt.Println("Good afternoon", name) fmt.Println("Good afternoon", name)
} else if greetings.IsEvening(){ } else if greetings.IsEvening() {
fmt.Println("Good evening", name) fmt.Println("Good evening", name)
} else { } else {
fmt.Println("Good night", name) fmt.Println("Good night", name)
} }
} }

View File

@@ -1,13 +1,13 @@
// test_oddeven.go // test_oddeven.go
package main package main
import ( import (
"fmt" "./even/even"
"./even/even" "fmt"
) )
func main() { func main() {
for i:=0; i<=100; i++ { for i := 0; i <= 100; i++ {
fmt.Printf("Is the integer %d even? %v\n", i, even.Even(i)) fmt.Printf("Is the integer %d even? %v\n", i, even.Even(i))
} }
} }

View File

@@ -1,14 +1,15 @@
// size_int.go // size_int.go
package main package main
import ( import (
"fmt" "fmt"
"unsafe" "unsafe"
) )
func main() { func main() {
var i int = 10 var i int = 10
size := unsafe.Sizeof(i) size := unsafe.Sizeof(i)
fmt.Println("The size of an int is: ", size) fmt.Println("The size of an int is: ", size)
} }
// The size of an int is: 4
// The size of an int is: 4