commit c13c1a08a246a48fa53ab64ce639be7310b65b3a Author: Shuhrat Tulyaganov Date: Wed Apr 19 03:59:50 2023 +0300 Тестовое Задание diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4e2cc05 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +.PHONY: build +build: + go build ./main.go +.DEFAULT_GOAL := build \ No newline at end of file diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..e69de29 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0885f7a --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.ukamnya.ru/stulyaganov/testtasks + +go 1.20 + +require golang.org/x/text v0.9.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..20cf523 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= diff --git a/main.go b/main.go new file mode 100644 index 0000000..549408e --- /dev/null +++ b/main.go @@ -0,0 +1,115 @@ +package main + +import ( + "encoding/xml" + "fmt" + "io" + "net/http" + "strconv" + "strings" + "time" + + "golang.org/x/text/encoding/charmap" +) + +type ValCurs struct { + XMLName xml.Name `xml:"ValCurs"` + Text string `xml:",chardata"` + Date string `xml:"Date,attr"` + Name string `xml:"name,attr"` + Valute []struct { + Text string `xml:",chardata"` + ID string `xml:"ID,attr"` + NumCode string `xml:"NumCode"` + CharCode string `xml:"CharCode"` + Nominal string `xml:"Nominal"` + Name string `xml:"Name"` + Value string `xml:"Value"` + } `xml:"Valute"` +} +type result struct { + name string + value float64 + CharCode string + date string +} + +func CharsetReader(char string, data io.Reader) (io.Reader, error) { + if char == "windows-1251" { + return charmap.Windows1251.NewDecoder().Reader(data), nil + } + return nil, fmt.Errorf("Кодировка не поддерживается") +} + +func MaxMinAvg(Curss []*ValCurs) (map[string]result, *float64) { + var min, max result + var agv, summ float64 + var summItem int + res := make(map[string]result) + for i := 0; i < len(Curss); i++ { + for j := 0; j < len(Curss[i].Valute); j++ { + val, err := strconv.ParseFloat(strings.Replace(Curss[i].Valute[j].Value, ",", ".", 1), 64) + if err != nil { + fmt.Println(err) + return nil, nil + } + + if val < min.value || min.value == 0 { + min.value = val + min.name = Curss[i].Valute[j].Name + min.CharCode = Curss[i].Valute[j].CharCode + min.date = Curss[i].Date + } + if val > max.value { + max.value = val + max.name = Curss[i].Valute[j].Name + max.CharCode = Curss[i].Valute[j].CharCode + max.date = Curss[i].Date + } + summ += val + summItem += i + } + } + res["Min"] = min + res["Max"] = max + agv = summ / float64(summItem) + return res, &agv +} + +func main() { + Curss := []*ValCurs{} + Client := http.Client{ + Timeout: time.Second * 10, + } + t := time.Now() + fmt.Println("Пожалуйста подождите....") + for i := 0; i <= 90; i++ { + buff := ValCurs{} + data := fmt.Sprint(t.Add((-2160 + 24*time.Duration(i)) * time.Hour).Format("02/01/2006")) + req, err := http.NewRequest("GET", "http://www.cbr.ru/scripts/XML_daily_eng.asp?date_req="+data, nil) + if err != nil { + fmt.Println(err) + } + req.Header.Add("User-Agent", "PostmanRuntime/7.31.3") + res, err := Client.Do(req) + if err != nil { + fmt.Println(err) + } + defer res.Body.Close() + dacoder := xml.NewDecoder(res.Body) + dacoder.CharsetReader = CharsetReader + err = dacoder.Decode(&buff) + + if err != nil { + fmt.Println(err) + } + Curss = append(Curss, &buff) + } + MaxMin, avg := MaxMinAvg(Curss) + fmt.Println("Минимальный Курс") + fmt.Println(MaxMin["Min"].value, " ", MaxMin["Min"].name, " ", MaxMin["Min"].CharCode, " ", MaxMin["Min"].date) + fmt.Println("Максимальный Курс") + fmt.Println(MaxMin["Max"].value, " ", MaxMin["Max"].name, " ", MaxMin["Max"].CharCode, " ", MaxMin["Max"].date) + fmt.Println("Среднее значение курса", float32(*avg)) + +}