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

Lab4-8 #529

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
b7b8983
Update main.go
Alexander56330 Dec 17, 2024
3cb1c6b
Create lab6.go
Alexander56330 Dec 17, 2024
c899539
Create lab7.go
Alexander56330 Dec 17, 2024
cc7fa74
Create lab8.go
Alexander56330 Dec 17, 2024
b592bf8
Update lab6.go
Alexander56330 Dec 17, 2024
f9fb7a1
Update lab7.go
Alexander56330 Dec 17, 2024
231e1fe
Create bibika.go
Alexander56330 Dec 17, 2024
7e73a22
Create mebel.go
Alexander56330 Dec 17, 2024
759ec6f
Create product.go
Alexander56330 Dec 17, 2024
729a696
Create productInterface.go
Alexander56330 Dec 17, 2024
a3ab19e
Create vkusnyshki.go
Alexander56330 Dec 17, 2024
cab799a
Update lab8.go
Alexander56330 Dec 17, 2024
bfe3501
Create input.txt
Alexander56330 Dec 17, 2024
005b811
Create readWriteFunc.go
Alexander56330 Dec 17, 2024
66885ab
Create readinput.go
Alexander56330 Dec 17, 2024
2d5849e
Update readWriteFunc.go
Alexander56330 Dec 17, 2024
e0f0dea
Update readWriteFunc.go
Alexander56330 Dec 17, 2024
63eb04a
Update lab7.go
Alexander56330 Dec 17, 2024
1c2e0ee
Update readWriteFunc.go
Alexander56330 Dec 17, 2024
2437902
Update lab7.go
Alexander56330 Dec 17, 2024
005fe40
Update lab6.go
Alexander56330 Dec 17, 2024
3de8716
Update lab4.go
Alexander56330 Dec 17, 2024
b8bef4c
Update lab7.go
Alexander56330 Dec 17, 2024
87cb0ff
Update lab7.go
Alexander56330 Dec 18, 2024
66c088d
Update readWriteFunc.go
Alexander56330 Dec 18, 2024
7b2e758
Create lab9.go
Alexander56330 Dec 22, 2024
5f786cf
Update main.go
Alexander56330 Dec 22, 2024
40640de
Create Genius-ideas.go
Alexander56330 Dec 22, 2024
0170467
Update lab9.go
Alexander56330 Dec 22, 2024
d2972cb
Create Rabota.go
Alexander56330 Dec 22, 2024
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
2 changes: 1 addition & 1 deletion golang/lab4/lab4.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

