-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (62 loc) · 1.72 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"flag"
"fmt"
)
func main() {
var (
celsius float64
fahrenheit float64
kelvin float64
toScale string
)
flag.Float64Var(&celsius, "c", 0.0, "Temperatur i Celsius")
flag.Float64Var(&fahrenheit, "f", 0.0, "Temperatur i Fahrenheit")
flag.Float64Var(&kelvin, "k", 0.0, "Temperatur i Kelvin")
flag.StringVar(&toScale, "to", "", "Temperaturskala for reslutatet (c/f/k)")
flag.Parse()
if toScale == "" {
fmt.Println("Spesifiser en temperatur skala for å konverter til å bruke -to flag (c/f/k)")
return
}
if (celsius != 0 && (fahrenheit != 0 || kelvin != 0)) ||
(fahrenheit != 0 && (celsius != 0 || kelvin != 0)) ||
(kelvin != 0 && (celsius != 0 || fahrenheit != 0)) {
fmt.Println("En temperatur verdi for å konverter")
return
}
switch toScale {
case "c":
if celsius != 0 {
fmt.Printf("%.2f°C\n", celsius)
} else if fahrenheit != 0 {
celsius = (fahrenheit - 32) * 5 / 9
fmt.Printf("%.2f°F is %.2f°C\n", fahrenheit, celsius)
} else {
celsius = kelvin - 273.15
fmt.Printf("%.2fK is %.2f°C\n", kelvin, celsius)
}
case "f":
if fahrenheit != 0 {
fmt.Printf("%.2f°F\n", fahrenheit)
} else if celsius != 0 {
fahrenheit = celsius*9/5 + 32
fmt.Printf("%.2f°C is %.2f°F\n", celsius, fahrenheit)
} else {
fahrenheit = kelvin*9/5 - 459.67
fmt.Printf("%.2fK is %.2f°F\n", kelvin, fahrenheit)
}
case "k":
if kelvin != 0 {
fmt.Printf("%.2fK\n", kelvin)
} else if celsius != 0 {
kelvin = celsius + 273.15
fmt.Printf("%.2f°C is %.2fK\n", celsius, kelvin)
} else {
kelvin = (fahrenheit + 459.67) * 5 / 9
fmt.Printf("%.2f°F is %.2fK\n", fahrenheit, kelvin)
}
default:
fmt.Printf("Ukjent temperatur skala %s\n", toScale)
}
}