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

Baldina Lab8 #456

Open
wants to merge 15 commits into
base: Baldina_Daria
Choose a base branch
from
98 changes: 98 additions & 0 deletions golang/Lab8/Lab4.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package Lab8

import (
"bufio"
"fmt"
"math"
"os"
"strconv"
)

func calcYA(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 taskAA(bA, xStart, xEnd, deltaX float64) []float64 {
var results []float64
for x := xStart; x <= xEnd; x += deltaX {
y := calcYA(bA, x)
results = append(results, y)
}
return results
}

func taskBA(bB float64, xValues []float64) []float64 {
var results []float64
for _, x := range xValues {
y := calcYA(bB, x)
results = append(results, y)
}
return results
}

func RunLab8A() {
const filename = "input.txt"

values, err := readInputFileA(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 := 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 := taskBA(bB, xValues)
fmt.Println("\nРезультаты задания B:")
for i, y := range resultsB {
x := xValues[i]
fmt.Printf("x: %.2f, y: %.4f\n", x, y)
}
}

func readInputFileA(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
}
211 changes: 211 additions & 0 deletions golang/Lab8/Lab8.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
package Lab8

import (
"bufio"
"fmt"
"math"
"os"
"strconv"
"strings"
)

func calcY(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(a, xStart, xEnd, deltaX float64) []float64 {
var results []float64
for x := xStart; x <= xEnd; x += deltaX {
y := calcY(a, x)
results = append(results, y)
}
return results
}

func taskB(b float64, xValues []float64) []float64 {
var results []float64
for _, x := range xValues {
y := calcY(b, x)
results = append(results, y)
}
return results
}

func RunLab8() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не увидел записи и поиска

const inputFilename = "input.txt"
const outputFilename = "output.txt"

values, err := readInputFile(inputFilename)
if err != nil {
fmt.Println("Ошибка при чтении файла:", err)
return
}

if len(values) < 2 {
fmt.Println("Недостаточно значений в файле")
return
}

a := values[0]
b := values[1]
xValues := values[2:]

xStart := 1.28
xEnd := 3.28
deltaX := 0.4

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(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) {
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 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()
}
7 changes: 7 additions & 0 deletions golang/Lab8/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
2.5
3.0
1.1
2.4
3.6
1.7
3.9
Loading