Тестовое Задание

main
Shuhrat Tulyaganov 2023-04-19 03:59:50 +03:00
commit c13c1a08a2
5 changed files with 126 additions and 0 deletions

4
Makefile 100644
View File

@ -0,0 +1,4 @@
.PHONY: build
build:
go build ./main.go
.DEFAULT_GOAL := build

0
Readme.md 100644
View File

5
go.mod 100644
View File

@ -0,0 +1,5 @@
module git.ukamnya.ru/stulyaganov/testtasks
go 1.20
require golang.org/x/text v0.9.0 // indirect

2
go.sum 100644
View File

@ -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=

115
main.go 100644
View File

@ -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))
}