diff options
Diffstat (limited to 'calculator.go')
| -rw-r--r-- | calculator.go | 54 |
1 files changed, 0 insertions, 54 deletions
diff --git a/calculator.go b/calculator.go index 3493382..b5dae10 100644 --- a/calculator.go +++ b/calculator.go @@ -1,27 +1,10 @@ package main import ( - "fmt" - "math" "strconv" "strings" ) -type Stack []float64 - -func (s *Stack) push(v float64) { - *s = append(*s, v) -} - -func (s *Stack) pop() *float64 { - if len(*s) > 0 { - v := (*s)[len(*s)-1] - *s = (*s)[:len(*s)-1] - return &v - } - return nil -} - type Calculator struct { stack Stack buf string @@ -79,40 +62,3 @@ func (c *Calculator) performOp(op byte) error { c.buf = "" return nil } - -// parseOp returns a closure that performs the specified arithmetic operation, -// or OpError if op is not a valid operator. -func parseOp(op byte) (func(lhs *float64, rhs float64), error) { - switch op { - case '+': - return func(lhs *float64, rhs float64) { *lhs += rhs }, nil - case '-': - return func(lhs *float64, rhs float64) { *lhs -= rhs }, nil - case '*': - return func(lhs *float64, rhs float64) { *lhs *= rhs }, nil - case '/': - return func(lhs *float64, rhs float64) { - if rhs != 0 { - *lhs /= rhs - } - }, nil - case '%': - return func(lhs *float64, rhs float64) { - if rhs != 0 { - *lhs = float64(int64(*lhs) % int64(rhs)) - } - }, nil - case '^': - return func(lhs *float64, rhs float64) { *lhs = math.Pow(*lhs, rhs) }, nil - } - return nil, OpError{op} -} - -// OpError records an invalid arithmetic operator. -type OpError struct { - c byte -} - -func (e OpError) Error() string { - return fmt.Sprintf("invalid operator: %c", e.c) -} |