Skip to content
Open

Lab8 #615

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
31 changes: 31 additions & 0 deletions golang/Laba7/book.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package lab7

type Book struct {
name string
price float64
varieties string
}

func (b *Book) GetName() string {
return b.name
}

func (b *Book) SetName(NewName string) {
b.name = NewName
}

func (b *Book) GetPrice() float64 {
return b.price
}

func (b *Book) SetPrice(NewPrice float64) {
b.price = NewPrice
}

func (b *Book) ApplyDiscount(discount float64) {
b.price = b.price * (100 - discount) / 100
}

func (b *Book) SetVarieties(NewVarieties string) {
b.varieties = NewVarieties
}
31 changes: 31 additions & 0 deletions golang/Laba7/chocolate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package lab7

type Chocolate struct {
name string
price float64
brand string
}

func (ch *Chocolate) GetName() string {
return ch.name
}

func (ch *Chocolate) SetName(NewName string) {
ch.name = NewName
}

func (ch *Chocolate) GetPrice() float64 {
return ch.price
}

func (ch *Chocolate) SetPrice(NewPrice float64) {
ch.price = NewPrice
}

func (ch *Chocolate) ApplyDiscount(discount float64) {
ch.price = ch.price * (100 - discount) / 100
}

func (ch *Chocolate) SetBrand(NewBrand string) {
ch.brand = NewBrand
}
31 changes: 31 additions & 0 deletions golang/Laba7/clothes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package lab7

type Clothes struct {
name string
price float64
material string
}

func (c *Clothes) GetName() string {
return c.name
}

func (c *Clothes) SetName(NewName string) {
c.name = NewName
}

func (c *Clothes) GetPrice() float64 {
return c.price
}

func (c *Clothes) SetPrice(NewPrice float64) {
c.price = NewPrice
}

func (c *Clothes) ApplyDiscount(discount float64) {
c.price = c.price * (100 - discount) / 100
}

func (c *Clothes) SetMaterial(NewMaterial string) {
c.material = NewMaterial
}
44 changes: 44 additions & 0 deletions golang/Laba7/lab7.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package lab7

import (
"fmt"
)

func CalculationSumProduct(listProducts []Product) string {
var sum float64 = 0
for _, product := range listProducts {
sum += product.GetPrice()
}
s := fmt.Sprintf("%.2f", sum)
return s
}

func Laba7() {
var jeans Product = &Clothes{name: "джинсы", price: 3000, material: "полиэстер"}
var chocolate Product = &Chocolate{name: "молочный шоколад", price: 70, brand: "Россия щедрая душа"}
var comic Product = &Book{name: "Sailor Moon", price: 700, varieties: "манга"}

if c, ok := jeans.(*Clothes); ok {
c.SetMaterial("вискоза")
} else {
fmt.Println("Ошибка приведения типа для Clothes")
}

if ch, ok := chocolate.(*Chocolate); ok {
ch.SetBrand("Alpen Gold")
} else {
fmt.Println("Ошибка приведения типа для Chocolate")
}

comic.SetPrice(800)

listProducts := []Product{jeans, chocolate, comic}
fmt.Printf("Сумма товаров, без учета скидки, равна: %v рублей \n", CalculationSumProduct(listProducts))
jeans.ApplyDiscount(10)
chocolate.ApplyDiscount(5)
comic.ApplyDiscount(15)
fmt.Printf("Сумма товаров, c учетом скидки, равна: %v рублей \n", CalculationSumProduct(listProducts))
fmt.Println(jeans)
fmt.Println(chocolate)
fmt.Println(comic)
}
9 changes: 9 additions & 0 deletions golang/Laba7/product.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package lab7

type Product interface {
GetName() string
SetName(NewName string)
GetPrice() float64
SetPrice(NewPrice float64)
ApplyDiscount(discount float64)
}
10 changes: 10 additions & 0 deletions golang/Laba8/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
7.2
1.3
1.56
4.71
0.63
2.4
2.8
3.9
4.7
3.16
28 changes: 28 additions & 0 deletions golang/Laba8/lab8.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package lab8

import (
"fmt"
)

func Laba8() {
var err error

err = CreateFile()
if err != nil {
panic(err)
}
err = WriteFile()
if err != nil {
panic(err)
}
text, err := ReadFile()
if err != nil {
panic(err)
}
fmt.Println(text)

err = task1()
if err != nil {
panic(err)
}
}
49 changes: 49 additions & 0 deletions golang/Laba8/task1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package lab8

import (
"io"
"os"
"strconv"
"strings"

lab4 "isuct.ru/informatics2022/Laba4"
)

const PathTask1 = "Laba8/input.txt"

func task1() error {
file, err := os.Open(PathTask1)
if err != nil {
return err
}
defer file.Close()

var text string
data := make([]byte, 64)
for {
n, err := file.Read(data)
if err == io.EOF {
break
}
text = string(data[:n])
}
var result []float64
Parameters := strings.Split(text, "\r\n")
for _, P := range Parameters {
number, err := strconv.ParseFloat(P, 64)
if err != nil {
return err
}
result = append(result, number)
}

a := result[0]
b := result[1]
xMin := result[2]
xMax := result[3]
xDelta := result[4]
lab4.CompleteTaskA(a, b, xMin, xMax, xDelta)
lab4.CompleteTaskB(a, b, result[6:])

return nil
}
68 changes: 68 additions & 0 deletions golang/Laba8/task2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package lab8

import (
"bufio"
"fmt"
"io"
"os"
"strings"
)

const Path = "Laba8/text.txt"

func CreateFile() error {
file, err := os.Create(Path)
if err != nil {
return err
}
file.Close()

return nil
}

func WriteFile() error {
file, err := os.OpenFile(Path, os.O_WRONLY, 0666)
if err != nil {
return err
}
defer file.Close()

fmt.Println("Ввидите текст для поиска в файле")
var in *bufio.Reader = bufio.NewReader(os.Stdin)
text, err := in.ReadString('\n')
if err != nil {
return err
}

file.WriteString(text)

return nil
}

func ReadFile() (string, error) {
file, err := os.Open(Path)
if err != nil {
return "", err
}
defer file.Close()

var text string
data := make([]byte, 64)
for {
n, err := file.Read(data)
if err == io.EOF {
break
}
text = string(data[:n])
}
return text, nil
}

func SearchText(searchText string) (bool, error) {
text, err := ReadFile()
if err != nil {
return false, err
}
search := strings.Contains(text, searchText)
return search, nil
}
4 changes: 4 additions & 0 deletions golang/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import (

lab4 "isuct.ru/informatics2022/Laba4"
lab6 "isuct.ru/informatics2022/Laba6"
lab7 "isuct.ru/informatics2022/Laba7"
lab8 "isuct.ru/informatics2022/Laba8"
)

func main() {
fmt.Println("Рындина Ксения Александровна")
lab4.CompleteLab4()
lab6.Laba6()
lab7.Laba7()
lab8.Laba8()
}
2 changes: 2 additions & 0 deletions golang/vendor/modules.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# github.com/stretchr/testify v1.8.1
## explicit
Loading