Exercise 2-2
authorwatkinsr <ryanwatkins54@gmail.com>
Fri, 5 Jun 2020 13:01:10 +0000 (14:01 +0100)
committerwatkinsr <ryanwatkins54@gmail.com>
Fri, 5 Jun 2020 13:01:10 +0000 (14:01 +0100)
cf/cf [new file with mode: 0755]
cf/cf.go [new file with mode: 0644]
lengthconv/conv.go [new file with mode: 0644]
lengthconv/lengthconv.go [new file with mode: 0644]

diff --git a/cf/cf b/cf/cf
new file mode 100755 (executable)
index 0000000..534dd6f
Binary files /dev/null and b/cf/cf differ
diff --git a/cf/cf.go b/cf/cf.go
new file mode 100644 (file)
index 0000000..2804d20
--- /dev/null
+++ b/cf/cf.go
@@ -0,0 +1,28 @@
+package main
+
+import (
+  "fmt"
+  "os"
+  "strconv"
+
+  "git.ryanwatkins.me/goproglang/tempconv"
+  "git.ryanwatkins.me/goproglang/lengthconv"
+)
+
+func main() {
+  for _, arg := range os.Args[1:] {
+    t, err := strconv.ParseFloat(arg, 64)
+    if err != nil {
+      fmt.Fprintf(os.Stderr, "cf: %v\n", err)
+      os.Exit(1)
+    }
+    f := tempconv.Fahrenheit(t)
+    c := tempconv.Celsius(t)
+    fmt.Printf("%s = %s, %s = %s\n",
+      f, tempconv.FToC(f), c, tempconv.CToF(c))
+    ft := lengthconv.Feet(t)
+    m := lengthconv.Meters(t)
+    fmt.Printf("%s = %s, %s = %s\n",
+      ft, lengthconv.FtoM(ft), m, lengthconv.MtoF(m))
+  }
+}
diff --git a/lengthconv/conv.go b/lengthconv/conv.go
new file mode 100644 (file)
index 0000000..97e679f
--- /dev/null
@@ -0,0 +1,8 @@
+package lengthconv
+
+// CToF converts a Celsius temperature to Fahrenheit.
+func FtoM(f Feet) Meters { return Meters(f / 3.2808) }
+
+// FToC converts a Fahrenheit temperature to Celsius.
+func MtoF(m Meters) Feet { return Feet(m * 3.2808) }
+
diff --git a/lengthconv/lengthconv.go b/lengthconv/lengthconv.go
new file mode 100644 (file)
index 0000000..4a100ec
--- /dev/null
@@ -0,0 +1,11 @@
+package lengthconv
+
+import (
+  "fmt"
+)
+
+type Feet float64
+type Meters float64
+
+func (f Feet) String() string { return fmt.Sprintf("%g feet", f) }
+func (m Meters) String() string { return fmt.Sprintf("%g meters", m) }