Go程序設計語言練習題(7.1-7.10)


7.1:使用類似ByteCounter的想法,實現單詞和行的計數器,實現時考慮使用bufio.ScanWords。

package main

import (
    "bufio"
    "fmt"
)

type WordsCounter int

func (c *WordsCounter) Write(content []byte) (int, error) {
    for start := 0; start < len(content); {
        //跳過開頭的space,返回遇到第一個word后下次scan的開始index
        //Hello Worlds 調用bufio.ScanWords返回
        //6 [Hello的字節slice] nil
        advance, _, err := bufio.ScanWords(content[start:], true)
        if err != nil {
            return 0, err
        }
        start += advance
        (*c)++
    }
    return int(*c), nil
}

type LinesCounter int

func (c *LinesCounter) Write(content []byte) (int, error) {
    for start := 0; start < len(content); {
        advance, _, err := bufio.ScanLines(content[start:], true)
        if err != nil {
            return 0, err
        }
        start += advance
        (*c)++
    }
    return int(*c), nil
}

func main() {
    var wc WordsCounter
    wc.Write([]byte("Hello Worlds Test Me"))
    fmt.Println(wc) // 4
    wc.Write([]byte("append something to the end"))
    fmt.Println(wc) // 9

    var lc LinesCounter
    fmt.Fprintf(&lc, "%s\n%s\n%s\n", "Hello World", "Second Line", "Third Line")
    fmt.Println(lc) // 3
    fmt.Fprintf(&lc, "%s\n%s\n%s", "第4行", "第5行", "")
    fmt.Println(lc) // 5
}
View Code

7.2:實現一個滿足如下簽名的CountingWriter函數,輸入一個io.Writer,輸出一個封裝了輸入值的心Writer,以及一個置項int64的指針,改制真對應的值是新的Writer吸入的字節數。

package main

import (
    "fmt"
    "io"
    "os"
)

type CountWriter struct {
    Writer io.Writer
    Count  int
}

func (cw *CountWriter) Write(content []byte) (int, error) {
    n, err := cw.Writer.Write(content)
    if err != nil {
        return n, err
    }
    cw.Count += n
    return n, nil
}

func CountingWriter(writer io.Writer) (io.Writer, *int) {
    cw := CountWriter{
        Writer: writer,
    }
    return &cw, &(cw.Count)
}

func main() {
    cw, counter := CountingWriter(os.Stdout)
    fmt.Fprintf(cw, "%s", "Print somethind to the screen...")
    fmt.Println(*counter)
    cw.Write([]byte("Append soething..."))
    fmt.Println(*counter)
}
View Code

7.3:為gopl.io/ch4/treesort中的*tree類型寫一個String方法,用於展示其中的值序列。

package main

import (
    "fmt"
    "math/rand"
)

type tree struct {
    value       int
    left, right *tree
}

func (t *tree) String() string {
    res := ""
    if t == nil {
        return res
    }
    res += t.left.String()
    res = fmt.Sprintf("%s %d", res, t.value)
    res += t.right.String()
    return res
}
func buildTree(data []int) *tree {
    var root = new(tree)
    for _, v := range data {
        root = add(root, v)
    }
    return root
}
func add(t *tree, e int) *tree {
    if t == nil {
        t = new(tree)
        t.value = e
        return t
    }

    if e < t.value {
        t.left = add(t.left, e)
    } else {
        t.right = add(t.right, e)
    }
    return t
}

func main() {
    data := make([]int, 50)
    for i := range data {
        data[i] = rand.Int() % 50
    }
    root := buildTree(data)
    fmt.Println(root)

    //空指針
    fmt.Println(new(tree))

    //只有根節點
    root = new(tree)
    root.value = 100
    fmt.Println(root)

    //沒有右子樹
    data = []int{5, 4, 3, 2, 1}
    root = buildTree(data)
    fmt.Println(root)

    //沒有左子樹
    data = []int{1, 3, 2, 4, 5}
    root = buildTree(data)
    fmt.Println(root)
}
View Code

7.4:strings.NewReader函數輸入一個字符串,返回一個從字符串讀取數據並滿足io.Reader接口的值。請實現該函數。

package main

import (
    "fmt"
    "io"
)

type StringReader struct {
    data string
    n    int
}

func (sr *StringReader) Read(b []byte) (int, error) {
    data := []byte(sr.data)
    if sr.n >= len(data) {
        return 0, io.EOF
    }

    data = data[sr.n:]
    n := 0
    if len(b) >= len(data) {
        n = copy(b, data)
        sr.n = sr.n + n
        return n, nil
    }
    n = copy(b, data)
    sr.n = sr.n + n
    return n, nil
}

