Logic for Making Simple iOS Calculator App

Mohammad Shayan
The Startup
Published in
6 min readApr 3, 2020

Starting out with iOS and Swift, a basic project to start out with is building a simple calculator app. Let’s look at a design and see what we can do in our code.

So in this simple design, there’s two UIViews, the top one covers one third of the screen and the second covers the rest. In the first UIView, there’s one text field and one label (though both could’ve been labels). The second UIView has 17 buttons (in stack views)

Let’s go into the logic

Using the MVC model, we start with making a Calculator Struct.

First we have a variable called “current” holding the current number. Then we have a variable for the first operand and another for the second operand. Another variable for the operation type (an enum with possibility for add, subtract, multiply, divide, equals). One variable for the result of the operation. Then we have another variable to determine if we’re working on first operand or the second (I used an enum).

Finally, we have three booleans. One to check if we can use the decimal point. If the current number we are working on doesn’t have a decimal, then we can allow for the decimal to be appended. If the current number has a decimal then we don’t allow for another decimal to be added. The next boolean is to see if we can allow the “minus” sign to work. The minus can be a operator or a negative sign so we need to see what’s meant when the user taps the button. The last variable is to check whether or not to clear the screen after the user taps the equals buttons (after two numbers and an operation were inputted) and then taps something else. If after getting the result the user decides to continue with that number and taps an operation, then the program goes on, but if the user decides to start something new and taps a number, the screen should clear and that number would the start of a new operation. I will explain more once I talk about the methods necessary for the calculator to work.

Next let’s start off with the easier functions. First we can have a method to get a string for the label under the text field. We want the label to show what’s going on since the text field will just show the current operand being worked on and the result after tapping the equals sign. So this method will return the left operand if it exists plus the operation if it exists and the right operand if it exists.

Next, the function for one the calculator is reset (holding the DEL button).

Next function is for doing the operation assuming the left operand, right operand, and the opeartion variable aren’t nil. If the operation is to add, then we add the two operands and similar operations for multiply, divide, and subtract.

The next function is to reset the current operand. This will be called when we tap the delete button, and there was only one digit, that operand has now gone, so the operand that we were working on needs to be cleared.

The next function is to let the Calculator that we want to work on the next number. One situation is when we are working on the left side and want to start on the right side. All we do is change our workingOn variable to rightSide. But if we already were working on the right side (like 2 + 3 then the multiply sign was tapped), we perform the 2 + 3 operation and store it on the left side and empty the right side and continue working on the right side. In both cases, we need to allow the negative sign and allow decimals to work for the new number and make sure whatever we enter next doesn’t erase the previous entry.

Next is to always update the current operand when a new number is tapped. So if the working operand is 2 and then a 5 is tapped, we need the operand variable to be updated from 2 to 25.

Next is a delete function to be called when the delete button is tapped (not held) and it’s supposed to remove the last number tapped. But if this is tapped after we got a result by tapping the equal button, we need to call the reset function and start fresh. If what we deleted was the negative sign or a decimal point, we need to allow for another negative sign or decimal to be tapped. As said above, if what remains after the delete is a negative sign or if nothing remains, then we want to reset the working operand.

The last three functions deal with when a new number was tapped, when the decimal was tapped, and when any of the operation buttons were tapped.

When a number is tapped we will just add that number to the current string. But we tapped on a number after we got a result from the equals sign, then the current number would be cleared and the tapped number would the be the start of the current operand.

Next is when the decimal point is tapped. In a normal situation, we just append the decimal to the current operand and set the canTakeDecimal boolean to false. But if the user tapped it again, then we just return. Or, if this is after the equals sign was tapped (and newNumberWillClear was set to true), then we reset all the operands and the decimal is the start of the first operand.

The last function is when an operator was tapped. In this we also have to decide when the subtract signs means subtract and when it means negative. So if the operation tapped was subtract and our variable for taking minus was false, then we return. If the operation wasn’t equal and there was a number in the label when the operation was tapped, then we go to next operand (the function was talked about above) and reset the current operand and set the current operation. But if there was no number when the operation was tapped, then we change the operation to the new operation, unless if it’s a minus, where we take that as a negative sign. If the operation tapped was the equals sign, then we perform the operation and update the result and make that the left operand (in case we want to continue with that result) and also set the flag to clear to be true if another digit was pressed

Source Code:

https://github.com/mshayanh13/Calculator

--

--

Mohammad Shayan
The Startup

iOS Developer, JavaScript Front-End and Back-End Developer