Golang助学跟练班day5-跟练加餐练习题
1、学生结构体定义:
type Student struct { Name string // 学生姓名 Age int // 学生年龄 Gender string // 学生性别 }
2、嵌套课程信息的学生结构体定义:
type Course struct { Name string // 课程名称 Score float64 // 课程分数 } type Student struct { Name string // 学生姓名 Age int // 学生年龄 Gender string // 学生性别 Courses []Course // 学生课程列表 }
3、添加学生的函数定义:
func AddStudent(students []Student, student Student) []Student { students = append(students, student) return students }
4、根据学生姓名查找学生信息的函数定义:
func FindStudentByName(students []Student, name string) *Student { for i, student := range students { if student.Name == name { return &students[i] } } return nil }
5、嵌套班级代表的学生结构体定义:
type ClassMonitor struct { Name string // 班级代表姓名 Duty string // 班级代表职责 Access bool // 班级代表权限 } type Student struct { Name string // 学生姓名 Age int // 学生年龄 Gender string // 学生性别 Courses []Course // 学生课程列表 ClassMonitor ClassMonitor // 班级代表信息 }
6、设置班级代表的函数定义:
func SetClassMonitor(student *Student, classMonitor ClassMonitor) { student.ClassMonitor = classMonitor }
7、计算学生平均分的方法定义:
func (s *Student) CalculateAverageScore() float64 { totalScore := 0.0 for _, course := range s.Courses { totalScore += course.Score } return totalScore / float64(len(s.Courses)) }
8、输出学生信息的函数定义:
func PrintStudents(students []Student) { for _, student := range students { fmt.Printf("Name: %sn", student.Name) fmt.Printf("Age: %dn", student.Age) fmt.Printf("Average Score: %.2fn", student.CalculateAverageScore()) fmt.Println() } }
9、餐厅菜品结构体定义:
type Dish struct { Name string // 菜品名称 Price float64 // 菜品价格 Description string // 菜品描述 }
10、嵌套菜品列表的菜单结构体定义:
type Menu struct { Category string // 菜单分类 Dishes []Dish // 菜品列表 }