golang-exercises/golangr/range/main.go

142 lines
2.8 KiB
Go
Raw Normal View History

2024-01-30 06:47:29 +01:00
package main
import (
"fmt"
"git.bulgariu.xyz/raul/min-max-array"
"os"
//"log"
2024-01-30 06:47:29 +01:00
)
// https://golangr.com/range
var notas []float64
var choice int8
var numToAdd float64
var notaSumada float64
2024-01-30 06:47:29 +01:00
2024-01-30 10:35:22 +01:00
func clear() {
fmt.Print("\033[H\033[2J")
}
2024-01-30 06:47:29 +01:00
func main() {
start()
}
func start() {
2024-01-30 10:35:22 +01:00
clear()
msg()
choices()
2024-01-30 06:47:29 +01:00
}
func msg() {
println("[1] Add grade to vector")
println("[2] View grades")
println("[3] Get grade average")
2024-01-30 10:35:22 +01:00
println("[4] Get min / max grade")
println("[5] Reset grades")
println("[6] Exit")
2024-01-30 06:47:29 +01:00
}
func choices() {
for {
fmt.Printf("Choice: ")
_, err := fmt.Scan(&choice)
if err != nil {
fmt.Println(err)
}
switch choice {
case 1:
addVec()
case 2:
viewGrades()
case 3:
average()
case 4:
getMinMax()
case 5:
resetGrades()
case 6:
fmt.Println("Have a nice day!")
os.Exit(0)
default:
clear()
msg()
fmt.Printf("\n-----------------------------------------------\n")
fmt.Printf("ERROR: Invalid input\n")
fmt.Printf("-----------------------------------------------\n\n")
}
}
}
2024-01-30 06:47:29 +01:00
func average() {
2024-01-30 10:35:22 +01:00
clear()
msg()
for _, elem := range notas {
notaSumada = notaSumada + elem
}
notaMedia := notaSumada / float64(len(notas))
2024-01-30 10:35:22 +01:00
fmt.Printf("\n-----------------------------------------------")
fmt.Printf("\nAll your grades combine to %.2v\n", notaSumada)
fmt.Printf("Your average grade is %.2v\n", notaMedia)
2024-01-30 10:35:22 +01:00
fmt.Printf("-----------------------------------------------\n\n")
2024-01-30 06:47:29 +01:00
2024-01-30 10:35:22 +01:00
notaSumada = 0
notaMedia = 0
2024-01-30 06:47:29 +01:00
}
func getMinMax() {
clear()
msg()
// Me finally getting to make use of my first library :)
minNota := minmax.Min(notas)
maxNota := minmax.Max(notas)
// minNota := Min(notas)
// maxNota := Max(notas)
fmt.Printf("\n-----------------------------------------------")
fmt.Printf("\nLargest grade: %v\n", maxNota)
fmt.Printf("Smallest grade: %v\n", minNota)
fmt.Printf("-----------------------------------------------\n\n")
2024-01-30 06:47:29 +01:00
}
func resetGrades() {
2024-01-30 10:35:22 +01:00
clear()
msg()
2024-01-30 10:35:22 +01:00
fmt.Printf("\n-----------------------------------------------")
fmt.Printf("\nYour grades have been reset!\n")
fmt.Printf("-----------------------------------------------\n\n")
notas = nil
}
func addVec() {
2024-01-30 10:35:22 +01:00
clear()
fmt.Println("Add a number to your vector")
fmt.Printf("Number: ")
_, err := fmt.Scanln(&numToAdd)
if err != nil {
fmt.Println(err)
}
2024-01-30 10:35:22 +01:00
notas = append(notas, numToAdd)
2024-01-30 10:35:22 +01:00
clear()
msg()
2024-01-30 10:35:22 +01:00
fmt.Printf("\n-----------------------------------------------")
fmt.Printf("\nYou have added the number %v\n", numToAdd)
fmt.Printf("-----------------------------------------------\n\n")
numToAdd = 0
}
func viewGrades() {
2024-01-30 10:35:22 +01:00
clear()
msg()
2024-01-30 10:35:22 +01:00
fmt.Printf("\n-----------------------------------------------\n")
fmt.Printf("These are your grades:")
fmt.Printf("\n-----------------------------------------------\n")
for _, elem := range notas {
fmt.Println(elem)
}
2024-01-30 10:35:22 +01:00
fmt.Printf("-----------------------------------------------\n\n")
}