// Вариант 5: Первое уравнение.
// Функция для вычисления значения
// Функция для вычисления значения:
func CalculateY(a, elements float64) float64 {
numerator := math.Pow(math.Log10(math.Pow(a, 2)+elements), 2)
denominator := (a + elements) * (a + elements)
Expand Down
28 changes: 28 additions & 0 deletions golang/lab6/lab6.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Вариант 5: Компьютер.
package lab6

import "fmt"

type Computer struct {
Name string
Color string
Capacity float64
}

func NewComputer(name, color string, Capacity float64) *Computer {
c := new(Computer)
c.Name = name
c.Color = color
c.Capacity = Capacity
return c
}
func (c *Computer) SetCapacity(Capacity float64) { c.Capacity = Capacity }
func (c Computer) GetCapacity() float64 { return c.Capacity }
func (c Computer) GetColor() float64 { return c.Capacity }

func Completelab6() {
DELL := NewComputer("ДЕЛЛ", "чёрный", 128.0)
DELL.SetCapacity(256.0)
fmt.Println(DELL.GetCapacity())
fmt.Println(DELL.GetColor())
}
48 changes: 48 additions & 0 deletions golang/lab7/bibika.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package lab7

import "errors"

var ErrColor error = errors.New("invalid color")
var ErrMaxHorsepower error = errors.New("the wrong maxHorsepower value")

type Color string

const (
Gold Color = "золотой"
Chrome Color = "хром"
White Color = "белый"
Black Color = "чёрный"
)

type bibika struct {
product
color Color
maxHorsepower float64
}

func (c *bibika) SetColor(color Color) error {
for _, standardColor := range []Color{Gold, Chrome, White, Black} {
if color == standardColor {
c.color = color
return nil
}
}
return ErrColor
}

func (c *bibika) SetHorsepower(maxHorsepower float64) error {
if maxHorsepower < 0 || maxHorsepower > 1000 {
return ErrMaxHorsepower
}
c.maxHorsepower = maxHorsepower
return nil
}

func Newbibika(id int, name string, price float64, color Color, maxHorsepower float64) *bibika {
c := &bibika{
product: newProduct(id, name, price),
}
c.SetColor(color)
c.SetHorsepower(maxHorsepower)
return c
}
42 changes: 42 additions & 0 deletions golang/lab7/lab7.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package lab7

import (
"fmt"
"log"
)

func Completelab7() {
var err error

priora := Newbibika(0, "анаконда", 100000, Gold, 200)
sofa := Newmebel(1, "диван", 1000, Skin)
armchear := Newmebel(2, "кресло", 10, Wood)
shaurma := Newvkusnyshki(3, "шаурма", 5)

var ProductList []Product = []Product{
priora,
sofa,
armchear,
shaurma,
}

fmt.Printf("общяя стоимость товара до скидок: %v \n", GetTotalPrice(ProductList))
for _, product := range ProductList {
err = product.SetDiscount(10)
if err != nil {
log.Fatal(err)
}
}
fmt.Printf("общяя стоимость товара после скидок в 10 процентов: %v \n", GetTotalPrice(ProductList))

err = priora.SetColor(Black)
if err != nil {
log.Fatal(err)
}
err = sofa.SetMaterial(Diamonds)
if err != nil {
log.Fatal(err)
}

displayListProducts(ProductList)
}
37 changes: 37 additions & 0 deletions golang/lab7/mebel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package lab7

import "errors"

var ErrMaterial error = errors.New("invalid material")

type Material string

const (
Skin Material = "кожа"
Wood Material = "дерево"
Diamonds Material = "бриллианты"
Dsp Material = "дсп"
)

type mebel struct {
product
material Material
}

func (f *mebel) SetMaterial(material Material) error {
for _, standardmaterial := range []Material{Skin, Wood, Diamonds, Dsp} {
if material == standardmaterial {
f.material = material
return nil
}
}
return ErrMaterial
}

func Newmebel(id int, name string, price float64, material Material) *mebel {
f := &mebel{
product: newProduct(id, name, price),
}
f.SetMaterial(material)
return f
}
43 changes: 43 additions & 0 deletions golang/lab7/product.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package lab7

import "errors"

var ErrPercentageValue error = errors.New("the wrong percentage value")

type product struct {
id int
name string
price float64
discount float64
}

func (p product) GetPrice() float64 { return p.price }

func (p product) GetName() string { return p.name }

func (p product) GetDiscount() float64 { return p.discount }

func (p *product) SetDiscount(percent float64) error {
if percent < 1 || percent > 100 {
return ErrPercentageValue
}
p.discount = percent

change := (p.price / 100) * percent

if change > p.price {
p.price = 0
return nil
}
p.price -= change
return nil
}

func newProduct(id int, name string, price float64) product {
return product{
id: id,
name: name,
price: price,
discount: 0,
}
}
28 changes: 28 additions & 0 deletions golang/lab7/productInterface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package lab7

import "fmt"

type Product interface {
SetDiscount(float64) error
GetDiscount() float64
GetPrice() float64
GetName() string
}

func displayListProducts(PList []Product) {
fmt.Println("____список товаров_____")
for _, p := range PList {
fmt.Printf(
"________________________\nтовар \"%v\" стоит %v$ благодоря скидке в %v процентов\n",
p.GetName(), p.GetPrice(), p.GetDiscount())
}
}

func GetTotalPrice(ProductList []Product) float64 {
var totalPrice float64

for _, product := range ProductList {
totalPrice += product.GetPrice()
}
return totalPrice
}
12 changes: 12 additions & 0 deletions golang/lab7/vkusnyshki.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package lab7

type vkusnyshki struct {
product
}

func Newvkusnyshki(id int, name string, price float64) *vkusnyshki {
f := &vkusnyshki{
product: newProduct(id, name, price),
}
return f
}
9 changes: 9 additions & 0 deletions golang/lab8/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-2.5
3.5
6.5
0.6
2.89
3.54
5.21
6.28
3.48
26 changes: 26 additions & 0 deletions golang/lab8/lab8.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package lab8

import (
"fmt"
"log"
)

func Completelab8() {
path := "lab8/lab8.txt"

errCreateFile := CreateFile(path)
if errCreateFile != nil {
log.Println(errCreateFile)
}

err := WriteFile(path)
if err != nil {
log.Fatalf("(Completelab8) запись файла: %v", err)
}

searchText, errInput := InputText("текст для поиска")
if errInput != nil {
log.Fatalf("(Completelab8) ввод текста для поиска: %v", errInput)
}
fmt.Print(SearchInFile(path, searchText))
}
105 changes: 105 additions & 0 deletions golang/lab8/readWriteFunc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package lab8

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

var errorFileAlreadyExists = errors.New("фаил с таким же именем уже есть")
var errSearchInFile = errors.New("в файле нету нужного текста")

func CreateFile(path string) error {
_, errStat := os.Stat(path)
if errStat == nil {
return errorFileAlreadyExists
}

file, errCreate := os.Create(path)
if errCreate != nil {
return fmt.Errorf("(CreateFile) создание файла %s: %w", path, errCreate)
}
file.Close()

return nil
}

func WriteFile(path string) error {
file, errOpenFile := os.OpenFile(path, os.O_WRONLY, 0666)
if errOpenFile != nil {
return fmt.Errorf("(WriteFile) открытие файла %s: %w", path, errOpenFile)
}
defer file.Close()

text, err := InputText("текст который будет в файле")
if err != nil {
return fmt.Errorf("(WriteFile) ввод: %w", err)
}

file.WriteString(text)

return nil
}

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

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

func SearchInFile(path string, searchText string) (int, error) {
var lineNum int = 1
var sign int = 0
StringFile, err := ReadFile(path)
if err != nil {
return 0, fmt.Errorf("(SearchInFile)ReadFile %s: %w", path, err)
}

for _, f := range StringFile {
log.Printf("N символ: %v, N строка: %v\n чтение: %v, поиск: %v\n", sign, lineNum, string(f), string(searchText[sign]))
if byte(f) == '\n' {
lineNum++
}
if byte(f) == searchText[sign] {
log.Println("\t", f, "=", searchText[sign])
sign++
} else {
log.Println("\t", f, "!=", searchText[sign])
sign = 0
}
if sign == len(searchText) {
return lineNum, nil
}
}
return 0, errSearchInFile
}

func InputText(text string) (string, error) {
var in *bufio.Reader = bufio.NewReader(os.Stdin)

fmt.Printf("Введите %v: ", text)
text, err := in.ReadString('\n')
if err != nil {
return "", err
}
text = strings.Replace(text, "\n", "", -1)
text = strings.Replace(text, "\r", "", -1)
return text, nil
}
Loading
Loading