diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2023-07-29 10:37:40 -0230 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2023-07-29 10:37:40 -0230 |
| commit | 0e1cd2b6a328887c1a630eae0b34366a607bb1b8 (patch) | |
| tree | 70825addc206c7b7e7fae33bbc0f86ab24534ee8 /calculator.go | |
| parent | 5b81647c27af77be3d35e10cb53cad1897a7f392 (diff) | |
| download | pfc-0e1cd2b6a328887c1a630eae0b34366a607bb1b8.zip | |
refactor
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) -} |