diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2025-10-04 13:56:56 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2025-10-04 13:56:56 -0400 |
| commit | 6817d5861986084f55785842b07639f7aeaacf0a (patch) | |
| tree | 5cbe9f9867dbea5df99f168f6c350a84e58f11cd | |
| parent | bc774798c0805af43558cf61152a65772fc8bbc6 (diff) | |
| download | pfc-6817d5861986084f55785842b07639f7aeaacf0a.zip | |
accept e-notation
| -rw-r--r-- | calc.go | 10 | ||||
| -rw-r--r-- | op.go | 9 | ||||
| -rw-r--r-- | ui.go | 24 |
3 files changed, 30 insertions, 13 deletions
@@ -61,17 +61,13 @@ func (c *Calculator) negate() { } } -// performOp performs the specified arithmetic operation. -func (c *Calculator) performOperation(operator byte) { - fn, err := parseOperator(operator) - if err != nil { - return - } +// operate performs a binary arithmetic operation on the stored values. +func (c *Calculator) operate(op Op) { lhs, rhs, err := c.operands() if err != nil { return } - c.stack.push(fn(lhs, rhs)) + c.stack.push(op(lhs, rhs)) c.buf = "" } @@ -5,9 +5,12 @@ import ( "math" ) -// parseOperator returns a closure that performs the specified arithmetic operation, -// or OpError if op is not a valid operator. -func parseOperator(op byte) (func(lhs float64, rhs float64) float64, error) { +// Op is a binary operator. +type Op func(lhs, rhs float64) float64 + +// parseOp parses a binary operator, returning a function that performs the operation, or +// OpError if op is not a valid operator. +func parseOperator(op byte) (Op, error) { switch op { case '+': return func(lhs, rhs float64) float64 { return lhs + rhs }, nil @@ -2,6 +2,7 @@ package main import ( "fmt" + "strings" "github.com/charmbracelet/bubbletea" ) @@ -52,9 +53,22 @@ func (ui UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) { ui.calc.angleMode = !ui.calc.angleMode case "N": ui.calc.negate() - case "+", "-", "*", "/", "%", "^": - operator := msg.String()[0] - ui.calc.performOperation(operator) + case "+", "-": + if isENotation(ui.calc.buf) { + ui.calc.buf += msg.String() + } else { + op, err := parseOperator(msg.String()[0]) + if err != nil { + panic(err) + } + ui.calc.operate(op) + } + case "*", "/", "%", "^": + op, err := parseOperator(msg.String()[0]) + if err != nil { + panic(err) + } + ui.calc.operate(op) case "backspace": if len(ui.calc.buf) > 0 { ui.calc.buf = ui.calc.buf[:len(ui.calc.buf)-1] @@ -75,6 +89,10 @@ func (ui UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return ui, nil } +func isENotation(s string) bool { + return strings.ContainsRune(s, 'e') +} + func (ui UI) View() string { s := padding(ui) |