用到了文件导入导出,字符串拼接,分割,随机数生成
题库txt示例, :分割 |结尾
package main
import (
"bufio"
"crypto/rand"
"fmt"
"io/ioutil"
"log"
"math/big"
"os"
"strings"
)
// 题目结构体
type structTimu struct {
title string
text string
}
var tiku []structTimu
var errtiku []structTimu
// 错题导入
func errtikuFunc(path string){
data, err := ioutil.ReadFile(path) //读取题库
if err != nil {
fmt.Println("File reading error", err)
return
}
//实例化
var timu structTimu
text := string(data) //类型转换
parts:=strings.Split(text,"|") //读取每一道题目
t :=0 //计数器
for _,i := range parts{
part := strings.Split(i,":") //对题目中以:做分割获取题目和答案
//如果切片长度不为2那么就是到最后或者是有问题,如果连续两次都错误那么就返回
if len(part)!=2 {
t=t+1
if t==2 {
break
}
continue
}
timu.title = part[0]
timu.text = part[1]
errtiku = append(errtiku, timu)
}
}
// 题库导入
func tikuFunc(path string){
data, err := ioutil.ReadFile(path) //读取题库
if err != nil {
fmt.Println("File reading error", err)
return
}
//实例化
var timu structTimu
text := string(data) //类型转换
parts:=strings.Split(text,"|") //读取每一道题目
t :=0 //计数器
for _,i := range parts{
part := strings.Split(i,":") //对题目中以:做分割获取题目和答案
//如果切片长度不为2那么就是到最后或者是有问题,如果连续两次都错误那么就返回
if len(part)!=2 {
t=t+1
if t==2 {
break
}
continue
}
timu.title = part[0]
timu.text = part[1]
tiku = append(tiku, timu)
}
}
// SimilarText 相似性
func SimilarText(first, second string, percent *float64) int {
var similarText func(string, string, int, int) int
similarText = func(str1, str2 string, len1, len2 int) int {
var sum, max int
pos1, pos2 := 0, 0
// Find the longest segment of the same section in two strings
for i := 0; i < len1; i++ {
for j := 0; j < len2; j++ {
for l := 0; (i+l < len1) && (j+l < len2) && (str1[i+l] == str2[j+l]); l++ {
if l+1 > max {
max = l + 1
pos1 = i
pos2 = j
}
}
}
}
if sum = max; sum > 0 {
if pos1 > 0 && pos2 > 0 {
sum += similarText(str1, str2, pos1, pos2)
}
if (pos1+max < len1) && (pos2+max < len2) {
s1 := []byte(str1)
s2 := []byte(str2)
sum += similarText(string(s1[pos1+max:]), string(s2[pos2+max:]), len1-pos1-max, len2-pos2-max)
}
}
return sum
}
l1, l2 := len(first), len(second)
if l1+l2 == 0 {
return 0
}
sim := similarText(first, second, l1, l2)
if percent != nil {
*percent = float64(sim*200) / float64(l1+l2)
}
return sim
}
func main() {
//题库导入
tikuFunc("./小程序/tiku.txt")
errtikuFunc("./小程序/errtiku.txt")
var a,t int
for true {
fmt.Println("输入1.开始抽题,2.抽取错题,3.显示所有错题,0.退出系统")
fmt.Printf("请输入操作:")
stdin := bufio.NewReader(os.Stdin)
fmt.Fscan(stdin, &a)
if a == 1{
if len(tiku)<=0{
fmt.Println("暂时没有题目")
continue
}
result, _ := rand.Int(rand.Reader, big.NewInt(int64(len(tiku))))
fmt.Printf("题目为:%s\n",tiku[result.Int64()].title)
fmt.Printf("请输入1会,0不会:")
stdin := bufio.NewReader(os.Stdin)
fmt.Fscan(stdin, &t)
if t==1{
txt := ""
fmt.Printf("请输入答案:")
stdin := bufio.NewReader(os.Stdin)
fmt.Fscan(stdin, &txt)
fmt.Println(txt)
i := SimilarText(tiku[result.Int64()].text,txt,nil)
fmt.Println("正确答案为:",tiku[result.Int64()].text)
fmt.Println("相似度为:",i)
}else if t==0 {
fmt.Println("正确答案为:",tiku[result.Int64()].text)
errtiku = append(errtiku, tiku[result.Int64()])
fmt.Println("已加入错题集")
}
}else if a==2 {
if len(errtiku)<=0{
fmt.Println("暂时没有错题")
continue
}
result, _ := rand.Int(rand.Reader, big.NewInt(int64(len(errtiku))))
fmt.Printf("题目为:%s\n",errtiku[result.Int64()].title)
fmt.Printf("请输入1会,0不会:")
stdin := bufio.NewReader(os.Stdin)
fmt.Fscan(stdin, &t)
if t==1{
txt := ""
fmt.Printf("请输入答案:")
stdin := bufio.NewReader(os.Stdin)
fmt.Fscan(stdin, &txt)
fmt.Println(txt)
i := SimilarText(errtiku[result.Int64()].text,txt,nil)
fmt.Println("正确答案为:",errtiku[result.Int64()].text)
fmt.Println("相似度为:",i)
errtiku = append(errtiku[:result.Int64()],errtiku[result.Int64()+1:]...)
}else if t==2 {
fmt.Println("正确答案为:",errtiku[result.Int64()].text)
}
}else if a==3 {
if len(errtiku)<=0{
fmt.Println("暂时没有错题")
continue
}
for inx,ti := range errtiku {
fmt.Println(inx+1,".题目:",ti.title)
fmt.Println("答案", ti.text)
}
}else if a==0 {
file, err := os.OpenFile("./小程序/errtiku.txt", os.O_CREATE, 0666)
if err != nil {
return
}
// 关闭文件
defer file.Close()
for _,ti := range errtiku {
// 字符串写入
_, err = file.WriteString(ti.title+":" + ti.text+"|")
if err != nil {
log.Println(err)
return
}
}
break
}
}
}