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

Lab8.1 #524

Open
wants to merge 7 commits into
base: Beilin_Aleksei
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
8 changes: 4 additions & 4 deletions golang/labs/lab4/lab4.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ func calculate (b, x float64) float64 {
}


func compliteTaskA (b, xbegin, xend, xdelta float64) []float64 {
func CompliteTaskA (b, xbegin, xend, xdelta float64) []float64 {
var ys []float64
for x := xbegin; x < xend; x += xdelta {
ys = append(ys, calculate(b, x))
}
return ys
}

func compliteTaskB (b float64, xs []float64) []float64 {
func CompliteTaskB (b float64, xs []float64) []float64 {
var ys []float64
for _, x := range xs {
ys = append(ys, calculate(b, x))
Expand All @@ -36,8 +36,8 @@ func AnsLab4 () {
var xend float64 = 3.28
var xdelta float64 = 0.4
var xs []float64 = []float64{1.1, 2.4, 3.6, 1.7, 3.9}
var for_a = compliteTaskA(b, xbegin, xend, xdelta)
var for_b = compliteTaskB(b, xs)
var for_a = CompliteTaskA(b, xbegin, xend, xdelta)
var for_b = CompliteTaskB(b, xs)

fmt.Println("Решение задания А:")
for i, y := range for_a {
Expand Down
1 change: 1 addition & 0 deletions golang/labs/lab5/lab5.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package lab4_test
2 changes: 1 addition & 1 deletion golang/labs/lab7/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package lab7

import "fmt"

func AnsLab() {
func AnsLab7() {
var product1 car = car{"Тойота Камри 2021", 1450000, 4, "Легковые машины"}
var product2 motorbike = motorbike{"ElectroTown Citycoco X7 2000W PRO", 94500, 10, "Электромотоциклы"}
var product3 tire = tire{"Зимняя резина", 5000, 0, "Шины для машин"}
Expand Down
37 changes: 37 additions & 0 deletions golang/labs/lab8/createFile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package lab8

import (
"fmt"
"os"
)

func createFile(str string) (int) {
file, err := os.Create(str)
if err != nil {
fmt.Println("Не удалось создать файл, ошибка: ", err)
return 0
}
fmt.Print("Файл успешно создан\n")
defer file.Close()
return 1
}

func create() {
var str string

fmt.Print("Ведите название файла: ")
fmt.Scan(&str)
num := createFile(str)
if num == 1 {
var openChoose string
fmt.Print("Желаете записать в этот файл что-нибудь?\nДа/нет ")
fmt.Scan(&openChoose)
if openChoose == "Да" {
file, _ := os.OpenFile(str, os.O_RDWR, 0666)
defer file.Close()
writeToFile(file)
} else {
fmt.Print("\n")
}
}
}
9 changes: 9 additions & 0 deletions golang/labs/lab8/lab8.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package lab8

func AnsTask1 () {
ansTask1()
}

func AnsTask2 () {
ansTask2()
}
9 changes: 9 additions & 0 deletions golang/labs/lab8/numbersFromLab4
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
2.5
1.28
3.28
0.4
1.1
2.4
3.6
1.7
3.9
33 changes: 33 additions & 0 deletions golang/labs/lab8/readFile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package lab8

import (
"fmt"
"io"
"os"
)

func read(str string) {
file, err := os.Open(str)
if err != nil {
fmt.Println(err)
}
defer file.Close()

data := make([]byte, 64)

for {
n, err := file.Read(data)
if err == io.EOF {
break
}
fmt.Print(string(data[:n]))
}
}

func readFile() {
var str string

fmt.Print("Введите название файла, который надо открыть: ")
fmt.Scan(&str)
read(str)
}
74 changes: 74 additions & 0 deletions golang/labs/lab8/search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package lab8

import (
"bufio"
"errors"
"fmt"
"io"
"io/fs"
"os"
)

func search(file *os.File, search string) {
var searchCopy []rune = []rune(search)
var count int
var countRune int

reader := bufio.NewReader(file)

for {
line, err := reader.ReadString('\n')
var lineUse []rune = []rune(line)
if err != nil {
if err == io.EOF {
break
} else {
fmt.Println(err)
return
}
}

for _, lineRune := range(lineUse) {
for _, searchRune := range(searchCopy) {
if searchRune == lineRune {
countRune++
if countRune == len([]rune(search)) {
countRune = 0
count++
searchCopy = []rune(search)
} else {
searchCopy = append(searchCopy[:0], searchCopy[1:]...)
break
}
} else {
countRune = 0
}
}
}
}
fmt.Println("Количество найденных слов: ", count)
}

func searchingTextIntoFile () {
var strFile string
var strSearch string


fmt.Print("Введите название файла, в котором будет поиск: ")
fmt.Scanln(&strFile)
file, err := os.OpenFile(strFile, os.O_RDONLY, 0444)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
fmt.Print("Файла не существует, выбирите другой файл")
} else{
fmt.Print("Не удалось открыть файл, ошибка:", err)
fmt.Print("\n")
}
}
defer file.Close()

fmt.Print("Введите искомый текст: ")
fmt.Scanln(&strSearch)

search(file, strSearch)
}
69 changes: 69 additions & 0 deletions golang/labs/lab8/task1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package lab8

import (
"fmt"

"io"

"os"

"strconv"

"isuct.ru/informatics2022/labs/lab4"
)

func getNumbers() (b, xbegin, xend, xdelta float64, xs []float64) {
file, err := os.OpenFile("labs/lab8/numbersFromLab4", os.O_RDONLY, 0666)
if err != nil{
fmt.Println(err)
os.Exit(1)
}
defer file.Close()

data := make([]byte, 1)
var buf []string
var str string

for{
_, err := file.Read(data)
if err == io.EOF{
buf = append(buf, str)
break
} else if string(data) == "\n" {
buf = append(buf, str)
str = ""
continue
}
str += string(data)
}

b, _ = strconv.ParseFloat(buf[0], 64)
buf = append(buf[:0], buf[1:]...)
xbegin, _ = strconv.ParseFloat(buf[0], 64)
buf = append(buf[:0], buf[1:]...)
xend, _ = strconv.ParseFloat(buf[0], 64)
buf = append(buf[:0], buf[1:]...)
xdelta, _ = strconv.ParseFloat(buf[0], 64)
buf = append(buf[:0], buf[1:]...)
for _, num := range buf {
numfloat, _ := strconv.ParseFloat(num, 64)
xs = append(xs, numfloat)
}
return b, xbegin, xend, xdelta, xs
}

func ansTask1 () {
b, xbegin, xend, xdelta, xs := getNumbers()
var for_a = lab4.CompliteTaskA(b, xbegin, xend, xdelta)
var for_b = lab4.CompliteTaskB(b, xs)

fmt.Println("Решение задания А:")
for i, y := range for_a {
fmt.Printf("y%d = %f\n", i+1, y)
}

fmt.Println("Решение задания B:")
for i, y := range for_b {
fmt.Printf("y%d = %f\n", i+1, y)
}
}
32 changes: 32 additions & 0 deletions golang/labs/lab8/task2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package lab8

import (
"fmt"
)

func ansTask2 () {
for {
var mainChoose int = 0

fmt.Println("Что вы желаете сделать?")
fmt.Println("1 - Создать файл/пересоздать")
fmt.Println("2 - Записать в файл")
fmt.Println("3 - Вывести файл в консоль")
fmt.Println("4 - Найти текст в файле")
fmt.Println("5 - Выйти из программы")
fmt.Scan(&mainChoose)

if mainChoose == 1 {
create()
} else if mainChoose == 2 {
write()
} else if mainChoose == 3 {
readFile()
} else if mainChoose == 4 {
searchingTextIntoFile()
} else if mainChoose == 5 {
break
}
fmt.Print("\n")
}
}
58 changes: 58 additions & 0 deletions golang/labs/lab8/writeToFile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package lab8

import (
"errors"
"fmt"
"io/fs"
"os"
)

func writeToFile (file *os.File) () {
var str string
var strBuf string
fmt.Print("Введите желаемый текст (чтобы прекратить запись введите \"QAWSED\" после пробела): ")
for {
fmt.Scan(&str)
if str == "QAWSED" {
break
}else {
strBuf += " " + str
}
}
_, err := file.WriteString(fmt.Sprint(strBuf[1:], "\n"))
if err != nil {
fmt.Println("Не удалось записать, ошибка: ", err)
return
}
fmt.Print("Запись успешна\n")
return
}

func ifNotExists (str string) {
var createChoose string
fmt.Print("Файла не существует, желаете создать?\nДа/нет ")
fmt.Scan(&createChoose)
if createChoose == "Да" {
createFile(str)
} else {
fmt.Print("\n")
}
}

func write() {
var str string

fmt.Print("Введите название файла, в который хотите записать текст: ")
fmt.Scan(&str)
file, err := os.OpenFile(str, os.O_RDWR, 0666)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
ifNotExists(str)
} else{
fmt.Print("Не удалось открыть файл, ошибка: ", err)
fmt.Print("\n")
}
}
defer file.Close()
writeToFile(file)
}
Loading
Loading