NSExpression Calculator in Swift

I'm trying to duplicate. I need to write a calculator in Objective-C in Swift, but my code does not work.

import Foundation

var equation:NSString = "5*(2.56-1.79)-4.1"

var result = NSExpression(format: equation, argumentArray: nil)

println(result)
+5
source share
2 answers

As mentioned in the comment, you must call expressionValueWithObject() on the expression:

let expr = NSExpression(format: equation)
if let result = expr.expressionValueWithObject(nil, context: nil) as? NSNumber {
    let x = result.doubleValue
    println(x)
} else {
    println("failed")
}

Update for Swift 3:

let expr = NSExpression(format: equation)
if let result = expr.expressionValue(with: nil, context: nil) as? Double {
    print(result) // -0.25
} else {
    print("failed")
}
+8
source

the details

  • Xcode 9.4.1, Swift 4.1
  • Xcode 10.2.1 (10E1001), Swift 5

Decision

import Foundation

extension String {

    private func allNumsToDouble() -> String {

        let symbolsCharSet = ".,"
        let fullCharSet = "0123456789" + symbolsCharSet
        var i = 0
        var result = ""
        var chars = Array(self)
        while i < chars.count {
            if fullCharSet.contains(chars[i]) {
                var numString = String(chars[i])
                i += 1
                loop: while i < chars.count {
                    if fullCharSet.contains(chars[i]) {
                        numString += String(chars[i])
                        i += 1
                    } else {
                        break loop
                    }
                }
                if let num = Double(numString) {
                    result += "\(num)"
                } else {
                    result += numString
                }
            } else {
                result += String(chars[i])
                i += 1
            }
        }
        return result
    }

    func calculate() -> Double? {
        let transformedString = allNumsToDouble()
        let expr = NSExpression(format: transformedString)
        return expr.expressionValue(with: nil, context: nil) as? Double
    }
}

using

"3 * (3-1) -5". Calculate ()

Full sample

    func test(_ expressrion: String) {
    if let num = expressrion.calculate() {
        print("\(expressrion) = \(num)")
    } else {
        print("\(expressrion) = nil")
    }
}

test("3*(3-1)-5")
test("5.2*(2-1.79)-5.1")
test("11/5")

results

enter image description here

+2
source

Source: https://habr.com/ru/post/1548112/


All Articles