From 932fc9a2a65d72e83ba0831f0f881563bd9974e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D0=B0=D0=BB=D0=B4=D0=B8=D0=BD=D0=B0=20=D0=94=D0=B0?= =?UTF-8?q?=D1=80=D1=8C=D1=8F?= Date: Sat, 7 Dec 2024 19:23:21 +0300 Subject: [PATCH 01/15] Baldina Lab8 --- golang/Lab8/Lab4.go | 94 ++++++++++++++++++++++++++ golang/Lab8/Lab8.go | 154 ++++++++++++++++++++++++++++++++++++++++++ golang/Lab8/input.txt | 7 ++ 3 files changed, 255 insertions(+) create mode 100644 golang/Lab8/Lab4.go create mode 100644 golang/Lab8/Lab8.go create mode 100644 golang/Lab8/input.txt diff --git a/golang/Lab8/Lab4.go b/golang/Lab8/Lab4.go new file mode 100644 index 00000000..17eefc2b --- /dev/null +++ b/golang/Lab8/Lab4.go @@ -0,0 +1,94 @@ +package Lab4 + +import ( + "bufio" + "fmt" + "math" + "os" + "strconv" +) + +func calculateY(b float64, x float64) float64 { + return (1 + math.Pow(math.Sin(math.Pow(b, 3)+math.Pow(x, 3)), 2)) / math.Cbrt(math.Pow(b, 3)+math.Pow(x, 3)) +} + +func taskA(bA float64, xStart float64, xEnd float64, deltaX float64) []float64 { + results := []float64{} + for x := xStart; x <= xEnd; x += deltaX { + y := calculateY(bA, x) + results = append(results, y) + } + return results +} + +func taskB(bB float64, xValues []float64) []float64 { + results := []float64{} + for _, x := range xValues { + y := calculateY(bB, x) + results = append(results, y) + } + return results +} + +func RunLab4() { + filename := "input.txt" + + values, err := readFile(filename) + if err != nil { + fmt.Println("Ошибка при чтении файла:", err) + return + } + + if len(values) < 2 { + fmt.Println("Недостаточно значений в файле") + return + } + + bA := values[0] + bB := values[1] + xValues := values[2:] + + xStart := 1.28 + xEnd := 3.28 + deltaX := 0.4 + + resultsA := taskA(bA, xStart, xEnd, deltaX) + fmt.Println("Результаты задания A:") + for i, y := range resultsA { + x := xStart + float64(i)*deltaX + fmt.Printf("x: %.2f, y: %.4f\n", x, y) + } + + resultsB := taskB(bB, xValues) + fmt.Println("\nРезультаты задания B:") + for i, y := range resultsB { + x := xValues[i] + fmt.Printf("x: %.2f, y: %.4f\n", x, y) + } +} + +func readFile(filename string) ([]float64, error) { + file, err := os.Open(filename) + if err != nil { + return nil, err + } + defer file.Close() + + var values []float64 + scanner := bufio.NewScanner(file) + + for scanner.Scan() { + line := scanner.Text() + value, err := strconv.ParseFloat(line, 64) + if err != nil { + fmt.Println("Ошибка при чтении числа:", err) + continue + } + values = append(values, value) + } + + if err := scanner.Err(); err != nil { + return nil, err + } + return values, nil +} diff --git a/golang/Lab8/Lab8.go b/golang/Lab8/Lab8.go new file mode 100644 index 00000000..3323a369 --- /dev/null +++ b/golang/Lab8/Lab8.go @@ -0,0 +1,154 @@ +package Lab8 + +import ( + "bufio" + "fmt" + "math" + "os" + "strconv" + "strings" +) + +func calculateY(b float64, x float64) float64 { + return (1 + math.Pow(math.Sin(math.Pow(b, 3)+math.Pow(x, 3)), 2)) / math.Cbrt(math.Pow(b, 3)+math.Pow(x, 3)) +} + +func taskA(bA float64, xStart float64, xEnd float64, deltaX float64) []float64 { + results := []float64{} + for x := xStart; x <= xEnd; x += deltaX { + y := calculateY(bA, x) + results = append(results, y) + } + return results +} + +func taskB(bB float64, xValues []float64) []float64 { + results := []float64{} + for _, x := range xValues { + y := calculateY(bB, x) + results = append(results, y) + } + return results +} + +func RunLab8() { + filename := "input.txt" + + createAndWriteFile(filename) + + values, err := readFile(filename) + if err != nil { + fmt.Println("Ошибка при чтении файла:", err) + return + } + + if len(values) < 2 { + fmt.Println("Недостаточно значений в файле") + return + } + + bA := values[0] + bB := values[1] + + xValues := values[2:] + + xStart := 1.28 + xEnd := 3.28 + deltaX := 0.4 + + resultsA := taskA(bA, xStart, xEnd, deltaX) + fmt.Println("Результаты задания A:") + for i, y := range resultsA { + x := xStart + float64(i)*deltaX + fmt.Printf("x: %.2f, y: %.4f\n", x, y) + } + + resultsB := taskB(bB, xValues) + fmt.Println("\nРезультаты задания B:") + for i, y := range resultsB { + x := xValues[i] + fmt.Printf("x: %.2f, y: %.4f\n", x, y) + } + + var searchTerm string + fmt.Print("\nВведите текст для поиска в файле: ") + fmt.Scanln(&searchTerm) + searchInFile(filename, searchTerm) +} + +func createAndWriteFile(filename string) { + file, err := os.Create(filename) + if err != nil { + fmt.Println("Ошибка при создании файла:", err) + return + } + defer file.Close() + + writer := bufio.NewWriter(file) + fmt.Println("Введите значения для записи в файл (введите 'exit' для завершения):") + scanner := bufio.NewScanner(os.Stdin) + for { + scanner.Scan() + text := scanner.Text() + if text == "exit" { + break + } + writer.WriteString(text + "\n") + } + + writer.Flush() + fmt.Println("Данные успешно записаны в файл:", filename) +} + +func readFile(filename string) ([]float64, error) { + file, err := os.Open(filename) + if err != nil { + return nil, err + } + defer file.Close() + + var values []float64 + scanner := bufio.NewScanner(file) + + for scanner.Scan() { + line := scanner.Text() + value, err := strconv.ParseFloat(line, 64) + if err != nil { + fmt.Println("Ошибка при чтении числа:", err) + continue + } + values = append(values, value) + } + + if err := scanner.Err(); err != nil { + return nil, err + } + return values, nil +} + +func searchInFile(filename string, searchTerm string) { + file, err := os.Open(filename) + if err != nil { + fmt.Println("Ошибка при открытии файла:", err) + return + } + defer file.Close() + + scanner := bufio.NewScanner(file) + found := false + for scanner.Scan() { + line := scanner.Text() + if strings.Contains(line, searchTerm) { + fmt.Println("Найдено:", line) + found = true + } + } + + if !found { + fmt.Println("Текст не найден в файле.") + } + + if err := scanner.Err(); err != nil { + fmt.Println("Ошибка при чтении файла:", err) + } +} diff --git a/golang/Lab8/input.txt b/golang/Lab8/input.txt new file mode 100644 index 00000000..adff80f9 --- /dev/null +++ b/golang/Lab8/input.txt @@ -0,0 +1,7 @@ +2.5 +3.0 +1.1 +2.4 +3.6 +1.7 +3.9 From 2520f6dbed37bdeb07c5ad733aec414c9556f35c Mon Sep 17 00:00:00 2001 From: BaldinaDaria Date: Sat, 7 Dec 2024 19:31:07 +0300 Subject: [PATCH 02/15] Update Lab4.go --- golang/Lab8/Lab4.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/golang/Lab8/Lab4.go b/golang/Lab8/Lab4.go index 17eefc2b..ad001d15 100644 --- a/golang/Lab8/Lab4.go +++ b/golang/Lab8/Lab4.go @@ -1,4 +1,4 @@ -package Lab4 +package Lab8 import ( "bufio" @@ -30,7 +30,7 @@ func taskB(bB float64, xValues []float64) []float64 { return results } -func RunLab4() { +func RunLab8() { filename := "input.txt" values, err := readFile(filename) From ff9b1a59f73dfa65e20c4a9229d7edd184c0a0d7 Mon Sep 17 00:00:00 2001 From: BaldinaDaria Date: Sat, 7 Dec 2024 19:37:36 +0300 Subject: [PATCH 03/15] Update Lab4.go --- golang/Lab8/Lab4.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/golang/Lab8/Lab4.go b/golang/Lab8/Lab4.go index ad001d15..cde53556 100644 --- a/golang/Lab8/Lab4.go +++ b/golang/Lab8/Lab4.go @@ -9,7 +9,11 @@ import ( ) func calculateY(b float64, x float64) float64 { - return (1 + math.Pow(math.Sin(math.Pow(b, 3)+math.Pow(x, 3)), 2)) / math.Cbrt(math.Pow(b, 3)+math.Pow(x, 3)) + denominator := math.Cbrt(math.Pow(b, 3) + math.Pow(x, 3)) + if denominator == 0 { + return math.Inf(1) + } + return (1 + math.Pow(math.Sin(math.Pow(b, 3)+math.Pow(x, 3)), 2)) / denominator } func taskA(bA float64, xStart float64, xEnd float64, deltaX float64) []float64 { From b194e59920b69fa9b9edbca23266bcfbaf679d17 Mon Sep 17 00:00:00 2001 From: BaldinaDaria Date: Sat, 7 Dec 2024 19:44:24 +0300 Subject: [PATCH 04/15] Update Lab4.go --- golang/Lab8/Lab4.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/golang/Lab8/Lab4.go b/golang/Lab8/Lab4.go index cde53556..6485f3a2 100644 --- a/golang/Lab8/Lab4.go +++ b/golang/Lab8/Lab4.go @@ -7,7 +7,6 @@ import ( "os" "strconv" ) - func calculateY(b float64, x float64) float64 { denominator := math.Cbrt(math.Pow(b, 3) + math.Pow(x, 3)) if denominator == 0 { @@ -15,7 +14,6 @@ func calculateY(b float64, x float64) float64 { } return (1 + math.Pow(math.Sin(math.Pow(b, 3)+math.Pow(x, 3)), 2)) / denominator } - func taskA(bA float64, xStart float64, xEnd float64, deltaX float64) []float64 { results := []float64{} for x := xStart; x <= xEnd; x += deltaX { @@ -24,7 +22,6 @@ func taskA(bA float64, xStart float64, xEnd float64, deltaX float64) []float64 { } return results } - func taskB(bB float64, xValues []float64) []float64 { results := []float64{} for _, x := range xValues { @@ -34,7 +31,7 @@ func taskB(bB float64, xValues []float64) []float64 { return results } -func RunLab8() { +func RunLab8(){ filename := "input.txt" values, err := readFile(filename) @@ -70,7 +67,6 @@ func RunLab8() { fmt.Printf("x: %.2f, y: %.4f\n", x, y) } } - func readFile(filename string) ([]float64, error) { file, err := os.Open(filename) if err != nil { From fd1c39251f2c6c82181781c733d3b82ae3744914 Mon Sep 17 00:00:00 2001 From: BaldinaDaria Date: Sat, 7 Dec 2024 19:47:51 +0300 Subject: [PATCH 05/15] Update Lab4.go --- golang/Lab8/Lab4.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/golang/Lab8/Lab4.go b/golang/Lab8/Lab4.go index 6485f3a2..7004fbef 100644 --- a/golang/Lab8/Lab4.go +++ b/golang/Lab8/Lab4.go @@ -7,23 +7,26 @@ import ( "os" "strconv" ) -func calculateY(b float64, x float64) float64 { + +func calculateY(b, x float64) float64 { denominator := math.Cbrt(math.Pow(b, 3) + math.Pow(x, 3)) if denominator == 0 { return math.Inf(1) } return (1 + math.Pow(math.Sin(math.Pow(b, 3)+math.Pow(x, 3)), 2)) / denominator } -func taskA(bA float64, xStart float64, xEnd float64, deltaX float64) []float64 { - results := []float64{} + +func taskA(bA, xStart, xEnd, deltaX float64) []float64 { + var results []float64 for x := xStart; x <= xEnd; x += deltaX { y := calculateY(bA, x) results = append(results, y) } return results } + func taskB(bB float64, xValues []float64) []float64 { - results := []float64{} + var results []float64 for _, x := range xValues { y := calculateY(bB, x) results = append(results, y) @@ -31,8 +34,8 @@ func taskB(bB float64, xValues []float64) []float64 { return results } -func RunLab8(){ - filename := "input.txt" +func RunLab8() { + const filename = "input.txt" values, err := readFile(filename) if err != nil { @@ -67,6 +70,7 @@ func RunLab8(){ fmt.Printf("x: %.2f, y: %.4f\n", x, y) } } + func readFile(filename string) ([]float64, error) { file, err := os.Open(filename) if err != nil { From 92cb0f70e1e52738ec05bce586d6a89d72f739e4 Mon Sep 17 00:00:00 2001 From: BaldinaDaria Date: Sat, 7 Dec 2024 19:50:40 +0300 Subject: [PATCH 06/15] Update Lab8.go --- golang/Lab8/Lab8.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/golang/Lab8/Lab8.go b/golang/Lab8/Lab8.go index 3323a369..653c72f8 100644 --- a/golang/Lab8/Lab8.go +++ b/golang/Lab8/Lab8.go @@ -9,12 +9,16 @@ import ( "strings" ) -func calculateY(b float64, x float64) float64 { - return (1 + math.Pow(math.Sin(math.Pow(b, 3)+math.Pow(x, 3)), 2)) / math.Cbrt(math.Pow(b, 3)+math.Pow(x, 3)) +func calculateY (b, x float64) float64 { + denominator := math.Cbrt(math.Pow(b, 3) + math.Pow(x, 3)) + if denominator == 0 { + return math.Inf(1) + } + return (1 + math.Pow(math.Sin(math.Pow(b, 3)+math.Pow(x, 3)), 2)) / denominator } -func taskA(bA float64, xStart float64, xEnd float64, deltaX float64) []float64 { - results := []float64{} +func taskA(bA, xStart, xEnd, deltaX float64) []float64 { + var results []float64 for x := xStart; x <= xEnd; x += deltaX { y := calculateY(bA, x) results = append(results, y) @@ -23,7 +27,7 @@ func taskA(bA float64, xStart float64, xEnd float64, deltaX float64) []float64 { } func taskB(bB float64, xValues []float64) []float64 { - results := []float64{} + var results []float64 for _, x := range xValues { y := calculateY(bB, x) results = append(results, y) @@ -32,7 +36,7 @@ func taskB(bB float64, xValues []float64) []float64 { } func RunLab8() { - filename := "input.txt" + const filename = "input.txt" createAndWriteFile(filename) @@ -49,7 +53,6 @@ func RunLab8() { bA := values[0] bB := values[1] - xValues := values[2:] xStart := 1.28 @@ -96,7 +99,9 @@ func createAndWriteFile(filename string) { writer.WriteString(text + "\n") } - writer.Flush() + if err := writer.Flush(); err != nil { + fmt.Println("Ошибка при записи данных в файл:", err) + } fmt.Println("Данные успешно записаны в файл:", filename) } @@ -126,7 +131,7 @@ func readFile(filename string) ([]float64, error) { return values, nil } -func searchInFile(filename string, searchTerm string) { +func searchInFile(filename, searchTerm string) { file, err := os.Open(filename) if err != nil { fmt.Println("Ошибка при открытии файла:", err) From b9812025b900f0ce4348493a1a051fa2e715d2d2 Mon Sep 17 00:00:00 2001 From: BaldinaDaria Date: Sat, 7 Dec 2024 20:01:22 +0300 Subject: [PATCH 07/15] Update Lab4.go --- golang/Lab8/Lab4.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/golang/Lab8/Lab4.go b/golang/Lab8/Lab4.go index 7004fbef..80819be4 100644 --- a/golang/Lab8/Lab4.go +++ b/golang/Lab8/Lab4.go @@ -8,7 +8,7 @@ import ( "strconv" ) -func calculateY(b, x float64) float64 { +func calcY(b, x float64) float64 { denominator := math.Cbrt(math.Pow(b, 3) + math.Pow(x, 3)) if denominator == 0 { return math.Inf(1) @@ -19,7 +19,7 @@ func calculateY(b, x float64) float64 { func taskA(bA, xStart, xEnd, deltaX float64) []float64 { var results []float64 for x := xStart; x <= xEnd; x += deltaX { - y := calculateY(bA, x) + y := calcY(bA, x) results = append(results, y) } return results @@ -28,7 +28,7 @@ func taskA(bA, xStart, xEnd, deltaX float64) []float64 { func taskB(bB float64, xValues []float64) []float64 { var results []float64 for _, x := range xValues { - y := calculateY(bB, x) + y := calcY(bB, x) results = append(results, y) } return results @@ -37,7 +37,7 @@ func taskB(bB float64, xValues []float64) []float64 { func RunLab8() { const filename = "input.txt" - values, err := readFile(filename) + values, err := readInputFile(filename) if err != nil { fmt.Println("Ошибка при чтении файла:", err) return @@ -71,7 +71,7 @@ func RunLab8() { } } -func readFile(filename string) ([]float64, error) { +func readInputFile(filename string) ([]float64, error) { file, err := os.Open(filename) if err != nil { return nil, err From 8cf9d1ca29de0decfe73b1c90c25eebb943ec98a Mon Sep 17 00:00:00 2001 From: BaldinaDaria Date: Sat, 7 Dec 2024 20:03:15 +0300 Subject: [PATCH 08/15] Update Lab8.go --- golang/Lab8/Lab8.go | 72 ++++----------------------------------------- 1 file changed, 5 insertions(+), 67 deletions(-) diff --git a/golang/Lab8/Lab8.go b/golang/Lab8/Lab8.go index 653c72f8..3b0acb8b 100644 --- a/golang/Lab8/Lab8.go +++ b/golang/Lab8/Lab8.go @@ -6,10 +6,9 @@ import ( "math" "os" "strconv" - "strings" ) -func calculateY (b, x float64) float64 { +func calcY(b, x float64) float64 { denominator := math.Cbrt(math.Pow(b, 3) + math.Pow(x, 3)) if denominator == 0 { return math.Inf(1) @@ -20,7 +19,7 @@ func calculateY (b, x float64) float64 { func taskA(bA, xStart, xEnd, deltaX float64) []float64 { var results []float64 for x := xStart; x <= xEnd; x += deltaX { - y := calculateY(bA, x) + y := calcY(bA, x) results = append(results, y) } return results @@ -29,7 +28,7 @@ func taskA(bA, xStart, xEnd, deltaX float64) []float64 { func taskB(bB float64, xValues []float64) []float64 { var results []float64 for _, x := range xValues { - y := calculateY(bB, x) + y := calcY(bB, x) results = append(results, y) } return results @@ -38,9 +37,7 @@ func taskB(bB float64, xValues []float64) []float64 { func RunLab8() { const filename = "input.txt" - createAndWriteFile(filename) - - values, err := readFile(filename) + values, err := readInputFile(filename) if err != nil { fmt.Println("Ошибка при чтении файла:", err) return @@ -72,40 +69,9 @@ func RunLab8() { x := xValues[i] fmt.Printf("x: %.2f, y: %.4f\n", x, y) } - - var searchTerm string - fmt.Print("\nВведите текст для поиска в файле: ") - fmt.Scanln(&searchTerm) - searchInFile(filename, searchTerm) } -func createAndWriteFile(filename string) { - file, err := os.Create(filename) - if err != nil { - fmt.Println("Ошибка при создании файла:", err) - return - } - defer file.Close() - - writer := bufio.NewWriter(file) - fmt.Println("Введите значения для записи в файл (введите 'exit' для завершения):") - scanner := bufio.NewScanner(os.Stdin) - for { - scanner.Scan() - text := scanner.Text() - if text == "exit" { - break - } - writer.WriteString(text + "\n") - } - - if err := writer.Flush(); err != nil { - fmt.Println("Ошибка при записи данных в файл:", err) - } - fmt.Println("Данные успешно записаны в файл:", filename) -} - -func readFile(filename string) ([]float64, error) { +func readInputFile(filename string) ([]float64, error) { file, err := os.Open(filename) if err != nil { return nil, err @@ -124,36 +90,8 @@ func readFile(filename string) ([]float64, error) { } values = append(values, value) } - if err := scanner.Err(); err != nil { return nil, err } return values, nil } - -func searchInFile(filename, searchTerm string) { - file, err := os.Open(filename) - if err != nil { - fmt.Println("Ошибка при открытии файла:", err) - return - } - defer file.Close() - - scanner := bufio.NewScanner(file) - found := false - for scanner.Scan() { - line := scanner.Text() - if strings.Contains(line, searchTerm) { - fmt.Println("Найдено:", line) - found = true - } - } - - if !found { - fmt.Println("Текст не найден в файле.") - } - - if err := scanner.Err(); err != nil { - fmt.Println("Ошибка при чтении файла:", err) - } -} From 19114f1f6e7e567bc9e8bf549e6242a71c92f37c Mon Sep 17 00:00:00 2001 From: BaldinaDaria Date: Sat, 7 Dec 2024 20:27:46 +0300 Subject: [PATCH 09/15] Update Lab4.go --- golang/Lab8/Lab4.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/golang/Lab8/Lab4.go b/golang/Lab8/Lab4.go index 80819be4..3c0e6bc5 100644 --- a/golang/Lab8/Lab4.go +++ b/golang/Lab8/Lab4.go @@ -8,7 +8,7 @@ import ( "strconv" ) -func calcY(b, x float64) float64 { +func calcYA(b, x float64) float64 { denominator := math.Cbrt(math.Pow(b, 3) + math.Pow(x, 3)) if denominator == 0 { return math.Inf(1) @@ -16,25 +16,25 @@ func calcY(b, x float64) float64 { return (1 + math.Pow(math.Sin(math.Pow(b, 3)+math.Pow(x, 3)), 2)) / denominator } -func taskA(bA, xStart, xEnd, deltaX float64) []float64 { +func taskAA(bA, xStart, xEnd, deltaX float64) []float64 { var results []float64 for x := xStart; x <= xEnd; x += deltaX { - y := calcY(bA, x) + y := calcYA(bA, x) results = append(results, y) } return results } -func taskB(bB float64, xValues []float64) []float64 { +func taskBA(bB float64, xValues []float64) []float64 { var results []float64 for _, x := range xValues { - y := calcY(bB, x) + y := calcYA(bB, x) results = append(results, y) } return results } -func RunLab8() { +func RunLab8A() { const filename = "input.txt" values, err := readInputFile(filename) @@ -56,14 +56,14 @@ func RunLab8() { xEnd := 3.28 deltaX := 0.4 - resultsA := taskA(bA, xStart, xEnd, deltaX) + resultsA := taskAA(bA, xStart, xEnd, deltaX) fmt.Println("Результаты задания A:") for i, y := range resultsA { x := xStart + float64(i)*deltaX fmt.Printf("x: %.2f, y: %.4f\n", x, y) } - resultsB := taskB(bB, xValues) + resultsB := taskBA(bB, xValues) fmt.Println("\nРезультаты задания B:") for i, y := range resultsB { x := xValues[i] From db4730567d2a9209d547fbf3eba2ac7e43485d30 Mon Sep 17 00:00:00 2001 From: BaldinaDaria Date: Sat, 7 Dec 2024 20:29:44 +0300 Subject: [PATCH 10/15] Update Lab4.go --- golang/Lab8/Lab4.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/golang/Lab8/Lab4.go b/golang/Lab8/Lab4.go index 3c0e6bc5..ed48cf91 100644 --- a/golang/Lab8/Lab4.go +++ b/golang/Lab8/Lab4.go @@ -71,7 +71,7 @@ func RunLab8A() { } } -func readInputFile(filename string) ([]float64, error) { +func readFile(filename string) ([]float64, error) { file, err := os.Open(filename) if err != nil { return nil, err From f6cd8c959fa4c904800cab065effd6083f28b8e3 Mon Sep 17 00:00:00 2001 From: BaldinaDaria Date: Sat, 7 Dec 2024 20:31:27 +0300 Subject: [PATCH 11/15] Update Lab4.go --- golang/Lab8/Lab4.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/golang/Lab8/Lab4.go b/golang/Lab8/Lab4.go index ed48cf91..3c0e6bc5 100644 --- a/golang/Lab8/Lab4.go +++ b/golang/Lab8/Lab4.go @@ -71,7 +71,7 @@ func RunLab8A() { } } -func readFile(filename string) ([]float64, error) { +func readInputFile(filename string) ([]float64, error) { file, err := os.Open(filename) if err != nil { return nil, err From 1329e0f56d4f16fcab0aa47f24d36ffebdcb3c2b Mon Sep 17 00:00:00 2001 From: BaldinaDaria Date: Sat, 7 Dec 2024 20:33:22 +0300 Subject: [PATCH 12/15] Update Lab4.go --- golang/Lab8/Lab4.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/golang/Lab8/Lab4.go b/golang/Lab8/Lab4.go index 3c0e6bc5..30903bf4 100644 --- a/golang/Lab8/Lab4.go +++ b/golang/Lab8/Lab4.go @@ -71,7 +71,7 @@ func RunLab8A() { } } -func readInputFile(filename string) ([]float64, error) { +func readInputFileA(filename string) ([]float64, error) { file, err := os.Open(filename) if err != nil { return nil, err From f531b8132ef3b7b2a00300e880a554f9eaed58de Mon Sep 17 00:00:00 2001 From: BaldinaDaria Date: Sat, 7 Dec 2024 20:37:04 +0300 Subject: [PATCH 13/15] Update Lab4.go --- golang/Lab8/Lab4.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/golang/Lab8/Lab4.go b/golang/Lab8/Lab4.go index 30903bf4..d5025c41 100644 --- a/golang/Lab8/Lab4.go +++ b/golang/Lab8/Lab4.go @@ -37,7 +37,7 @@ func taskBA(bB float64, xValues []float64) []float64 { func RunLab8A() { const filename = "input.txt" - values, err := readInputFile(filename) + values, err := readInputFileA(filename) if err != nil { fmt.Println("Ошибка при чтении файла:", err) return From 75672ac0c44a74eb836b8764e6f814f372ecb328 Mon Sep 17 00:00:00 2001 From: BaldinaDaria Date: Wed, 18 Dec 2024 21:19:37 +0300 Subject: [PATCH 14/15] Update Lab8.go --- golang/Lab8/Lab8.go | 134 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 124 insertions(+), 10 deletions(-) diff --git a/golang/Lab8/Lab8.go b/golang/Lab8/Lab8.go index 3b0acb8b..445566d7 100644 --- a/golang/Lab8/Lab8.go +++ b/golang/Lab8/Lab8.go @@ -6,6 +6,7 @@ import ( "math" "os" "strconv" + "strings" ) func calcY(b, x float64) float64 { @@ -16,28 +17,29 @@ func calcY(b, x float64) float64 { return (1 + math.Pow(math.Sin(math.Pow(b, 3)+math.Pow(x, 3)), 2)) / denominator } -func taskA(bA, xStart, xEnd, deltaX float64) []float64 { +func taskA(a, xStart, xEnd, deltaX float64) []float64 { var results []float64 for x := xStart; x <= xEnd; x += deltaX { - y := calcY(bA, x) + y := calcY(a, x) results = append(results, y) } return results } -func taskB(bB float64, xValues []float64) []float64 { +func taskB(b float64, xValues []float64) []float64 { var results []float64 for _, x := range xValues { - y := calcY(bB, x) + y := calcY(b, x) results = append(results, y) } return results } func RunLab8() { - const filename = "input.txt" + const inputFilename = "input.txt" + const outputFilename = "output.txt" - values, err := readInputFile(filename) + values, err := readInputFile(inputFilename) if err != nil { fmt.Println("Ошибка при чтении файла:", err) return @@ -48,27 +50,55 @@ func RunLab8() { return } - bA := values[0] - bB := values[1] + a := values[0] + b := values[1] xValues := values[2:] xStart := 1.28 xEnd := 3.28 deltaX := 0.4 - resultsA := taskA(bA, xStart, xEnd, deltaX) + resultsA := taskA(a, xStart, xEnd, deltaX) fmt.Println("Результаты задания A:") for i, y := range resultsA { x := xStart + float64(i)*deltaX fmt.Printf("x: %.2f, y: %.4f\n", x, y) } - resultsB := taskB(bB, xValues) + resultsB := taskB(b, xValues) fmt.Println("\nРезультаты задания B:") for i, y := range resultsB { x := xValues[i] fmt.Printf("x: %.2f, y: %.4f\n", x, y) } + + err = writeOutputFile(outputFilename, resultsA, resultsB, xStart, deltaX, xValues) + if err != nil { + fmt.Println("Ошибка при записи в файл:", err) + return + } + + err = writeDataToFile(outputFilename) + if err != nil { + fmt.Println("Ошибка при записи данных в файл:", err) + return + } + + err = displayFileContents(outputFilename) + if err != nil { + fmt.Println("Ошибка при выводе данных из файла:", err) + return + } + + fmt.Print("Введите текст для поиска в файле: ") + scanner := bufio.NewScanner(os.Stdin) + scanner.Scan() + searchTerm := scanner.Text() + err = searchInFile(outputFilename, searchTerm) + if err != nil { + fmt.Println("Ошибка при поиске в файле:", err) + return + } } func readInputFile(filename string) ([]float64, error) { @@ -95,3 +125,87 @@ func readInputFile(filename string) ([]float64, error) { } return values, nil } + +func writeOutputFile(filename string, resultsA, resultsB []float64, xStart, deltaX float64, xValues []float64) error { + file, err := os.Create(filename) + if err != nil { + return err + } + defer file.Close() + + writer := bufio.NewWriter(file) + + writer.WriteString("Результаты задания A:\n") + for i, y := range resultsA { + x := xStart + float64(i)*deltaX + writer.WriteString(fmt.Sprintf("x: %.2f, y: %.4f\n", x, y)) + } + + writer.WriteString("\nРезультаты задания B:\n") + for i, y := range resultsB { + x := xValues[i] + writer.WriteString(fmt.Sprintf("x: %.2f, y: %.4f\n", x, y)) + } + + return writer.Flush() +} + +func writeDataToFile(filename string) error { + file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0644) + if err != nil { + return err + } + defer file.Close() + + writer := bufio.NewWriter(file) + fmt.Println("Введите данные для записи в файл (введите 'exit' для выхода):") + + scanner := bufio.NewScanner(os.Stdin) + for { + scanner.Scan() + line := scanner.Text() + if line == "exit" { + break + } + writer.WriteString(line + "\n") + } + return writer.Flush() +} + +func displayFileContents(filename string) error { + file, err := os.Open(filename) + if err != nil { + return err + } + defer file.Close() + + scanner := bufio.NewScanner(file) + fmt.Println("\nСодержимое файла:") + for scanner.Scan() { + fmt.Println(scanner.Text()) + } + return scanner.Err() +} + +func searchInFile(filename, searchTerm string) error { + file, err := os.Open(filename) + if err != nil { + return err + } + defer file.Close() + + scanner := bufio.NewScanner(file) + found := false + fmt.Printf("\nПоиск текста '%s' в файле:\n", searchTerm) + for scanner.Scan() { + line := scanner.Text() + if strings.Contains(line, searchTerm) { + fmt.Printf("Найдено: %s\n", line) + found = true + } + } + if !found { + fmt.Println("Текст не найден.") + } + return scanner.Err() +} From cdd90386f47a83ccf8ee6c9702de27f7e7d723ac Mon Sep 17 00:00:00 2001 From: BaldinaDaria Date: Wed, 18 Dec 2024 21:31:18 +0300 Subject: [PATCH 15/15] Update Lab8.go