Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
pub:labprog:20181112 [2018/11/12 14:30] lucafavalli |
pub:labprog:20181112 [2018/11/13 18:09] (current) andrea.milani |
||
---|---|---|---|
Line 71: | Line 71: | ||
//log.Printf("Command finished with error: %v", err) | //log.Printf("Command finished with error: %v", err) | ||
} | } | ||
+ | |||
+ | /*+++++++++++++++++ Programma clessidra +++++++++++++++++*/ | ||
+ | |||
+ | package main | ||
+ | |||
+ | import ( | ||
+ | "fmt" | ||
+ | "os" | ||
+ | "os/exec" | ||
+ | "strings" | ||
+ | "time" | ||
+ | ) | ||
+ | |||
+ | func main() { | ||
+ | |||
+ | var height, timer int | ||
+ | var char string | ||
+ | |||
+ | clean() | ||
+ | |||
+ | fmt.Print("Inserire i livelli della clessidra (metà clessidra): ") | ||
+ | fmt.Scan(&height) | ||
+ | fmt.Print("Inserire il carattere da usare per la clessidra: ") | ||
+ | fmt.Scan(&char) | ||
+ | fmt.Print("Inserire la durata della clessidra: ") | ||
+ | fmt.Scan(&timer) | ||
+ | |||
+ | rChar := []rune(char) | ||
+ | |||
+ | if draw(height, rChar[0], timer) { | ||
+ | fmt.Println("Tempo scaduto!") | ||
+ | } else { | ||
+ | fmt.Println("Il numero di livelli deve essere maggiore di 1") | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | // Funzione che disegna una singola riga. Accetta lunghezza, pienezza, carattere, shift | ||
+ | func row(lenght int, full bool, char rune, shift int) { | ||
+ | |||
+ | str := string(char) | ||
+ | |||
+ | switch full { | ||
+ | |||
+ | case true: | ||
+ | str := strings.Repeat(" ", shift) + strings.Repeat(str, lenght-2*shift) + strings.Repeat(" ", shift) | ||
+ | fmt.Println(str) | ||
+ | |||
+ | case false: | ||
+ | str := strings.Repeat(" ", shift) + str + strings.Repeat(" ", lenght-2*shift-2) + str + strings.Repeat(" ", shift) | ||
+ | fmt.Println(str) | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | // Funzione disegna clessidra | ||
+ | func draw(height int, char rune, timer int) bool { | ||
+ | |||
+ | clean() | ||
+ | |||
+ | livelliT := height | ||
+ | livelli := livelliT | ||
+ | |||
+ | if height > 1 { | ||
+ | |||
+ | l := height*2 + 1 | ||
+ | |||
+ | for livelli >= 0 { | ||
+ | |||
+ | for i := 0; i < livelliT-livelli; i++ { | ||
+ | row(l, false, char, i) | ||
+ | } | ||
+ | for i := livelliT - livelli; i < livelliT; i++ { | ||
+ | row(l, true, char, i) | ||
+ | } | ||
+ | for i := livelliT - 1; i >= livelliT-livelli; i-- { | ||
+ | row(l, false, char, i) | ||
+ | } | ||
+ | for i := livelliT - livelli - 1; i >= 0; i-- { | ||
+ | row(l, true, char, i) | ||
+ | } | ||
+ | |||
+ | if livelli != 0 { | ||
+ | time.Sleep(time.Duration((timer * 1000 / height)) * time.Millisecond) | ||
+ | clean() | ||
+ | } | ||
+ | livelli-- | ||
+ | } | ||
+ | } else { | ||
+ | return false | ||
+ | } | ||
+ | |||
+ | clean() | ||
+ | return true | ||
+ | } | ||
+ | |||
+ | // Funzione cancella schermo | ||
+ | func clean() { | ||
+ | |||
+ | cmd := exec.Command("clear") | ||
+ | cmd.Stdout = os.Stdout | ||
+ | cmd.Run() | ||
+ | } | ||
+ | |||
</code> | </code> |