help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • Blog
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
swift-ios-calculator
RSK World
swift-ios-calculator
Swift iOS Calculator v1.0 - AI Math Solver + 3D Graphing + Apple Watch Integration + iOS Widgets + Siri Shortcuts + Currency Converter + Scientific Calculator + Matrix Operations + Platform Integration + Modern iOS Development
swift-ios-calculator
  • Assets.xcassets
  • Base.lproj
  • swift-ios-calculator.xcodeproj
  • AIMathSolverViewController.swift26.6 KB
  • AppDelegate.swift1.7 KB
  • CalculatorHistoryViewController.swift6.5 KB
  • CalculatorLogic.swift4.4 KB
  • CalculatorSettingsViewController.swift5.6 KB
  • CalculatorTheme.swift8.3 KB
  • CalculatorUtils.swift6.9 KB
  • CalculatorViewController.swift3.4 KB
  • CalculatorWidget.swift14.3 KB
  • CalculatorWidgetInfo.plist846 B
  • ChemistryCalculatorViewController.swift26.9 KB
  • CurrencyConverterViewController.swift13.3 KB
  • CustomFormulaBuilderViewController.swift22.1 KB
  • EngineeringCalculatorViewController.swift25.7 KB
  • EquationSolverViewController.swift22.8 KB
  • FinancialCalculatorViewController.swift27.5 KB
  • GeometryCalculatorViewController.swift29.7 KB
  • Graphing3DViewController.swift20.8 KB
  • GraphingCalculatorViewController.swift14.7 KB
  • Info.plist3.2 KB
  • LICENSE1.1 KB
  • MainTabBarController.swift22.3 KB
  • MatrixCalculatorViewController.swift26.6 KB
  • PRIVACY_POLICY.md1.6 KB
  • PhysicsCalculatorService.swift8.2 KB
  • ProgrammerCalculatorViewController.swift11 KB
  • README.md8.7 KB
  • RELEASE_NOTES.md6.5 KB
  • SceneDelegate.swift2.8 KB
  • ScientificCalculatorViewController.swift6.2 KB
  • SiriShortcutsManager.swift23.5 KB
  • Swift iOS Calculator.entitlements1.1 KB
  • UnitConverterViewController.swift14 KB
  • WatchCalculatorViewController.swift15.3 KB
  • index.html47.5 KB
CalculatorLogic.swift
CalculatorLogic.swift
Raw Download
Find: Go to:
//
//  CalculatorLogic.swift
//  Swift iOS Calculator
//
//  Created by RSK World on 23/01/2026.
//  Copyright © 2026 RSK World. All rights reserved.
//
//  Developer: Molla Samser (Founder, RSK World)
//  Designer & Tester: Rima Khatun
//  Contact: info@rskworld.com, +91 93305 39277
//  Website: https://rskworld.in
//  Address: Nutanhat, Mongolkote, Purba Burdwan, West Bengal, India - 713147
//

import Foundation

struct CalculatorLogic {
    
    // MARK: - Properties
    private var firstNumber: Double?
    private var secondNumber: Double?
    private var operation: String?
    private var intermediateResult: Double?
    private var memoryValue: Double = 0.0
    private var history: [String] = []
    
    // MARK: - Public Methods
    mutating func setNumber(_ number: String) {
        if let doubleValue = Double(number) {
            if operation == nil {
                firstNumber = doubleValue
            } else {
                secondNumber = doubleValue
            }
        }
    }
    
    mutating func calculate(symbol: String) -> Double? {
        if symbol == "=" {
            return performCalculation()
        } else {
            operation = symbol
            if let first = firstNumber {
                intermediateResult = first
            }
            return intermediateResult
        }
    }
    
    mutating func clear() {
        firstNumber = nil
        secondNumber = nil
        operation = nil
        intermediateResult = nil
    }
    
    mutating func clearAll() {
        clear()
        memoryValue = 0.0
        history.removeAll()
    }
    
    // MARK: - Memory Functions
    mutating func memoryClear() {
        memoryValue = 0.0
    }
    
    mutating func memoryRecall() -> Double? {
        return memoryValue
    }
    
    mutating func memoryAdd(_ number: Double) {
        memoryValue += number
    }
    
    mutating func memorySubtract(_ number: Double) {
        memoryValue -= number
    }
    
    mutating func memoryStore(_ number: Double) {
        memoryValue = number
    }
    
    // MARK: - History Functions
    mutating func addToHistory(_ calculation: String) {
        history.append(calculation)
        if history.count > 100 {
            history.removeFirst()
        }
    }
    
    func getHistory() -> [String] {
        return history
    }
    
    mutating func clearHistory() {
        history.removeAll()
    }
    
    // MARK: - Private Methods
    private func performCalculation() -> Double? {
        guard let first = firstNumber,
              let second = secondNumber,
              let op = operation else {
            return firstNumber
        }
        
        let result: Double
        
        switch op {
        case "+":
            result = first + second
        case "-":
            result = first - second
        case "×":
            result = first * second
        case "÷":
            if second == 0 {
                return nil // Division by zero
            }
            result = first / second
        default:
            return nil
        }
        
        // Update first number for chained calculations
        firstNumber = result
        secondNumber = nil
        operation = nil
        
        return result
    }
    
    // MARK: - Scientific Functions
    mutating func performScientificOperation(_ operation: String) -> Double? {
        guard let number = firstNumber else { return nil }
        
        let result: Double
        
        switch operation {
        case "sin":
            result = sin(number * .pi / 180) // Convert to radians
        case "cos":
            result = cos(number * .pi / 180)
        case "tan":
            result = tan(number * .pi / 180)
        case "√":
            result = sqrt(number)
        case "x²":
            result = number * number
        case "x³":
            result = number * number * number
        case "log":
            result = log10(number)
        case "ln":
            result = log(number)
        case "1/x":
            if number == 0 {
                return nil
            }
            result = 1 / number
        case "π":
            result = .pi
        case "e":
            result = .e
        default:
            return nil
        }
        
        firstNumber = result
        return result
    }
}
174 lines•4.4 KB
swift

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer