Advertisement
Guest User

Untitled

a guest
Nov 11th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.45 KB | None | 0 0
  1. import UIKit
  2.  
  3. class ViewController: UIViewController {
  4.    
  5.     var numberFromScreen : Double = 0;
  6.     var firstNum : Double = 0;
  7.      var operation : Int = 0;
  8.     var mathSign : Bool = false;
  9.    
  10.     @IBOutlet weak var result: UILabel!
  11.    
  12.     @IBAction func digits(_ sender: UIButton) {
  13.         if mathSign == true {
  14.             result.text = String( sender.tag)
  15.             mathSign = false
  16.         }
  17.         else {
  18.  
  19.         result.text = result.text! + String(sender.tag)
  20.            
  21.         }
  22.        
  23.         numberFromScreen = Double(result.text!)!
  24.     }
  25.    
  26.    
  27.     @IBAction func buttons(_ sender: UIButton) {
  28.         if result.text != ""  &&  sender.tag != 10 && sender.tag != 15 {
  29.             firstNum = Double(result.text!)!
  30.             if sender.tag == 11 { // division
  31.                  result.text = "/";
  32.                
  33.             }
  34.             else if sender.tag == 12 { // multuply
  35.                     result.text = "x";
  36.                
  37.             }
  38.             else if sender.tag == 13 { // minus
  39.                  result.text = "-";
  40.                
  41.             }
  42.             else if sender.tag == 14 { // plus
  43.                 result.text = "+";
  44.             }
  45.             else if sender.tag == 17 { // plus
  46.                 result.text = String( sqrt(firstNum));
  47.             }
  48.             else if sender.tag == 20 { // plus
  49.         result.text = String(firstNum * firstNum);
  50.                
  51.             } else if sender.tag == 21 { // plus
  52.                 result.text = String(firstNum * firstNum*firstNum)
  53.                
  54.             } else if sender.tag == 16 { // plus
  55.                 func factorial(_ num : Int) -> Int
  56.                     {    var num = Int(firstNum)
  57.                         var factorialOfNumber = 0
  58.                         if num == 0
  59.                         {
  60.                             factorialOfNumber = 1
  61.                             return factorialOfNumber
  62.                         }
  63.                         return (1...num).reduce(1){$0 * $1}
  64.                         var n = ""
  65.                         result.text = String?(n)
  66.                         n = ((1...num).reduce(1){$0 * $1})
  67.             }
  68.             }
  69.        
  70.             else if sender.tag == 18 { // √
  71.                 result.text = "3.1459";
  72.                
  73.             }
  74.             else if sender.tag == 19 { // √
  75.                 result.text = "2.73711";
  76.                
  77.             }
  78.             operation = sender.tag
  79.             mathSign = true
  80.         }
  81.         else if sender.tag == 15 { //equal to = botton
  82.             if operation == 11 {
  83.                 result.text = String(firstNum / numberFromScreen)
  84.             }
  85.             else if operation == 12 {
  86.                 result.text = String( firstNum * numberFromScreen)
  87.             }
  88.             else if operation == 13 {
  89.                 result.text = String( firstNum - numberFromScreen)
  90.             }
  91.             else if operation == 14 {
  92.                 result.text = String( firstNum + numberFromScreen)
  93.             }
  94.             else if operation == 17 {
  95.                 result.text = String( sqrt(firstNum))
  96.             }
  97.             else if operation == 18 {
  98.                 result.text = String(3.1459)
  99.             }
  100.             else if operation == 19 {
  101.                 result.text = String(2.73911)
  102.             }
  103.             else if operation == 20 {
  104.                 result.text = String(firstNum * firstNum)
  105.             }
  106.             else if operation == 21 {
  107.                 result.text = String(firstNum * firstNum * firstNum)
  108.             }
  109.         else if operation == 16 {
  110.                 func factorial(_ num : Int) -> Int
  111.                 {    var num = Int(firstNum)
  112.                     var factorialOfNumber = 0
  113.                     if num == 0
  114.                     {
  115.                         factorialOfNumber = 1
  116.                         return factorialOfNumber
  117.                     }
  118.                     return (1...num).reduce(1){$0 * $1}
  119.                 }
  120.         }
  121.        
  122.         }
  123.         else if sender.tag == 10 {
  124.             result.text = ""
  125.             firstNum = 0
  126.             numberFromScreen = 0
  127.             operation = 0
  128.             // dispoose of any resources that can be recreated
  129.         }
  130.        
  131.     }
  132.    
  133.    
  134.    
  135.    
  136.    
  137.     override func viewDidLoad() {
  138.         super.viewDidLoad()
  139.         // Do any additional setup after loading the view, typically from a nib.
  140.     }
  141.  
  142.  
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement