diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2023-07-27 11:53:19 -0230 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2023-07-27 11:53:19 -0230 |
| commit | e89fd14004291f65998b8fa6b5febc2d2fdc52d6 (patch) | |
| tree | 40dda188656c8505ad6fa9aa30ebd8a4cab34e06 /calculator.go | |
| parent | 1743ab7e7f9a38acff10fb6593dbc2e2f8c6f7ab (diff) | |
| download | pfc-e89fd14004291f65998b8fa6b5febc2d2fdc52d6.zip | |
addition operator
Diffstat (limited to 'calculator.go')
| -rw-r--r-- | calculator.go | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/calculator.go b/calculator.go new file mode 100644 index 0000000..30cc7ec --- /dev/null +++ b/calculator.go @@ -0,0 +1,26 @@ +package main + +import "strconv" + +type Stack []float64 + +type Calculator struct { + stack Stack + buffer string +} + +// add performs addition when the user inputs the '+' operator. +func (c *Calculator) add() { + if len(c.stack) < 1 { + return + } + if con := parseConstant(c.buffer); con != nil { + c.stack[len(c.stack)-1] += *con + } else if f, err := strconv.ParseFloat(c.buffer, 64); err == nil { + c.stack[len(c.stack)-1] += f + } else if len(c.stack) > 1 { + c.stack[len(c.stack)-2] += c.stack[len(c.stack)-1] + c.stack = c.stack[:len(c.stack)-1] + } + c.buffer = "" +} |