A Simple Calculator App created by implementing binary operations in Swift.
siddique26 / calculator-app Goto Github PK
View Code? Open in Web Editor NEWA Simple Calculator App.
A Simple Calculator App.
For Division.
let lhs = currentValue.index(currentValue.startIndex, offsetBy: 0)
let rhs = currentValue.index(currentValue.startIndex, offsetBy: 2)
//Here LHS and RHS are just indices 0 and 2
let a = String(currentValue[lhs])
let b = String(currentValue[rhs])
//a is just the character in position 0 and b is the character in position 2 (typecast as strings)
let operand1 = Double(a)
let operand2 = Double(b)
// operand 1 is double of the a. operand 2 is double of b
//This works if the two characters are numbers. But not if they are non-numeric.
//If string is "1/3" then a is 1 and b is 3. So Double(a) and Double(b) are valid.
//But if String is "12/3" then a is 1 and b is /. Double(b) is Double(/) which is invalid. So nil is //returned
answer = operand1! / operand2!
//Here you are unwrapping an optional by using !. Operand1 and Operand2 must not be nil.
//But they will be nil if either a or b are non-numeric and cannot be converted to Double.
Another error is that you can only divide with string of form "a/b" where a and b are two digits.
What about "aaa/b" "aa/bbb" etc etc.
The better solution is to use
let numberArray = currentValue.components(separatedBy: "/")
This returns an array of strings separated by "/". Then take the first two elements of the array and convert to double. Then divide.
```
let a = numberArray[0]
let b = numberArray[1]
let operand1 = Double(a)
let operand2 = Double(b)
let answer: Double = operand1! / operand2!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.