Prechádzať zdrojové kódy

fix: coding style and file format for chapter 9.

Bo-Yi Wu 9 rokov pred
rodič
commit
cee64d1429

+ 29 - 28
eBook/exercises/chapter_9/dlinked_list.go

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

+ 6 - 6
eBook/exercises/chapter_9/even/even.go

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

+ 35 - 33
eBook/exercises/chapter_9/fibo/fibonacci.go

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

+ 26 - 27
eBook/exercises/chapter_9/greetings/greetings.go

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

+ 61 - 62
eBook/exercises/chapter_9/main_fibo.go

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

+ 22 - 22
eBook/exercises/chapter_9/main_greetings.go

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

+ 13 - 13
eBook/exercises/chapter_9/main_oddeven.go

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

+ 15 - 14
eBook/exercises/chapter_9/size_int.go

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