8.1 班上有30個學生,每個學生的信息包括學號、姓名、性別、年齡、三門課的成績。要求建立學生信息的結構體student,輸入這30個學生的信息,然後打印輸出各項數據。
package main
import (
"fmt"
)
type student struct {
num string
name string
sex string
age int
score [3]float32
}
func main() {
var stu [30]student
for i := 0; i < len(stu); i++ {
fmt.Scan(&stu[i].num, &stu[i].name, &stu[i].sex, &stu[i].age, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2])
}
for _, v := range stu {
v.print()
}
}
func (stu *student) print() {
fmt.Println("學號:", stu.num)
fmt.Println("姓名:", stu.name)
fmt.Println("年齡:", stu.age)
fmt.Println("性別:", stu.sex)
fmt.Println("數學:", stu.score[0])
fmt.Println("語文:", stu.score[1])
fmt.Println("英語:", stu.score[2], "\n")
}
8.2 有4名學生,每個學生包括學號、姓名、成績,編寫函數找出成績最高學生的學號、姓名和成績。
package main
import (
"fmt"
)
type student struct {
num string
name string
score float32
}
func main() {
var stu = [4]student{{"001", "Tom", 90}, {"002", "Jim", 77}, {"003", "Pony", 99}, {"004", "Lily", 90}}
var max float32 = stu[0].score
var index int
for i, v := range stu {
if max < v.score {
max = v.score
index = i
}
}
fmt.Println(stu[index])
}
8.4 有一批圖書,每本書有書名、作者、書號、出版日期4項數據,希望既可以通過書名查詢,也可以使用作者或書名來查詢圖書。編寫方法來實現此功能,如果查到,打印出此書的書名、作者、書號和出版日期信息。如果查不到此書,則打印出“無此書”。
package main
import (
"errors"
"fmt"
"strings"
)
type Book struct {
Name string
Author string
Isdn string
Date string
}
func main() {
var books = []Book{
{"Clojure程序設計", "Aaron Bedra", "9787115308474", "2013年4月"},
{"Go語言程序設計", "王鵬", "9787302347231", "2014年1月"},
{"TCP Sockets編程", "Jesse Stirumer", "9787115330529", "2013年10月"},
{"Haskell趣學指南", "Miran Lipovaca", "9787115335593", "2014年1月"}}
for {
var field, value string
switch menu() {
case 1:
field = "name"
fmt.Print("請輸入書名:")
fmt.Scan(&value)
case 2:
field = "author"
fmt.Print("請輸入作者:")
fmt.Scan(&value)
case 3:
field = "isdn"
fmt.Print("請輸入ISDN:")
fmt.Scan(&value)
default:
return
}
if b, err := find(books, field, value); err == nil {
fmt.Println(b)
} else {
fmt.Println(err)
}
}
}
func find(books []Book, field, value string) (b Book, err error) {
for _, v := range books {
if v.IsMatch(field, value) {
return v, nil
}
}
return b, errors.New("無此書")
}
func (b *Book) IsMatch(field, value string) (r bool) {
switch field {
case "name":
r = strings.Contains(b.Name, value)
case "author":
r = strings.Contains(b.Author, value)
case "isdn":
r = strings.Contains(b.Isdn, value)
default:
r = false
}
return
}
func menu() int {
fmt.Println("1.根據書名查詢")
fmt.Println("2.根據作者查詢")
fmt.Println("3.根據書號查詢")
fmt.Println("4.退出")
fmt.Print("請選擇:")
var choice int
fmt.Scan(&choice)
return choice
}
8.5 有兩個單鏈表a,b。設節點中包含學號、姓名。從鏈表a中刪除所有與鏈表b中學號相同的節點。
package main
import (
"fmt"
)
type Student struct {
Id int
Name string
}
type Node struct {
Student
Next *Node
}
type LinkList struct {
head *Node
tail *Node
}
func main() {
var link, another *LinkList
var stu = [4]Student{{1, "Timi"}, {2, "Jack"}, {3, "Lucy"}, {4, "Lily"}}
link = link.Create()
for _, v := range stu {
link.Add(v)
}
fmt.Print("LinkList A:")
link.Print()
another = another.Create()
another.Add(stu[0])
another.Add(stu[1])
another.Add(stu[3])
fmt.Print("\nLinkList B:")
another.Print()
link.Diff(another)
fmt.Print("\nA-B:")
link.Print()
}
func (link *LinkList) Create() *LinkList {
link = new(LinkList)
link.head = nil
link.tail = nil
return link
}
func (link *LinkList) Add(stu Student) {
if link == nil {
return
}
var node = new(Node)
node.Id = stu.Id
node.Name = stu.Name
node.Next = nil
if link.head != nil {
link.tail.Next = node
link.tail = node
} else {
link.head = node
link.tail = node
}
}
func (link *LinkList) Del(index int) {
if link.head == nil {
return
}
if index == 0 {
if link.head.Next == nil {
link.head = nil
link.tail = nil
} else {
link.head = link.head.Next
}
return
}
p := link.head
for i := 0; i < index-1 && p != nil; i++ {
p = p.Next
}
if p.Next != nil {
p.Next = p.Next.Next
} else {
p.Next = nil
link.tail = p
}
}
func (link *LinkList) Diff(another *LinkList) {
LOOP:
p1 := link.head
var index int
for p1 != nil {
p2 := another.head
for p2 != nil {
if p1.Id == p2.Id {
link.Del(index)
goto LOOP
}
p2 = p2.Next
}
index++
p1 = p1.Next
}
}
func (link *LinkList) Print() {
p := link.head
for p != nil {
fmt.Printf("%v->", p.Student)
p = p.Next
}
fmt.Print("nil")
}