TestTasks/main.go

116 lines
2.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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