Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Istomina lab 8 #622

Open
wants to merge 28 commits into
base: Istomina_Elena
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions golang/lab4/Calculate.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package Lab4
package lab4

import (
"math"
"fmt"
"math"
"strconv"

"isuct.ru/informatics2022/lab8"
)

func Calculate(a,b,x float64) float64 {
Expand All @@ -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)
}
30 changes: 30 additions & 0 deletions golang/lab7/Clothes.go
Original file line number Diff line number Diff line change
@@ -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
}
28 changes: 28 additions & 0 deletions golang/lab7/HouseholdGoods.go
Original file line number Diff line number Diff line change
@@ -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
}
29 changes: 29 additions & 0 deletions golang/lab7/PetFood.go
Original file line number Diff line number Diff line change
@@ -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
}
46 changes: 46 additions & 0 deletions golang/lab7/lab7.go
Original file line number Diff line number Diff line change
@@ -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)
}
52 changes: 52 additions & 0 deletions golang/lab8/lab8_1.go
Original file line number Diff line number Diff line change
@@ -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
}
78 changes: 78 additions & 0 deletions golang/lab8/lab8_2.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 4 additions & 2 deletions golang/main.go
Original file line number Diff line number Diff line change
@@ -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")
}
Expand Down
Loading