diff --git a/main.go b/main.go index 8c9d8f4..2b36403 100644 --- a/main.go +++ b/main.go @@ -1,5 +1,82 @@ package main -func main(){ +import ( + "fmt" +) -} \ No newline at end of file +func main() { + var operatore1, operatore2 float32 + var exit bool + + for !exit { + fmt.Print("Inserire il primo numero: ") + _, err := fmt.Scanf("%f", &operatore1) + + if err != nil { + fmt.Println("\nValore non valido") + + } else { + + for !exit { + + fmt.Print("Inserire il secondo numero: ") + _, err := fmt.Scanf("%f", &operatore2) + + if err != nil { + fmt.Println("\nValore non valido") + } else { + exit = menu(operatore1, operatore2) + } + } + + } + } +} + +func menu(operatore1, operatore2 float32) bool{ + var scelta uint8 + var exit bool + + for !exit { + + fmt.Println("Inserire un'opzione") + fmt.Println("1. Addizione") + fmt.Println("2. Sottrazione") + fmt.Println("3. Moltiplicazione") + fmt.Println("4. Divisione") + fmt.Println("0. Esci") + fmt.Print("Scelta: ") + + _, err := fmt.Scanf("%d", &scelta) + + if err != nil { + fmt.Println("\nValore non valido") + + } else { + + switch scelta { + + case 0: + exit = true + case 1: + fmt.Printf("%f + %f = %f\n", operatore1, operatore2, operatore1+operatore2) + case 2: + fmt.Printf("%f - %f = %f\n", operatore1, operatore2, operatore1-operatore2) + case 3: + fmt.Printf("%f * %f = %f\n", operatore1, operatore2, operatore1*operatore2) + case 4: + if operatore2 == 0 { + fmt.Println("Impossibile dividere per zero") + } else { + fmt.Printf("%f / %f = %f\n", operatore1, operatore2, operatore1/operatore2) + } + default: + fmt.Println("Opzione non valida") + + } + + } + + } + return exit +}