小张同学的浓度脚本

package main

import (

    "encoding/csv"

    "bufio"

    "fmt"

    "io"

    "io/ioutil"

    "math"

    "os"

    "os/exec"

    "path/filepath"

    "strconv"

    "strings"

    "time"

)

func main() {

    dir := getCurrentFilePath()

    fmt.Println(dir)

    fmt.Println("lwt")

    fmt.Println(os.Args[1])

    Cycle  := os.Args[1]

    int ,err:= strconv.ParseFloat(Cycle, 64)

    if err != nil {

        fmt.Println("命令行 跟一个周期数字 !")

        return

    }

    readDir(dir,int)

    fmt.Println("complete!  press any key to close this window!")

    var in string

    fmt.Scanf("%s", &in)

}

func readDir(dir string,Cycle float64) {

    rd, err := ioutil.ReadDir(dir)

    if err != nil {

        fmt.Println("read dir error!")

        return

    }

    for _, file := range rd {

        filename := file.Name()

        fmt.Println("filename: ", filename)

        if strings.Contains(filename, ".conc") { // find  this file

            fmt.Println("filename: ", filename)

            f, err := os.Open(filename)

            inputReader := bufio.NewReader(f)

            // if inputReader == "" {

            //  fmt.Println("readfile error!!!!!!!")

            //  return

            // }

            if err != nil {

                fmt.Println("open " + filename + "failed!!!!!")

            }

            var titleMapONE []string

            var temp_compare_result []float64  // 单行对比 结果

            var period_compare_result []float64  // 周期内做和

            var period_compare_result_average []string  // 周期内做和

            inputStringOne, readerErrorOne := inputReader.ReadString('\n')

            if readerErrorOne == io.EOF {

                return

            }

            titleMapONE = formatTitleString(inputStringOne)

            // fmt.Println(titleMapONE)

            t1 := time.Now()

            s2 :=filename+t1.Format("2006-1-2 15-04-05")

            xlsname := s2+".xls"

            ft,err:= os.Create(xlsname)

            if err != nil {

                fmt.Println("create " + xlsname + "failed!!!!!")

            }

            defer ft.Close()

            ft.WriteString("\xEF\xBB\xBF") // 写入UTF-8 BOM

            w := csv.NewWriter(ft)

            // var countryCapitalMap []float64

            w.Write(titleMapONE)

            i := 0

            tempday := 0

            items := make([][]float64, int(Cycle))

            for {

                inputString, readerError := inputReader.ReadString('\n')

                if readerError == io.EOF {

                    return

                }

                     // 数据列

                    val := formatString(inputString) //val: [12 45 8]

                    mod := i % int(Cycle)

                    if len(items[mod]) == 0 { // 这一行还没有上一次的数据

                        // no comparea

                    } else {                  // 上周期 有了 对比一下 1点 和1 点 

                        // comparea with

                        temp_compare_result = comparelow(items[mod], val)

                    }

                    items[mod] = val

                    // fmt.Println("dd", mod, items[mod])

                    // fmt.Println("temp_compare_result", temp_compare_result)

                    if(len(temp_compare_result)>0){

                                period_compare_result = add_temp(period_compare_result,temp_compare_result)

                                if(mod==(int(Cycle)-1)){

                                    tempday ++

                                        // fmt.Println(period_compare_result)

                                    period_compare_result_average = average_sum(period_compare_result,Cycle,float64(tempday))

                                    // fmt.Println(period_compare_result_average)


                                    w.Write(period_compare_result_average)

                                    w.Flush()

                                    period_compare_result =[]float64{}

                                }


                    }

                    i++


                // first := []string{}

                // seend := []string{}

            }


            fmt.Println(s2)

        }

    }

}

func average_sum(period_compare_result[]float64,num float64,tempday float64)[]string{

    var avg =make([]string, len(period_compare_result))

    for k, v := range period_compare_result {

        if(k==0){

            avg[k] = strconv.FormatFloat(tempday, 'E', -1, 64)

        }else{

            avg[k]=    strconv.FormatFloat(v/num , 'E', -1, 64)

        }


    }

    return avg

}

func add_temp(period_compare_result[]float64,temp_compare_result[]float64) []float64 {

    for k, v := range temp_compare_result {

        if(len(period_compare_result)>0){

            period_compare_result[k] = v+period_compare_result[k]

        }else{

            period_compare_result=temp_compare_result

        }


    }

    return period_compare_result

}

func comparelow(oldLow []float64, blow []float64) []float64 {

    var clow = oldLow

    // fmt.Println("222")

    for k, v := range oldLow {

        if(k!=0){

            if(v<0){

                clow[k] = 0

            }else{

                a := comppareNum(v, blow[k])

                clow[k] = a

            }



        }else{

            clow[k] =v

        }

    }

    return clow

}

func comppareNum(old float64, new float64) float64 {

    // fmt.Println("iii")

    cazhi := old - new

    cuhou := cazhi / new

    // fmt.Println("a差值", cazhi)

    // fmt.Println("anew", new)

    // fmt.Println("a 除后", cuhou)

    fa := math.Abs(cuhou)

    // fmt.Println(fa)

    if fa > 0.6 {

        // fa =

    } else {

    }

    return fa

}

func getCurrentFilePath() string {

    file, _ := exec.LookPath(os.Args[0])

    path, _ := filepath.Abs(file)

    dir, _ := filepath.Split(path)

    return dir

}

// 32->空格    9->tab

func formatString(str string) []float64 {

    srcLength := len(str)

    rtnSli := []float64{}

    // fmt.Println(str)

    // fmt.Println("zongdeyihang\n")

    i := 0

    for {

        if i >= srcLength {

            break

        }

        if str[i] >= 48 && str[i] <= 57 {

            // 开始循环取数据

            var tmp string

            var v2 float64

            // fmt.Println(str[i])

            // fmt.Println(string(str[i]))

            // fmt.Println("kankanshangminaddddddddd\n")

            for {

                i++

                if i >= srcLength {

                    break

                }

                if str[i] == 32 || str[i] == 10 {

                    break

                }

                tmp = tmp + string(str[i])

            }

            v2, _ = strconv.ParseFloat(tmp, 64)

            rtnSli = append(rtnSli, v2)

        }

        i++

    }

    return rtnSli

}

func formatTitleString(str string) []string {

    srcLength := len(str)

    fmt.Println("title")

    rtnSli := []string{}

    i := 0

    for {

        if i >= srcLength {

            break

        }

        if str[i] >= 65 && str[i] <= 122 {

            // 开始循环取数据

            var tmp string

            for {

                tmp = tmp + string(str[i])

                i++

                if i >= srcLength {

                    break

                }

                if str[i] == 32 || str[i] == 9 {

                    break

                }

            }

            rtnSli = append(rtnSli, tmp)

        }

        i++

    }

    return rtnSli

}

你可能感兴趣的:(小张同学的浓度脚本)