go经典练习题涉及流程控制-字符串-struct-map的数据类型的处理

one:求1到100之间的质数

package main

import (
	"fmt"
)

func isPrime(n int) bool {
	var flag = true
	for j := 2; j < n; j++ {
		if n % j == 0 { //计算质数,如果这个数能被自己整除哪么它就是质数
			flag = false
			break
		}
	}
	return flag
}

func main() {
	var n int = 100
	for i := 2; i < n; i++ {
		if isPrime(i) {
			fmt.Printf("%d is prime
", i)
		}
	}
}

  

two:统计字符串中有哪些单词并统计出出现次数

package main

import (
	"fmt"
)

func addWord(wordCount map[string]int, chars []rune) {
	words := string(chars)
	if len(words) > 0 {
		count, ok := wordCount[words]
		if !ok {
			wordCount[words] = 1

		} else {
			wordCount[words] = count + 1
			
		}
	}	
}

func main() {
	str := "how are hou! you are welcome!"
	var tmp []rune
	var wordCount map[string]int = make(map[string]int, 10)
	var chars []rune = []rune(str)
	for i := 0; i < len(str); i++ {
        if str[i] >= 'a' && str[i] <= 'z'|| str[i] >= 'A' && str[i] <= 'Z' {
			tmp = append(tmp, chars[i])
		} else {
			addWord(wordCount, tmp)
			tmp = tmp[0:0]
		}
	}
	if len(tmp) > 0 {
		addWord(wordCount, tmp)
	}
	for k,v := range wordCount {
		fmt.Printf("key:%s v:%d
", k, v)	
	}
}

Three:统计字符串中每一个字符个数

package main

import (
	"fmt"
)

func addWord(charCount map[rune]int, char rune) {

 		count, ok := charCount[char]
		if !ok {
			charCount[char] = 1

		} else {
			charCount[char] = count + 1
			
		}
	
}

func main() {
	str := "how are hou! you are welcome!中国"
	var charCount map[rune]int = make(map[rune]int, 10)
	var chars []rune = []rune(str)
	for i := 0; i < len(chars); i++ {
			addWord(charCount, chars[i])
	}
	for k,v := range charCount {
		fmt.Printf("key:%c v:%d
", k, v)	
	}
}

  

Four:简易版学生信息管理系统

package main

import (
	"os"
	"fmt"
)

type Student struct {
	Id string
	Name string 
	Age int
	Sex string
	Score float32
}

func showMenu() {
	fmt.Printf("please select:
")
	fmt.Printf("1.添加学生信息
")
	fmt.Printf("2.修改学生信息
")
	fmt.Printf("3.显示学生列表
")
	fmt.Printf("4.退出
")

}

func getStudentInfo() Student {
	var stu Student
	fmt.Printf("Please input Id:
")
	fmt.Scanf("%s
",&stu.Id)
	
	fmt.Printf("Please input name:
")
	fmt.Scanf("%s
",&stu.Name)

	fmt.Printf("Please input Age:
")
	fmt.Scanf("%d
",&stu.Age)

	fmt.Printf("Please input Sex:
")
	fmt.Scanf("%s
",&stu.Sex)

	fmt.Printf("Please input Score:
")
	fmt.Scanf("%f
",&stu.Score)
	return stu
}

func addStudent(allStudent map[string]Student) {
	stu  := getStudentInfo()
	_, ok := allStudent[stu.Id]
	if ok {
		fmt.Printf("studnet %s is exists
",stu.Id)
		return 
	}
    allStudent[stu.Id] = stu
}

func modifyStudent(allStudent map[string]Student) {
	stu  := getStudentInfo()
	_, ok := allStudent[stu.Id]
	if !ok {
		fmt.Printf("studnet %s is not exists
",stu.Id)
		return 
	}
    allStudent[stu.Id] = stu
}

func showStudnetList(allStudent map[string]Student) {
    for _, val := range allStudent {
		fmt.Printf("Id:%s
Name:%s
Age:%d
Sex:%s
Score:%f
", val.Id, val.Name, val.Age, val.Sex, val.Score)

	}
}
func main() {
	var sallStudent map[string]Student = make(map[string]Student, 10)
	for {
		showMenu()
		var sel int
		fmt.Println("-------------------------")
		fmt.Scanf("%d
", &sel)
		switch sel {
		case 1:
			addStudent(sallStudent)
		case 2:
		case 3:
			showStudnetList(sallStudent)
		case 4:
            os.Exit(0)
		}
	}
}

  

塑造ing
原文地址:https://www.cnblogs.com/Brin-guo/p/9355517.html