golang-exercises/golangr/array/main.go

104 lines
1.8 KiB
Go
Raw Normal View History

2024-01-26 18:53:59 +01:00
package main
import (
"fmt"
"log"
"os"
//"time"
2024-01-26 18:53:59 +01:00
)
var notas []float64
var num float64
var choice int
2024-01-26 18:53:59 +01:00
func main() {
fmt.Print("\033[H\033[2J")
msg()
options()
}
func options() {
for {
if _, err := fmt.Scanln(&choice); err != nil {
log.Print("Something failed")
}
if choice == 0 {
return
}
switch choice {
case 1:
addVec()
case 2:
showResults()
case 3:
fmt.Println("Have a nice day!")
os.Exit(0)
default:
fmt.Print("\033[H\033[2J")
fmt.Printf("[-] ERROR: invalid input\n\n")
fmt.Printf("[1] Add to vector\n")
fmt.Printf("[2] View results\n")
fmt.Printf("[3] Exit\n")
fmt.Printf("\nPlease, select your choice: ")
}
}
}
// There is no saving this codebase, it has become so convoluted I can barely comprehend anything anymore
func msg() {
fmt.Print("\033[H\033[2J")
fmt.Printf("[1] Add to vector\n")
fmt.Printf("[2] View results\n")
fmt.Printf("[3] Exit\n")
fmt.Printf("\nPlease, select your choice: ")
}
func addVec() {
fmt.Print("\033[H\033[2J")
fmt.Printf("Which number would you like to add to the vector?\nNumber: ")
fmt.Scanln(&num)
if num >= 0.001 && num <= 10 {
} else {
msg()
fmt.Printf("\nERROR: Invalid number\nChoice: ")
return
}
notas = append(notas, num)
//time.Sleep(2 * time.Second)
//time.Sleep(2 * time.Second)
msg()
fmt.Printf("\nYou have added the number %v!\nChoice: ", num)
num = 0
}
func showResults() {
//time.Sleep(2 * time.Second)
msg()
min := notas[0]
max := notas[0]
for _, v := range notas {
if v < min {
min = v
}
}
for _, v := range notas {
if v > max {
max = v
}
}
fmt.Printf("\nThese are your current grades: \n")
fmt.Printf("%.2f\n", notas)
fmt.Printf("Lowest grade: %v\n", min)
fmt.Printf("Highest grade: %v\n", max)
fmt.Printf("Choice: ")
2024-01-26 18:53:59 +01:00
}