This commit is contained in:
Dmitry Sirotkin 2025-02-11 16:12:21 +03:00
commit c5893a616e
11 changed files with 258 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.vscode

15
hw/calc/.vscode/launch.json vendored Normal file
View File

@ -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}"
}
]
}

61
hw/calc/cmd/calc/calc.go Normal file
View File

@ -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)
}

3
hw/calc/go.mod Normal file
View File

@ -0,0 +1,3 @@
module calc
go 1.23.6

35
hw/calc/pkg/calc.go Normal file
View File

@ -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
}

44
hw/calc/pkg/calc_test.go Normal file
View File

@ -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
} */

34
hw/calc/pkg/go.go Normal file
View File

@ -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
}

18
test/cmd/main.go Normal file
View File

@ -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()
}

3
test/go.mod Normal file
View File

@ -0,0 +1,3 @@
module test
go 1.23.6

10
test/pkg/test/cycle.go Normal file
View File

@ -0,0 +1,10 @@
package cycle
import "fmt"
func SimpleFor(count int) {
for i := 0; i < count; i++ {
fmt.Println(i)
}
}

34
test/pkg/variable.go Normal file
View File

@ -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)
}