Homework
This commit is contained in:
parent
8a73b135c0
commit
5d6a88c960
@ -1,23 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
time "time"
|
|
||||||
|
|
||||||
cycle "train_golang/pkg"
|
|
||||||
)
|
|
||||||
|
|
||||||
func add(a, b int) {
|
|
||||||
fmt.Println("Sum = ", a+b)
|
|
||||||
}
|
|
||||||
|
|
||||||
func sub(a, b int) {
|
|
||||||
fmt.Println("Sub = ", a-b)
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
go add(5, 6)
|
|
||||||
go sub(7, 9)
|
|
||||||
cycle.SimpleForCycle()
|
|
||||||
time.Sleep(1 * time.Second)
|
|
||||||
}
|
|
49
go/homework_one/cmd/main.go
Normal file
49
go/homework_one/cmd/main.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"homework_one/internal/input"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
for {
|
||||||
|
|
||||||
|
number_one, err := input.GetNumber("Введите первое число:")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Некорректное число. Пожалуйста, введите числовое значение.")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
operator := input.GetOperator()
|
||||||
|
|
||||||
|
number_two, err := input.GetNumber("Введите второе число:")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Некорректное число. Пожалуйста, введите числовое значение.")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var result float64
|
||||||
|
switch operator {
|
||||||
|
case "+":
|
||||||
|
result = number_one + number_two
|
||||||
|
case "-":
|
||||||
|
result = number_one - number_two
|
||||||
|
case "*":
|
||||||
|
result = number_one * number_two
|
||||||
|
case "/":
|
||||||
|
if number_two == 0 {
|
||||||
|
fmt.Println("Ошибка: деление на ноль невозможно")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
result = number_one / number_two
|
||||||
|
default:
|
||||||
|
fmt.Println("Неизвестный оператор")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
fmt.Printf("Результат: %f %s %f = %f\n", number_one, operator, number_two, result)
|
||||||
|
|
||||||
|
fmt.Println("Press the Enter Key to stop anytime")
|
||||||
|
fmt.Scanln()
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
10
go/homework_one/go.mod
Normal file
10
go/homework_one/go.mod
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
module homework_one
|
||||||
|
|
||||||
|
go 1.23.6
|
||||||
|
|
||||||
|
require github.com/manifoldco/promptui v0.9.0
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
|
||||||
|
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b // indirect
|
||||||
|
)
|
10
go/homework_one/go.sum
Normal file
10
go/homework_one/go.sum
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE=
|
||||||
|
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
|
||||||
|
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
|
||||||
|
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
|
||||||
|
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
|
||||||
|
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
|
||||||
|
github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA=
|
||||||
|
github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg=
|
||||||
|
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b h1:MQE+LT/ABUuuvEZ+YQAMSXindAdUh7slEmAkup74op4=
|
||||||
|
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
BIN
go/homework_one/home
Executable file
BIN
go/homework_one/home
Executable file
Binary file not shown.
52
go/homework_one/internal/input/input.go
Normal file
52
go/homework_one/internal/input/input.go
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
package input
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
p "github.com/manifoldco/promptui"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetNumber получение числового ввода пользователя
|
||||||
|
func GetNumber(message string) (float64, error) {
|
||||||
|
validate := func(input string) error {
|
||||||
|
_, err := strconv.ParseFloat(input, 64)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("Invalid number")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
prompt := p.Prompt{
|
||||||
|
Label: message,
|
||||||
|
Validate: validate,
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := prompt.Run()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Prompt failed %v\n", err)
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
number, _ := strconv.ParseFloat(result, 64)
|
||||||
|
return number, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetOperator получение оператора
|
||||||
|
func GetOperator() string {
|
||||||
|
prompt := p.Select{
|
||||||
|
Label: "Выберите операцию (+, -, *, /):",
|
||||||
|
Items: []string{"+", "-", "*", "/"},
|
||||||
|
}
|
||||||
|
|
||||||
|
_, result, err := prompt.Run()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Prompt failed %v\n", err)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
@ -1,11 +0,0 @@
|
|||||||
package cycle
|
|
||||||
|
|
||||||
import (
|
|
||||||
fmt "fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
func SimpleForCycle() {
|
|
||||||
for i := 0; i < 100; i++ {
|
|
||||||
fmt.Println("Iter: ", i)
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user