-
Notifications
You must be signed in to change notification settings - Fork 176
/
conv.go
113 lines (96 loc) · 2.04 KB
/
conv.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// ex2.2 prints measurements given on the command line or stdin in various
// units.
package main
import (
"bufio"
"fmt"
"log"
"os"
"regexp"
"strconv"
"strings"
)
type Measurement interface {
String() string
}
type Distance struct {
meters float64
}
func FromFeet(f float64) Distance {
return Distance{f * 0.3048}
}
func FromMeters(m float64) Distance {
return Distance{m}
}
func (d Distance) String() string {
return fmt.Sprintf("%.3gm = %.3gft", d.meters, d.Feet())
}
func (d Distance) Meters() float64 {
return d.meters
}
func (d Distance) Feet() float64 {
return d.meters / 0.3048
}
type Temperature float64
func FromCelcius(c float64) Temperature {
return Temperature(c)
}
func FromFarenheit(f float64) Temperature {
return Temperature((f * 5 / 9) - 32)
}
func (t Temperature) String() string {
return fmt.Sprintf("%.3gC = %.3gF", t.Celcius(), t.Farenheit())
}
func (t Temperature) Celcius() float64 {
return float64(t)
}
func (t Temperature) Farenheit() float64 {
return float64((t * 9 / 5) + 32)
}
func newMeasurement(f float64, unit string) (Measurement, error) {
unit = strings.ToLower(unit)
switch unit {
case "m":
return FromMeters(f), nil
case "\"", "ft":
return FromFeet(f), nil
case "c":
return FromCelcius(f), nil
case "f":
return FromFarenheit(f), nil
default:
return Distance{}, fmt.Errorf("Unexpected unit %v", unit)
}
}
func printMeasurement(s string) {
re := regexp.MustCompile(`(-?\d+(?:\.\d+)?)([A-Za-z]+)`)
match := re.FindStringSubmatch(s)
if match == nil {
log.Fatalf("Expecting <number><unit>, got %q", s)
}
f, err := strconv.ParseFloat(match[1], 64)
if err != nil {
log.Fatalf("%v isn't a number.", match[1])
}
if match[2] == "" {
log.Fatalf("No unit specified.")
}
unit := match[2]
m, err := newMeasurement(f, unit)
if err != nil {
log.Fatalf(err.Error())
}
fmt.Println(m)
}
func main() {
if len(os.Args) > 1 {
for _, arg := range os.Args[1:] {
printMeasurement(arg)
}
} else {
scan := bufio.NewScanner(os.Stdin)
for scan.Scan() {
printMeasurement(scan.Text())
}
}
}