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

Выполнил 7 лабораторную #493

Merged
merged 5 commits into from
Dec 23, 2024
Merged
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
1 change: 1 addition & 0 deletions golang/Task2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Запись текста в файл
31 changes: 31 additions & 0 deletions golang/lab7/Car.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package lab7

import "fmt"

type Car struct {
Name string
Model string
Price float64
}

func NewCar(name string, model string, price float64) *Car {
e := new(Car)
e.Name = name
e.Model = model
e.Price = price
return e
}

func (e *Car) SetPrice(price float64) { e.Price = price }
func (e Car) GetPrice() float64 { return e.Price }
func (e *Car) SetName(name string) { e.Name = name }
func (e Car) GetName() string { return e.Name }
func (e *Car) SetModel(model string) { e.Model = model }

func (e *Car) ApplyDiscount(discount float64) {
if discount > 0 && discount <= 100 {
e.SetPrice(e.GetPrice() * (1 - discount/100))
} else {
fmt.Println("Неверная скидка")
}
}
31 changes: 31 additions & 0 deletions golang/lab7/Magazine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package lab7

import "fmt"

type Magazine struct {
Name string
Publisher string
Price float64
}

func NewMagazine(name string, publisher string, price float64) *Magazine {
m := new(Magazine)
m.Name = name
m.Publisher = publisher
m.Price = price
return m
}

func (m *Magazine) SetPrice(price float64) { m.Price = price }
func (m Magazine) GetPrice() float64 { return m.Price }
func (m *Magazine) SetName(name string) { m.Name = name }
func (m Magazine) GetName() string { return m.Name }
func (m *Magazine) SetPublisher(publisher string) { m.Publisher = publisher }

func (m *Magazine) ApplyDiscount(discount float64) {
if discount > 0 && discount <= 100 {
m.SetPrice(m.GetPrice() * (1 - discount/100))
} else {
fmt.Println("Неверная скидка")
}
}
51 changes: 51 additions & 0 deletions golang/lab7/lab7.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package lab7

import (
"fmt"
)

type Product interface {
GetName() string
GetPrice() float64
SetPrice(price float64)
ApplyDiscount(discount float64)
}

func CalculateTotalCost(products []Product) float64 {
var total float64
for _, product := range products {
total += product.GetPrice()
}
return total
}

func RunLab7() {
magazine := &Magazine{
Name: "Всё для дома",
Publisher: "ЧитайГород",
Price: 200,
}

car := &Car{
Name: "Ваз",
Model: "VAZ2109",
Price: 2500000,
}

products := []Product{magazine, car}

totalBeforeDiscount := CalculateTotalCost(products)
fmt.Printf("Общая стоимость до применения скидок: %.2f\n", totalBeforeDiscount)

products[0].ApplyDiscount(10)
products[1].ApplyDiscount(5)

totalAfterDiscount := CalculateTotalCost(products)
fmt.Printf("Общая стоимость после применения скидок: %.2f\n", totalAfterDiscount)

car.SetModel("VAZ3109")
magazine.SetName("Всё для дома")

fmt.Println(car)
fmt.Println(magazine)
}
10 changes: 10 additions & 0 deletions golang/lab8/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
2.5
4.6
1.15
3.05
0.38
1.2
1.36
1.57
1.93
2.25
5 changes: 5 additions & 0 deletions golang/lab8/lab8.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package lab8

func CompleteLab8() {

}
22 changes: 22 additions & 0 deletions golang/lab8/task1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package lab8

import (
"fmt"
"os"
)

const Numbers = "lab8/input.txt"

func ReadNumbers() {
data, err := os.Open("lab8/input.txt")
if err != nil {
panic(err)
}
defer data.Close()

dataContent, err := os.ReadFile("lab8/input.txt")
if err != nil {
panic(err)
}
fmt.Println(string(dataContent))
}
21 changes: 21 additions & 0 deletions golang/lab8/task2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package lab8

import (
"fmt"
"os"
)

func FileCreation() {
file, err := os.Create("Task2.txt")
if err != nil {
fmt.Println("Не удалось создать файл")
os.Exit(1)
}
defer file.Close()
fmt.Println(file.Name())
written, err := file.WriteString("Запись текста в файл")
if err != nil {
panic(err)
}
fmt.Printf("Записано %#v байт в новый файл\n", written)
}
5 changes: 5 additions & 0 deletions golang/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ import (

"isuct.ru/informatics2022/lab4"
"isuct.ru/informatics2022/lab6"
"isuct.ru/informatics2022/lab7"
"isuct.ru/informatics2022/lab8"
)

func main() {
fmt.Println("Трофимов Илья Михайлович")
lab6.Lab6()
lab4.CompleteLab4()
lab7.RunLab7()
lab8.ReadNumbers()
lab8.FileCreation()
}
Loading