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
|
/* https://theweeklychallenge.org/blog/perl-weekly-challenge-047/
TASK #1
Roman Calculator
Write a script that accepts two roman numbers and operation. It should then perform the operation on the give roman numbers and print the result.
For example,
perl ch-1.pl V + VI
It should print
XI
*/
package main
import (
"fmt"
"log"
"os"
"strings"
"github.com/pokgopun/go/roman"
)
func main() {
guide := `
Usage:
%cmd% %RomanString1% %op% %RomanString2%
supported ops are + and -
result less than 1 or greater than 3999 will get error as it cannot be displayed in standard roman form
MMXXII + DXLIII
MMDLXV
MMDLXV - DXLIII
MMXXII`
var str1, op, str2 string
_, err := fmt.Sscanf(strings.Join(os.Args[1:], " "), "%s %s %s", &str1, &op, &str2)
if err != nil {
fmt.Println(guide)
os.Exit(0)
}
roman := roman.NewRoman()
r1, err1 := roman.ToDec(str1)
r2, err2 := roman.ToDec(str2)
if err1 != nil {
fmt.Println("RomanString1:", err1)
}
if err2 != nil {
fmt.Println("RomanString2:", err2)
}
if err1 != nil || err2 != nil {
os.Exit(1)
}
switch op {
case "+":
r, err := roman.FromDec(r1 + r2)
if err != nil {
log.Fatal(err)
}
fmt.Println(r)
case "-":
r, err := roman.FromDec(r1 - r2)
if err != nil {
log.Fatal(err)
}
fmt.Println(r)
default:
log.Fatal("invalid ops: supported ops are +,-,*")
}
}
|