Init commit
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
go-demo/app-1
|
||||
3
go-demo-2/go.mod
Normal file
3
go-demo-2/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module demo/app-2
|
||||
|
||||
go 1.23.6
|
||||
44
go-demo-2/main.go
Normal file
44
go-demo-2/main.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// добавление слайса к слайсу - unpack
|
||||
// tr1 := []int{1, 2, 3}
|
||||
// tr2 := []int{4, 5, 6}
|
||||
// tr1 = append(tr1, tr2...)
|
||||
// fmt.Println(tr1)
|
||||
|
||||
// Проход в цикле по массиву
|
||||
// for index, value := range tr1 {
|
||||
// fmt.Printf("Index: %v, value: %v\n", index, value)
|
||||
// }
|
||||
|
||||
transactions := []float64{}
|
||||
|
||||
for {
|
||||
newTrans := scanTransaction()
|
||||
if newTrans == 0 {
|
||||
break
|
||||
}
|
||||
transactions = append(transactions, newTrans)
|
||||
}
|
||||
fmt.Printf("Ваш баланс равен: %.2f ₽", sumTransactions(transactions))
|
||||
}
|
||||
|
||||
func scanTransaction() float64 {
|
||||
var transaction float64
|
||||
fmt.Print("Введите транзакцию (n для выхода): ")
|
||||
fmt.Scan(&transaction)
|
||||
return transaction
|
||||
}
|
||||
|
||||
func sumTransactions(slc []float64) float64 {
|
||||
var balance float64
|
||||
for _, value := range slc {
|
||||
balance += value
|
||||
}
|
||||
return balance
|
||||
}
|
||||
3
go-demo/go.mod
Normal file
3
go-demo/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module demo/app-1
|
||||
|
||||
go 1.23.6
|
||||
69
go-demo/main.go
Normal file
69
go-demo/main.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("__ Калькулятор индекса массы тела __")
|
||||
for {
|
||||
userKg, userHeight := getUserInput()
|
||||
IMT, err := calculateIMT(userKg, userHeight)
|
||||
if err != nil {
|
||||
// fmt.Println(err)
|
||||
// continue
|
||||
panic("Не заданы параметры для расчета")
|
||||
}
|
||||
outputResult(IMT)
|
||||
|
||||
fmt.Print("Хотите расчитать еще (Y/n): ")
|
||||
var choice string
|
||||
fmt.Scan(&choice)
|
||||
if choice == "n" {
|
||||
break
|
||||
}
|
||||
if choice == "Y" || choice == "y" {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func outputResult(imt float64) {
|
||||
result := fmt.Sprintf("Ваш индекс массы тела: %.1f\n", imt)
|
||||
fmt.Print(result)
|
||||
|
||||
switch {
|
||||
case imt < 16:
|
||||
fmt.Println("У вас сильный дефицит массы тела")
|
||||
case imt < 18.5:
|
||||
fmt.Println("У вас дефицит массы тела")
|
||||
case imt < 25:
|
||||
fmt.Println("У вас нормальный вес")
|
||||
case imt < 30:
|
||||
fmt.Println("У вас избыточный вес")
|
||||
default:
|
||||
fmt.Println("У вас степень ожирения")
|
||||
}
|
||||
}
|
||||
|
||||
func calculateIMT(userKg float64, userHeight float64) (float64, error) {
|
||||
const IMTPower float64 = 2
|
||||
if userHeight <= 0 || userKg <= 0 {
|
||||
return 0, errors.New("NO_VALID_PARAMS")
|
||||
}
|
||||
IMT := userKg / math.Pow(userHeight/100, IMTPower)
|
||||
return IMT, nil
|
||||
}
|
||||
|
||||
func getUserInput() (float64, float64) {
|
||||
var userHeight float64
|
||||
var userKg float64
|
||||
fmt.Print("Введите свой рост в сантиметрах: ")
|
||||
fmt.Scan(&userHeight)
|
||||
fmt.Print("Введите свой вес в килограммах: ")
|
||||
fmt.Scan(&userKg)
|
||||
|
||||
return userKg, userHeight
|
||||
}
|
||||
Reference in New Issue
Block a user