commit 534d9fa5838e51b7efd76ba0ee6630ad45d6cf47 Author: Dmitry Sirotkin Date: Tue Feb 11 16:12:21 2025 +0300 init diff --git a/hw/calc/.vscode/launch.json b/hw/calc/.vscode/launch.json new file mode 100644 index 0000000..608d3c6 --- /dev/null +++ b/hw/calc/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch Package", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "${fileDirname}" + } + ] +} \ No newline at end of file diff --git a/hw/calc/cmd/calc/calc.go b/hw/calc/cmd/calc/calc.go new file mode 100644 index 0000000..f4ea45e --- /dev/null +++ b/hw/calc/cmd/calc/calc.go @@ -0,0 +1,61 @@ +package main + +import ( + "fmt" + "os" + "strconv" + c "calc/pkg" +) + +func main() { + + var x string + fmt.Println("Введите первое число:") + fmt.Scan(&x) + + numx, errx := Validatenumber(x) + fmt.Println(numx) + + if errx != nil { + fmt.Println("Некорректное число. Пожалуйста, введите числовое значение.") + os.Exit(1) + } + + var o string + fmt.Println("Выберите операцию (+, -, *, /)") + fmt.Scan(&o) + + if !Validateoperation(o) { + fmt.Println("Некорректная операция. Пожалуйста, используйте символы +, -, * или /.") + os.Exit(1) + } + + var y string + fmt.Println("Введите второе число") + fmt.Scan(&y) + + numy, erry := Validatenumber(y) + fmt.Println(numy) + + if erry != nil { + fmt.Println("Некорректное число. Пожалуйста, введите числовое значение.") + os.Exit(1) + } + + + fmt.Println(c.Calc(numx, numy, o)) +} + +func Validateoperation(s string) bool { + opertions := [...]string{"+", "-", "*", "/"} + for _, o := range opertions { + if o == s { + return true + } + } + return false +} + +func Validatenumber(s string) (float64, error){ + return strconv.ParseFloat(s, 64) +} diff --git a/hw/calc/go.mod b/hw/calc/go.mod new file mode 100644 index 0000000..e1bf432 --- /dev/null +++ b/hw/calc/go.mod @@ -0,0 +1,3 @@ +module calc + +go 1.23.6 diff --git a/hw/calc/pkg/calc.go b/hw/calc/pkg/calc.go new file mode 100644 index 0000000..cb1334f --- /dev/null +++ b/hw/calc/pkg/calc.go @@ -0,0 +1,35 @@ +package calc + +import "fmt" + + +func Calc(a, b float64, o string) (float64, error) { + + switch o { + case "+": + return sum(a, b), nil + case "-": + return sub(a, b), nil + case "*": + return mul(a, b), nil + case "/": + return div(a, b), nil + } + return 0, fmt.Errorf("Uknown operation") +} + +func sum(a, b float64) float64 { + return a + b +} + +func sub(a, b float64) float64 { + return a - b +} + +func mul(a, b float64) float64 { + return a * b +} + +func div(a, b float64) float64 { + return a / b +} diff --git a/hw/calc/pkg/calc_test.go b/hw/calc/pkg/calc_test.go new file mode 100644 index 0000000..2c8beab --- /dev/null +++ b/hw/calc/pkg/calc_test.go @@ -0,0 +1,44 @@ +package calc + +import ( + "testing" +) + +func TestCalcUknownOperation(t *testing.T) { + _, err := Calc(1,1,"a") + + if err == nil { + t.Fatalf("Should be error") + } +} + +/* func Calc(a, b float64, o string) (float64, error) { + + switch o { + case "+": + return sum(a, b), nil + case "-": + return sub(a, b), nil + case "*": + return mul(a, b), nil + case "/": + return div(a, b), nil + } + return 0, fmt.Errorf("Uknown operation") +} + +func sum(a, b float64) float64 { + return a + b +} + +func sub(a, b float64) float64 { + return a - b +} + +func mul(a, b float64) float64 { + return a * b +} + +func div(a, b float64) float64 { + return a / b +} */ \ No newline at end of file diff --git a/hw/calc/pkg/go.go b/hw/calc/pkg/go.go new file mode 100644 index 0000000..58d0233 --- /dev/null +++ b/hw/calc/pkg/go.go @@ -0,0 +1,34 @@ +package calc + +import "fmt" + + +func Calcc(a, b float64, o string) (float64, error) { + switch o { + case "+": + return sum(a, b), nil + case "-": + return sub(a, b), nil + case "*": + return mul(a, b), nil + case "/": + return div(a, b), nil + } + return 0, fmt.Errorf("Uknown operation") +} + +func summ(a, b float64) float64 { + return a + b +} + +func subb(a, b float64) float64 { + return a - b +} + +func mull(a, b float64) float64 { + return a * b +} + +func divv(a, b float64) float64 { + return a / b +} \ No newline at end of file diff --git a/test/cmd/main.go b/test/cmd/main.go new file mode 100644 index 0000000..6b4f326 --- /dev/null +++ b/test/cmd/main.go @@ -0,0 +1,18 @@ +package main + +import ( + "fmt" + "time" + c "test/pkg/test" + v "test/pkg" +) + +func add(a int, b int) { + time.Sleep(1 * time.Second) + fmt.Println(a + b) +} + +func main() { + c.SimpleFor(4) + v.SimpleVar() +} diff --git a/test/go.mod b/test/go.mod new file mode 100644 index 0000000..e047a36 --- /dev/null +++ b/test/go.mod @@ -0,0 +1,3 @@ +module test + +go 1.23.6 diff --git a/test/pkg/test/cycle.go b/test/pkg/test/cycle.go new file mode 100644 index 0000000..9950f88 --- /dev/null +++ b/test/pkg/test/cycle.go @@ -0,0 +1,10 @@ +package cycle + +import "fmt" + +func SimpleFor(count int) { + + for i := 0; i < count; i++ { + fmt.Println(i) + } +} diff --git a/test/pkg/variable.go b/test/pkg/variable.go new file mode 100644 index 0000000..3ef1a28 --- /dev/null +++ b/test/pkg/variable.go @@ -0,0 +1,34 @@ +package variable + +import ( + "fmt" +) + +func SimpleVar() { + var a string = "initial" + fmt.Println(a) + + var b, c int = 1, 2 + fmt.Println(b, c) + + var d = true + fmt.Println(d) + + f:= "short" + fmt.Println(f) + + var ar [4]int + for i :=0; i < len(ar); i++ { + fmt.Println(i) + } + + //динамический массив + slice := make([]int, 3) + fmt.Println(len(slice)) + fmt.Println(slice) + fmt.Println(len(slice)) + slice = append(slice, 4) + fmt.Println(len(slice)) + fmt.Println(slice) + +}