Skip to content

Commit

Permalink
lab4,5 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
makhotinna committed Jan 18, 2024
1 parent 6ef648a commit 069f133
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
4 changes: 2 additions & 2 deletions golang/internal/lab4/lab4.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

func Task1(a, b, xn, xk, deltax float64) []float64 {
var answers1 []float64 = make([]float64, 0)
var answers1 []float64 = make([]float64, 0, int((xk - xn)/deltax) + 1)
for x := xn; x <= xk; x += deltax {
y := (a + math.Pow(math.Tan(b*x), 2)) / (b + math.Pow(math.Atan(a*x), 2))
answers1 = append(answers1, y)
Expand All @@ -14,7 +14,7 @@ func Task1(a, b, xn, xk, deltax float64) []float64 {
}

func Task2(a float64, b float64, znX []float64) []float64 {
var answers2 []float64 = make([]float64, 0)
var answers2 []float64 = make([]float64, 0, len(znX))
for _, x := range znX {
y := (a + math.Pow(math.Tan(b*x), 2)) / (b + math.Pow(math.Atan(a*x), 2))
answers2 = append(answers2, y)
Expand Down
13 changes: 10 additions & 3 deletions golang/internal/lab5/lab5.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lab5

import (
"fmt"
"errors"
)

type city struct {
Expand All @@ -13,11 +14,17 @@ func (c city) GetPopulation() int {
return c.population
}

func NewCity(setPopulation int, setCountry string) city {
return city{
func NewCity(setPopulation int, setCountry string) (*city, error) {
c := &city{
population: setPopulation,
country: setCountry,
}

if err := c.SetPopulation(setPopulation); err != nil {
return nil, err
}

return c, nil
}

func (c *city) SetCountry(country string) {
Expand All @@ -29,7 +36,7 @@ func (c *city) SetPopulation(population int) error {
c.population = population
return nil
}
return fmt.Errorf("invalid city population")
return errors.New("invalid city population")
}

func (c city) GetCountry() string {
Expand Down
24 changes: 8 additions & 16 deletions golang/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@ package main

import (
"fmt"
"log"

"isuct.ru/informatics2022/internal/lab4"
"isuct.ru/informatics2022/internal/lab5"
)

func checkForError(err error) {
if err != nil {
log.Fatal(err)
}
}

func main() {
fmt.Println("Махотина Валерия Олеговна")
fmt.Println("---")
Expand All @@ -24,14 +17,13 @@ func main() {
a2 := lab4.Task2(0.1, 0.5, []float64{0.2, 0.3, 0.44, 0.6, 0.56})
fmt.Println(a2)

var city = lab5.NewCity(400, "Russia")
city, err := lab5.NewCity(400, "Russia")

var err error = city.SetPopulation(400)
checkForError(err)

city.SetCountry("Russia")

fmt.Printf("City's population is %d\n", city.GetPopulation())
fmt.Printf("This city is in %s\n", city.GetCountry())
city.GetTheCity()
if err == nil{
fmt.Printf("City's population is %d\n", city.GetPopulation())
fmt.Printf("This city is in %s\n", city.GetCountry())
city.GetTheCity()
} else {
fmt.Println(err)
}
}

0 comments on commit 069f133

Please sign in to comment.