Calculator: an iOS application that does the job of a normal calculator

Overview

Calculator

Calculator is an iOS application that does the job of a normal calculator.

Application Features

  • It is a simple iOS app built by Swift, which can perform simple mathematical equations involving adding, subtracting multiplying and dividing.
  • The app takes in account operations priority when performing calculations.
  • More to be done.

Code Samples

@IBAction func divisionClicked(_ sender: Any) {
        if result.text != "" {
            rslt += "/"
        }
    }
    
    @IBAction func multplyClicked(_ sender: Any) {
        if result.text != "" {
            rslt += "*"
        }
    }
    
    @IBAction func subClicked(_ sender: Any) {
        if result.text == "0" {
            
        }else if !rslt.hasSuffix("+") || !rslt.hasSuffix("-") || !rslt.hasSuffix("*") || !rslt.hasSuffix("/"){
            let expression = NSExpression(format:rslt)
            if let total = expression.expressionValue(with: nil, context: nil) as? Double {
                result.text = IntFormat(total: total)
                rslt += "-"
            }
        }
    }
    
    @IBAction func addClicked(_ sender: Any) {
        if result.text == "0" {
            
        }else if rslt.hasSuffix("+"){
            let lastOperator = String(rslt.last!)
            rslt.removeLast()
            rslt.removeLast()
            rslt += lastOperator
            
        }else if !rslt.hasSuffix("+") || !rslt.hasSuffix("-") || !rslt.hasSuffix("*") || !rslt.hasSuffix("/"){
            let expression = NSExpression(format:rslt)
            if let total = expression.expressionValue(with: nil, context: nil) as? Double {
                result.text = IntFormat(total: total)
                rslt += "+"
            }
        }
    }
    
    @IBAction func equalClicked(_ sender: Any) {
        let expression = NSExpression(format: rslt)
        print(expression)
        if let total = expression.expressionValue(with: nil, context: nil) as? Double{
            result.text = IntFormat(total: total)
        }
    }
    
    @IBAction func acClicked(_ sender: Any) {
        result.text = "0"
        rslt = "1.0*"
    }
    
    func IntFormat(total : Double) -> String {
        if total.truncatingRemainder(dividingBy: 1) == 0{
            return String(format: "%.0f", total)
        }else{
            return String(total)
        }
    }
    
    func numberClicked(_ numberPressed: String){
        if result.text?.count == 1 && result.text == "0" {
            result.text = numberPressed
            rslt += numberPressed
        }else if rslt.hasSuffix("+") || rslt.hasSuffix("-") || rslt.hasSuffix("*") || rslt.hasSuffix("/"){
            result.text = numberPressed
            rslt += numberPressed
        }else{
            result.text! += numberPressed
            rslt += numberPressed
        }
    }

Demos


demo2.mp4
You might also like...
Implementation of LondonAppBrewery's BMI Calculator App
Implementation of LondonAppBrewery's BMI Calculator App

BMI Calculator 💪 Our Goal The objective of this tutorial is to look at how we can customise Flutter Widgets to achieve our own beautiful user interfa

A basic discount calculator app Using Swift
A basic discount calculator app Using Swift

DiscountCalcuator This is a basic discount calculator app I created. Have you ever been out shopping and there is a really awesome sale going on? Well

Splito - Nice and easy-to-use billing and tip calculator with swift
Splito - Nice and easy-to-use billing and tip calculator with swift

Splito! Nice and easy-to-use billing and tip calculator

CodePathPrework - The pre-work assignment for the CodePath program. Involves making a tip calculator using XCode.

Pre-work - Tip Calculator Tip Calculator is a tip calculator application for iOS. Submitted by: David Zhu Time spent: 3 hours spent in total User Stor

Recreation of calculator-checklist project in SwiftUI

Calculator Checklist A project created out of curiosity to test the waters of SwiftUI, Apple's new UI Framework. The caculator-checklist concept was o

BMI-Calculator - An app which calculates the BMI and gives suggestion
BMI-Calculator - An app which calculates the BMI and gives suggestion

BMI-Calculator An app which calculates the BMI and gives suggestion with MVC pat

BodyMassIndexCalculator - A multiscreen BMI Calculator app
BodyMassIndexCalculator - A multiscreen BMI Calculator app

Body Mass Index Calculator A multiscreen BMI Calculator app Uses UISliders for w

Stock Average Down Calculator
Stock Average Down Calculator

A calculator to figure out what your new average stock price and future value will be on purchasing of a new stock. Most of the custom code can be found in ViewController.swift

A Windows calculator ported to macOS with SwiftUI interface.
A Windows calculator ported to macOS with SwiftUI interface.

Windows Calculator on Apple A Windows calculator ported to macOS with SwiftUI interface. Standard Programmer Build Clone the project: git clone --recu

Owner
Alharbi
Alharbi
Tippy is a tip calculator application for iOS.

Pre-work - Tippy Tippy is a tip calculator application for iOS. Submitted by: Shoaib Huq Time spent: 5 hours spent in total User Stories The following

Shoaib Huq 0 Nov 24, 2021
Tipculator is a tip calculator application for iOS.

Pre-work - Tipculator Tipculator is a tip calculator application for iOS. Submitted by: Jordan Sukhnandan Time spent: Prework-> 3 hours Additional fea

Jordan Sukhnandan 0 Feb 1, 2022
A tip calculator for iOS (in Swift) and Android

Round & Split I needed a tip calculator, so I wrote one. I always round my tips, and when I dine out with a friend, I email them reminders that I owe

Lukhnos Liu 48 Jun 25, 2022
Basic calculator app for iOS devices using Swift3. Created for learning purpose.

iOS Calculator App Basic calculator app for iOS devices using Swift 4 and Xcode 9. Created for learning purpose. How to use Just download source code

Jogendra 15 Dec 28, 2022
iOS calculator app made without storyboard.

Calculator app Standart iOS calculator app written without storyboard - elements placed only by using code. FUN FACT - you can download it and install

Paweł Brzozowski 1 Dec 21, 2021
Bmi calculator app (iOS)

BMI Calculator Our Goal The goal of this tutorial is to learn more about Optionals, solidify your understanding of the MVC design pattern and to intro

null 0 Jan 3, 2022
A clone of the built-in iOS Calculator APP for iPadOS, using SwiftUI

A clone of the built-in iOS Calculator APP for iPadOS, using SwiftUI Yes, it looks exactly the same as the iOS built-in Calculator The UI is implement

Bobby Wang 31 Jan 1, 2023
Free42 : An HP-42S Calculator Simulator

------------------------------------------------------------------------------- Free42 is a software clone of the Hewlett-Packard 42S calculator. If y

Thomas Okken 211 Dec 14, 2022
An experimental prototype calculator.

What is NumberPad NumberPad is an experimental prototype to explore how we write, visualize, and manipulate math equations. This post highlights some

Bridger Maxwell 87 Apr 25, 2022
SIP calculator calculates the future value of SIP (Systematic Investment Plan) payments.

SIP calculator calculates the future value of SIP (Systematic Investment Plan) payments. This app is available in the AppStore. Learn more here. #Desc

Tirupati Balan 26 Dec 7, 2022