• 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
  • 相关阅读:
    java实现简单web服务器(分析+源代码)
    Java中常见的5种WEB服务器介绍
    Android ViewDragHelper完全解析 自定义ViewGroup神器
    设置SVN,Git忽略MAC的.DS_Store文件的方法
    Android中图片大小和屏幕密度的关系讲解
    Android组件化方案及组件消息总线modular-event实战
    机器学习在美团配送系统的实践:用技术还原真实世界
    智能支付稳定性测试实战
    数据库智能运维探索与实践
    【人物志】技术十年:美团第一位前端工程师潘魏增
  • 原文地址:https://www.cnblogs.com/Brin-guo/p/9355517.html
Copyright © 2020-2023  润新知