String Calculator

Let’s write a calculator that takes a string and evaluates it as if it were a calculation. The idea is to implement it a bit at a time with tests, and see where the design ends up. The kata description contains quite a detailed list of requirements and examples, written in the order you should implement them in.

Note: if your language has a built-in ‘eval’ or another kind of runtime evaluation of string expressions - you’re not allowed to use it in your solution to this exercise!

Step 0 - Integers

Let’s start with the basics! If you give your String Calculator a single integer, just return that integer.

Step 1 - Addition

If you get two integers separated by a + sign, add them together.

Step 2 - Subtraction

Also handle minus signs.

Step 3 - Whitespace

Whitespace should be ignored

Further steps

I’m sure you can think of more steps to add here. Multiplication, floating point numbers, parentheses… Try to continue in the same style though. Add one small thing at a time with a test and just enough code to make it work, while all the existing tests also keep working.

Referenced in these Learning Hours: