diff --git a/golang/lab4/Calculate.go b/golang/lab4/Calculate.go index 97b0ebf1..76157df0 100644 --- a/golang/lab4/Calculate.go +++ b/golang/lab4/Calculate.go @@ -1,8 +1,11 @@ -package Lab4 +package lab4 import ( - "math" "fmt" + "math" + "strconv" + + "isuct.ru/informatics2022/lab8" ) func Calculate(a,b,x float64) float64 { @@ -18,19 +21,41 @@ func TaskA(a, b, x1, x2, dx float64) []float64 { return y } -func TaskB(a float64, b float64, x[5] float64) []float64 { - var y []float64 +func TaskB(a float64, b float64, x[] float64) []float64 { + var arr []float64 for _, value := range x { - y = append(y, Calculate(a,b,value)) + arr = append(arr, Calculate(a,b, value)) } - return y + return arr +} + +func PrintValue(Values []float64) { + for _, value := range Values { + fmt.Println(value) + } +} + +func ReadFileTask4() []float64 { + fmt.Println("Введите X1, X2, dX для задачи A и список значений для задачи B") + arr := lab8.RunLab8() + var numbers []float64 + for _, values := range arr { + value, _ := strconv.ParseFloat(values, 64) + numbers = append(numbers, value) + } + return numbers } + func RunLab4 () { - a := 1.1 - b := 0.09 - - fmt.Println(TaskA(a,b,1.2,2.2,0.2)) - var s = [5]float64{1.21,1.76,2.53,3.48,4.52} - fmt.Println(TaskB(a, b, s)) + const a = 1.1 + const b = 0.09 + arr := ReadFileTask4() + slice := arr[3:] + + ValuesA := TaskA(a, b, arr[0], arr[1], arr[2]) + ValuesB := TaskB(a, b, slice) + + PrintValue(ValuesA) + PrintValue(ValuesB) } diff --git a/golang/lab7/Clothes.go b/golang/lab7/Clothes.go new file mode 100644 index 00000000..d2c8acaa --- /dev/null +++ b/golang/lab7/Clothes.go @@ -0,0 +1,30 @@ +package lab7 + +import "fmt" + +type Dress struct { + Description string + Fabric string + Brand string + Price float64 +} + +func (f Dress) GetDescription() { + fmt.Println("Это", f.Description, "из", f.Fabric, "от бренда", f.Brand ,"стоимостью", f.Price) +} + +func (f Dress) GetPrice() float64 { + return f.Price +} + +func (f *Dress) ApplyDiscount(x float64) { + (*f).Price = (f.Price) * (x) +} + +func (f *Dress) UpdatePrice(x float64) { + (*f).Price = x +} + +func (f *Dress) UpdateDescription(x string) { + (*f).Fabric = x +} diff --git a/golang/lab7/HouseholdGoods.go b/golang/lab7/HouseholdGoods.go new file mode 100644 index 00000000..79c8e897 --- /dev/null +++ b/golang/lab7/HouseholdGoods.go @@ -0,0 +1,28 @@ +package lab7 + +import "fmt" + +type Microwave_oven struct { + Name string + Price float64 +} + +func (f Microwave_oven) GetDescription() { + fmt.Println("Микроволновая печь", f.Name, "стоимостью", f.Price) +} + +func (f Microwave_oven) GetPrice() float64 { + return f.Price +} + +func (f *Microwave_oven) ApplyDiscount(x float64) { + (*f).Price = (f.Price) * (x) +} + +func (f *Microwave_oven) UpdatePrice(x float64) { + (*f).Price = x +} + +func (f *Microwave_oven) UpdateDescription(x string) { + (*f).Name = x +} diff --git a/golang/lab7/PetFood.go b/golang/lab7/PetFood.go new file mode 100644 index 00000000..b371e3fb --- /dev/null +++ b/golang/lab7/PetFood.go @@ -0,0 +1,29 @@ +package lab7 + +import "fmt" + +type CatFood struct { + Name string + Brand string + Price float64 +} + +func (f CatFood) GetDescription() { + fmt.Println("Корм для кошек", f.Name, "от бренда", f.Brand, "он стоит", f.Price) +} + +func (f CatFood) GetPrice() float64 { + return f.Price +} + +func (f *CatFood) ApplyDiscount(x float64) { + (*f).Price = (f.Price) * (x) +} + +func (f *CatFood) UpdatePrice(x float64) { + (*f).Price = x +} + +func (f *CatFood) UpdateDescription(x string) { + (*f).Name = x +} diff --git a/golang/lab7/lab7.go b/golang/lab7/lab7.go new file mode 100644 index 00000000..18441cc0 --- /dev/null +++ b/golang/lab7/lab7.go @@ -0,0 +1,46 @@ +package lab7 + +import "fmt" + +type Product interface { + GetDescription() + GetPrice() float64 + ApplyDiscount(discount float64) + UpdatePrice(newPrice float64) + UpdateDescription(newDescription string) +} + +func CalculatePrice(list []Product) float64 { + var sum float64 = 0 + for _, price := range list { + sum += price.GetPrice() + } + return sum +} + +func RunLab7() { + var CatFood Product = &CatFood{"Orijen Cat&Kitten Grin Free", "Orijen", 9900} + var Microwave_oven Product = &Microwave_oven{"Hotpoint MWHA 201", 10490} + var Dress Product = &Dress{"гипюровое платье", "полиэстера", "DStrend", 5983} + + CatFood.GetDescription() + Microwave_oven.GetDescription() + Dress.GetDescription() + + var purchase []Product = []Product{CatFood, Microwave_oven, Dress} + totalCost := CalculatePrice(purchase) + fmt.Println("Общая стоимость списка товаров:", totalCost) + + CatFood.UpdatePrice(9400) + Microwave_oven.UpdateDescription("Samsung") + Microwave_oven.ApplyDiscount(0.1) + Dress.ApplyDiscount(0.2) + Dress.UpdateDescription("хлопка") + + CatFood.GetDescription() + Microwave_oven.GetDescription() + Dress.GetDescription() + + totalCost = CalculatePrice(purchase) + fmt.Println("Общая стоимость списка товаров с учетом всех скидок:", totalCost) +} diff --git a/golang/lab8/lab8_1.go b/golang/lab8/lab8_1.go new file mode 100644 index 00000000..ee60030a --- /dev/null +++ b/golang/lab8/lab8_1.go @@ -0,0 +1,52 @@ +package lab8 + +import ( + "bufio" + "fmt" + "os" + "strconv" +) + +type Params struct { + A float64 + B float64 + x1 float64 + x2 float64 + dx float64 + slice []float64 +} + +func LoadParameters(filename string) (Params, error) { + file, err := os.Open(filename) + if err != nil { + return Params{}, fmt.Errorf("Не удалось открыть файл: %w", err) + } + defer file.Close() + + scanner := bufio.NewScanner(file) + var values []float64 + for scanner.Scan() { + line := scanner.Text() + num, err := strconv.ParseFloat(line, 64) + if err != nil { + return Params{}, fmt.Errorf("Ошибка преобразования строки '%s' в float: %w", line, err) + } + values = append(values, num) + } + + if err := scanner.Err(); err != nil { + return Params{}, fmt.Errorf("ошибка при чтении файла: %w", err) + } + if len(values) < 6 { + return Params{}, fmt.Errorf("Недостаточно значений. Введите еще раз") + } + parameters := Params{ + A: values[0], + B: values[1], + x1: values[2], + x2: values[3], + dx: values[4], + slice: values[5:], + } + return parameters, nil +} diff --git a/golang/lab8/lab8_2.go b/golang/lab8/lab8_2.go new file mode 100644 index 00000000..2621484c --- /dev/null +++ b/golang/lab8/lab8_2.go @@ -0,0 +1,78 @@ +package lab8 + +import ( + "fmt" + "os" + "io" + "strings" +) + +func CreateFile(name string) { + file, err := os.Create(name) + if err != nil { + panic(err) + } + defer file.Close() +} + + +func ReadFile(name string) []string { + var arr []string + file, err := os.Open(name) + if err != nil { + panic(err) + } + defer file.Close() + for { + data := make([]byte, 64) + for { + n, err := file.Read(data) + if err == io.EOF { + break + } + arr = append(arr, string(data[:n])) + break + } + return arr + } +} + +func WriteInFile(name string, count int) { + var value string + file, err := os.OpenFile(name, os.O_RDWR, 0666) + if err != nil { + panic(err) + } + defer file.Close() + for i := 1; i <= count; i++ { + fmt.Print("Введите данные: ") + fmt.Fscan(os.Stdin, &value) + file.WriteString(value + "\n") + } +} + +func SearchInFile(data string, search string) bool { + return strings.Contains(data, search) +} + +func RunLab8() []string { + var NameFile string + fmt.Print("Введите название файла: ") + fmt.Fscan(os.Stdin, &NameFile) + CreateFile(NameFile) + + var Count int + fmt.Print("Cколько значений вы хотите занести в файл: ") + fmt.Fscan(os.Stdin, &Count) + WriteInFile(NameFile, Count) + + arr := strings.Split(ReadFile(NameFile)[0], "\n") + arr = arr[0 : len(arr)-1] + + var Search string + fmt.Print("Какое значение вы хотите найти в файле: ") + fmt.Fscan(os.Stdin, &Search) + SearchInFile(NameFile, Search) + + return arr +} diff --git a/golang/main.go b/golang/main.go index 74f53540..b034dc82 100644 --- a/golang/main.go +++ b/golang/main.go @@ -1,14 +1,16 @@ package main import ( - "isuct.ru/informatics2022/lab4" "isuct.ru/informatics2022/lab6" + "isuct.ru/informatics2022/lab7" + "isuct.ru/informatics2022/lab8" "fmt" ) func main() { - Lab4.RunLab4() lab6.RunLab6() + lab7.RunLab7() + lab8.RunLab8() fmt.Println("Istomina_Elena_Sergeevna") }