func NewReader(in string) *StringReader {
    sr := new(StringReader)
    sr.data = in
    return sr
}

func main() {
    str := "Hello World"
    sr := NewReader(str)
    data := make([]byte, 10)
    n, err := sr.Read(data)
    for err == nil {
        fmt.Println(n, string(data[0:n]))
        n, err = sr.Read(data)
    }
}
View Code

7.5:io包中的LimitReader函數接受一個io.Reader r和字節數n,返回一個Reader,該返回值從r讀取數據,但在讀取n字節后報告文件結束,請實現該函數。

func LimitReader(r io.Reader,n int64) io.Reader

package main

import (
    "fmt"
    "io"
    "os"
)

type LimitedReader struct {
    Reader  io.Reader
    Limit   int
    current int
}

func (r *LimitedReader) Read(b []byte) (int, error) {
    if r.current >= r.Limit {
        return 0, io.EOF
    }

    if r.current+len(b) > r.Limit {
        b = b[:r.Limit-r.current]
    }
    n, err := r.Reader.Read(b)
    if err != nil {
        return n, err
    }
    r.current += n
    return n, nil
}

func LimitReader(r io.Reader, limit int) io.Reader {
    lr := LimitedReader{
        Reader: r,
        Limit:  limit,
    }
    return &lr
}

func main() {
    file, err := os.Open("limit.txt") // 1234567890
    if err != nil {
        panic(err)
    }
    defer file.Close()

    lr := LimitReader(file, 5)
    buf := make([]byte, 10)
    n, err := lr.Read(buf)
    if err != nil {
        panic(err)
    }
    fmt.Println(n, buf) // 5 [49 50 51 52 53 0 0 0 0 0]
}
View Code

7.8:(先實現一部分)將多個排序方法存到一個slice中,先按最后的方法排序,再按之前的方法排序。

package main

import (
    "fmt"
    "sort"
)

type book struct {
    name   string
    price  float64
    author string
}
type byFunc func(i, j int) bool
type tableSlice struct {
    lists     []*book
    lessFuncs []byFunc
}

func (ts tableSlice) Len() int {
    return len(ts.lists)
}
func (ts tableSlice) Swap(i, j int) {
    ts.lists[i], ts.lists[j] = ts.lists[j], ts.lists[i]
}
func (ts tableSlice) Less(i, j int) bool {
    for t := len(ts.lessFuncs) - 1; t >= 0; t-- {
        if ts.lessFuncs[t](i, j) {
            return true
        } else if !ts.lessFuncs[t](j, i) {
            continue
        } else {
            return false
        }
    }
    return false
}

func (ts tableSlice) byName(i, j int) bool {
    return ts.lists[i].name < ts.lists[j].name
}
func (ts tableSlice) byPrice(i, j int) bool {
    return ts.lists[i].price < ts.lists[j].price
}

func main() {
    book1 := book{"GoLang", 65.50, "Aideng"}
    book2 := book{"PHP", 45.50, "Sombody"}
    book3 := book{"C", 45.50, "Tan"}
    ts := tableSlice{
        lists: []*book{&book1, &book2, &book3},
    }
    ts.lessFuncs = []byFunc{ts.byName, ts.byPrice}
    sort.Sort(ts)
    for _, book := range ts.lists {
        fmt.Println(*book)
    }
}
View Code

7.10:寫一個函數IsPalindrome(s sort.Interface)bool判斷一個序列是否是回文。

package palindrome

import (
    "fmt"
    "sort"
)

func IsPalindrome(s sort.Interface) bool {
    if s.Len() == 0 {
        return false
    }
    i, j := 0, s.Len()-1
    for i < j {
        if !s.Less(i, j) && !s.Less(j, i) {
            i++
            j--
        } else {
            return false
        }
    }
    return true
}

func main() {
    a := []int{1, 2, 3, 2, 1}
    fmt.Println(IsPalindrome(sort.IntSlice(a))) // true
    a = []int{2, 1, 3, 4, 5}
    fmt.Println(IsPalindrome(sort.IntSlice(a))) //false
    a = []int{1}
    fmt.Println(IsPalindrome(sort.IntSlice(a))) // true
    a = []int{}
    fmt.Println(IsPalindrome(sort.IntSlice(a))) // false
}
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM