Skip to content

Commit

Permalink
Update Lab8.go
Browse files Browse the repository at this point in the history
  • Loading branch information
BaldinaDaria authored Dec 18, 2024
1 parent f531b81 commit 75672ac
Showing 1 changed file with 124 additions and 10 deletions.
134 changes: 124 additions & 10 deletions golang/Lab8/Lab8.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math"
"os"
"strconv"
"strings"
)

func calcY(b, x float64) float64 {
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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()
}

0 comments on commit 75672ac

Please sign in to comment.