diff --git a/golang/lab/lab4.go b/golang/lab/lab4.go new file mode 100644 index 00000000..958db95d --- /dev/null +++ b/golang/lab/lab4.go @@ -0,0 +1,54 @@ +package main + +import ( + "fmt" + "math" +) + +func CalculateY(a, b, x float64) float64 { + return math.Pow((a*x+b), 1.0/3.0) / math.Pow(math.Log10(x), 2) +} + +func TaskA(a, b, xStart, xEnd, step float64) []float64 { + var result []float64 + for x := xStart; x <= xEnd; x += step { + result = append(result, CalculateY(a, b, x)) + } + return result +} + +func TaskB(a, b float64, xValues []float64) []float64 { + var result []float64 + for _, x := range xValues { + result = append(result, CalculateY(a, b, x)) + } + return result +} + +func main() { + a := 2.0 + b := 3.0 + + xStart := 1.0 + xEnd := 5.0 + step := 1.0 + resultsA := TaskA(a, b, xStart, xEnd, step) + fmt.Println("Результаты TaskA:") + for i, y := range resultsA { + fmt.Printf("x = %.2f, y = %.6f\n", xStart+float64(i)*step, y) + } + + fmt.Println() + + xValues := []float64{1.5, 2.5, 3.5, 4.5} + resultsB := TaskB(a, b, xValues) + fmt.Println("Результаты TaskB:") + for i, y := range resultsB { + fmt.Printf("x = %.2f, y = %.6f\n", xValues[i], y) + } + fmt.Println() + x := 3.0 + resultY := CalculateY(a, b, x) + fmt.Printf("Результат CalculateY: x= %.2f, y = %.6f\n", x, resultY) +} + diff --git a/golang/lab/lab6.go b/golang/lab/lab6.go new file mode 100644 index 00000000..c6f945f9 --- /dev/null +++ b/golang/lab/lab6.go @@ -0,0 +1,35 @@ +package main + +import "fmt" + +type Dog struct { + Name string + Breed string + Age int +} + +func NewDog(name string, breed string, age int) Dog { + return Dog{Name: name, Breed: breed, Age: age} +} +func (d Dog) GetAge() int { + return d.Age +} +func (d *Dog) SetAge(age int) { + d.Age = age +} +func (d Dog) GetInfo() string { + return fmt.Sprintf("Name: %s, Breed: %s, Age: %d", d.Name, d.Breed, d.Age) +} + +func main() { + dog1 := NewDog("Buddy", "Golden Retriever", 3) + fmt.Println(dog1.GetInfo()) + + fmt.Println("Возраст:", dog1.GetAge()) + dog1.SetAge(5) + fmt.Println("Новый возраст:", dog1.GetAge()) + fmt.Println(dog1.GetInfo()) + + dog2 := NewDog("Bella", "Labrador", 7) + fmt.Println(dog2.GetInfo()) +} diff --git a/golang/lab/lab7.go b/golang/lab/lab7.go new file mode 100644 index 00000000..ea2c5b2e --- /dev/null +++ b/golang/lab/lab7.go @@ -0,0 +1,79 @@ +package main + +import ( + "fmt" +) + +type Product interface { + GetPrice() float64 + SetPrice(price float64) + ApplyDiscount(percent float64) + GetDetails() string +} + +type Book struct { + title string + author string + price float64 +} + +func (b *Book) GetPrice() float64 { + return b.price +} + +func (b *Book) SetPrice(price float64) { + b.price = price +} + +func (b *Book) ApplyDiscount(percent float64) { + b.price -= b.price * percent / 100 +} + +func (b *Book) GetDetails() string { + return fmt.Sprintf("Book: %s, Author: %s, Price: %.2f", b.title, b.author, b.price) +} + +type Electronics struct { + name string + brand string + price float64 +} + +func (e *Electronics) GetPrice() float64 { + return e.price +} + +func (e *Electronics) SetPrice(price float64) { + e.price = price +} + +func (e *Electronics) ApplyDiscount(percent float64) { + e.price -= e.price * percent / 100 +} + +func (e *Electronics) GetDetails() string { + return fmt.Sprintf("Electronics: %s, Brand: %s, Price: %.2f", e.name, e.brand, e.price) +} + +func CalculateTotalPrice(products []Product) float64 { + total := 0.0 + for _, product := range products { + total += product.GetPrice() + } + return total +} + +func main() { + book := Book{title: "The Go Programming Language", author: "Alan Donovan", price: 50.00} + electronics := Electronics{name: "Laptop", brand: "XYZ", price: 1200.00} + + fmt.Println(book.GetDetails()) + fmt.Println(electronics.GetDetails()) + + products := []Product{&book, &electronics} + totalPrice := CalculateTotalPrice(products) + fmt.Printf("Общая стоимость: %.2f\n", totalPrice) + fmt.Println() + book.ApplyDiscount(10) + fmt.Println("Цена со скидкой:", book.GetDetails()) +}