Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // Calculator
- //
- // Created by Sabin Tabirca on 29/01/2024.
- //
- import UIKit
- class ViewController: UIViewController {
- // outlet and actions
- @IBOutlet weak var screen: UITextField!
- @IBAction func clearAction(_ sender: Any) {
- // clear screen and reset vars
- screen.text = ""
- }
- @IBAction func equalAction(_ sender: Any) {
- // get the current value
- let currentValue = screen.text!
- // convert them
- let value1 = Double(screenValue)!
- let value2 = Double(currentValue)!
- var result : Double = 0
- switch oper{
- case "+":
- result = value1 + value2
- screen.text = String(result)
- case "-":
- result = value1 - value2
- screen.text = String(result)
- case "*":
- result = value1 * value2
- screen.text = String(result)
- case "/":
- if value2 != 0{
- result = value1 / value2
- screen.text = String(result)}
- else{
- screen.text = "ERROR"
- }
- default:break
- }
- }
- @IBAction func operationAction(_ sender: Any) {
- // memorise the operation to compute
- oper = ((sender as! UIButton).titleLabel?.text)!
- // memorise teh screen value
- screenValue = screen.text!
- // clear the screen
- screen.text = ""
- }
- @IBAction func digitAction(_ sender: Any) {
- // append the button label to screen's text
- let screenText = screen.text
- let digit = (sender as! UIButton).titleLabel?.text
- screen.text = "\(screenText!)\(digit!)"
- }
- // vars
- var oper = ""
- var screenValue = ""
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement