go语言编写一个学生实验平台
时间: 2023-08-05 16:46:03 浏览: 94
好的,下面是一个简单的学生实验平台的Go语言实现,包括学生信息的增删改查和实验结果的上传和查看。
```go
package main
import (
"fmt"
"os"
"bufio"
)
type Student struct {
id string
name string
age int
}
type Experiment struct {
id string
name string
result string
}
func addStudent() {
var student Student
fmt.Println("请输入学生ID:")
fmt.Scanln(&student.id)
fmt.Println("请输入学生姓名:")
fmt.Scanln(&student.name)
fmt.Println("请输入学生年龄:")
fmt.Scanln(&student.age)
// 将学生信息写入文件
file, err := os.OpenFile("students.txt", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
fmt.Println("文件打开失败:", err)
return
}
defer file.Close()
writer := bufio.NewWriter(file)
fmt.Fprintf(writer, "%s,%s,%d\n", student.id, student.name, student.age)
writer.Flush()
fmt.Println("添加成功!")
}
func listStudents() {
file, err := os.Open("students.txt")
if err != nil {
fmt.Println("文件打开失败:", err)
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
var student Student
fmt.Sscanf(line, "%s,%s,%d", &student.id, &student.name, &student.age)
fmt.Printf("ID:%s\t姓名:%s\t年龄:%d\n", student.id, student.name, student.age)
}
}
func deleteStudent() {
var id string
fmt.Println("请输入要删除的学生ID:")
fmt.Scanln(&id)
// 读取文件内容,找到需要删除的学生
file, err := os.Open("students.txt")
if err != nil {
fmt.Println("文件打开失败:", err)
return
}
defer file.Close()
reader := bufio.NewReader(file)
writer, err := os.OpenFile("students.tmp", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
fmt.Println("临时文件打开失败:", err)
return
}
defer writer.Close()
writerWriter := bufio.NewWriter(writer)
for {
line, err := reader.ReadString('\n')
if err != nil {
break
}
if line == "" {
continue
}
var student Student
fmt.Sscanf(line, "%s,%s,%d", &student.id, &student.name, &student.age)
if student.id != id {
fmt.Fprintf(writerWriter, "%s,%s,%d\n", student.id, student.name, student.age)
}
}
writerWriter.Flush()
// 删除原始文件并将临时文件重命名
os.Remove("students.txt")
os.Rename("students.tmp", "students.txt")
fmt.Println("删除成功!")
}
func editStudent() {
var id string
fmt.Println("请输入要编辑的学生ID:")
fmt.Scanln(&id)
// 读取文件内容,找到需要编辑的学生
file, err := os.Open("students.txt")
if err != nil {
fmt.Println("文件打开失败:", err)
return
}
defer file.Close()
reader := bufio.NewReader(file)
writer, err := os.OpenFile("students.tmp", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
fmt.Println("临时文件打开失败:", err)
return
}
defer writer.Close()
writerWriter := bufio.NewWriter(writer)
for {
line, err := reader.ReadString('\n')
if err != nil {
break
}
if line == "" {
continue
}
var student Student
fmt.Sscanf(line, "%s,%s,%d", &student.id, &student.name, &student.age)
if student.id == id {
fmt.Println("请输入学生姓名:")
fmt.Scanln(&student.name)
fmt.Println("请输入学生年龄:")
fmt.Scanln(&student.age)
fmt.Fprintf(writerWriter, "%s,%s,%d\n", student.id, student.name, student.age)
} else {
fmt.Fprintf(writerWriter, "%s,%s,%d\n", student.id, student.name, student.age)
}
}
writerWriter.Flush()
// 删除原始文件并将临时文件重命名
os.Remove("students.txt")
os.Rename("students.tmp", "students.txt")
fmt.Println("编辑成功!")
}
func addExperiment() {
var experiment Experiment
fmt.Println("请输入实验ID:")
fmt.Scanln(&experiment.id)
fmt.Println("请输入实验名称:")
fmt.Scanln(&experiment.name)
fmt.Println("请输入实验结果:")
fmt.Scanln(&experiment.result)
// 将实验结果写入文件
file, err := os.OpenFile("experiments.txt", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
fmt.Println("文件打开失败:", err)
return
}
defer file.Close()
writer := bufio.NewWriter(file)
fmt.Fprintf(writer, "%s,%s,%s\n", experiment.id, experiment.name, experiment.result)
writer.Flush()
fmt.Println("添加成功!")
}
func listExperiments() {
file, err := os.Open("experiments.txt")
if err != nil {
fmt.Println("文件打开失败:", err)
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
var experiment Experiment
fmt.Sscanf(line, "%s,%s,%s", &experiment.id, &experiment.name, &experiment.result)
fmt.Printf("ID:%s\t名称:%s\t结果:%s\n", experiment.id, experiment.name, experiment.result)
}
}
func main() {
for {
fmt.Println("请选择操作:")
fmt.Println("1. 添加学生")
fmt.Println("2. 查看学生")
fmt.Println("3. 删除学生")
fmt.Println("4. 编辑学生")
fmt.Println("5. 添加实验结果")
fmt.Println("6. 查看实验结果")
fmt.Println("0. 退出")
var choice int
fmt.Scanln(&choice)
switch choice {
case 1:
addStudent()
case 2:
listStudents()
case 3:
deleteStudent()
case 4:
editStudent()
case 5:
addExperiment()
case 6:
listExperiments()
case 0:
return
default:
fmt.Println("无效的操作")
}
}
}
```
这个实现使用了文件存储学生和实验信息。学生信息保存在`students.txt`文件中,每行包括学生ID、姓名和年龄,用逗号分隔。实验结果保存在`experiments.txt`文件中,每行包括实验ID、名称和结果,也用逗号分隔。
运行程序后,根据提示进行相应的操作即可。
阅读全文