Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class ViewController: UIViewController {
- var numberFromScreen : Double = 0;
- var firstNum : Double = 0;
- var operation : Int = 0;
- var mathSign : Bool = false;
- @IBOutlet weak var result: UILabel!
- @IBAction func digits(_ sender: UIButton) {
- if mathSign == true {
- result.text = String( sender.tag)
- mathSign = false
- }
- else {
- result.text = result.text! + String(sender.tag)
- }
- numberFromScreen = Double(result.text!)!
- }
- @IBAction func buttons(_ sender: UIButton) {
- if result.text != "" && sender.tag != 10 && sender.tag != 15 {
- firstNum = Double(result.text!)!
- if sender.tag == 11 { // division
- result.text = "/";
- }
- else if sender.tag == 12 { // multuply
- result.text = "x";
- }
- else if sender.tag == 13 { // minus
- result.text = "-";
- }
- else if sender.tag == 14 { // plus
- result.text = "+";
- }
- else if sender.tag == 17 { // plus
- result.text = String( sqrt(firstNum));
- }
- else if sender.tag == 20 { // plus
- result.text = String(firstNum * firstNum);
- } else if sender.tag == 21 { // plus
- result.text = String(firstNum * firstNum*firstNum)
- } else if sender.tag == 16 { // plus
- func factorial(_ num : Int) -> Int
- { var num = Int(firstNum)
- var factorialOfNumber = 0
- if num == 0
- {
- factorialOfNumber = 1
- return factorialOfNumber
- }
- return (1...num).reduce(1){$0 * $1}
- var n = ""
- result.text = String?(n)
- n = ((1...num).reduce(1){$0 * $1})
- }
- }
- else if sender.tag == 18 { // √
- result.text = "3.1459";
- }
- else if sender.tag == 19 { // √
- result.text = "2.73711";
- }
- operation = sender.tag
- mathSign = true
- }
- else if sender.tag == 15 { //equal to = botton
- if operation == 11 {
- result.text = String(firstNum / numberFromScreen)
- }
- else if operation == 12 {
- result.text = String( firstNum * numberFromScreen)
- }
- else if operation == 13 {
- result.text = String( firstNum - numberFromScreen)
- }
- else if operation == 14 {
- result.text = String( firstNum + numberFromScreen)
- }
- else if operation == 17 {
- result.text = String( sqrt(firstNum))
- }
- else if operation == 18 {
- result.text = String(3.1459)
- }
- else if operation == 19 {
- result.text = String(2.73911)
- }
- else if operation == 20 {
- result.text = String(firstNum * firstNum)
- }
- else if operation == 21 {
- result.text = String(firstNum * firstNum * firstNum)
- }
- else if operation == 16 {
- func factorial(_ num : Int) -> Int
- { var num = Int(firstNum)
- var factorialOfNumber = 0
- if num == 0
- {
- factorialOfNumber = 1
- return factorialOfNumber
- }
- return (1...num).reduce(1){$0 * $1}
- }
- }
- }
- else if sender.tag == 10 {
- result.text = ""
- firstNum = 0
- numberFromScreen = 0
- operation = 0
- // dispoose of any resources that can be recreated
- }
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view, typically from a nib.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement