swift-Calculator (Stanford Open Class)

Source: Internet
Author: User

Look at Stanford old man's class, really feel, my Chinese how also changed so rubbish. It's about IOS8 's course, written by Swift, a calculator application, a look at someone else's class, and then a look at our school class (but the garbage school, the sheer waste of college), nonsense, nonsense, continue nonsense. The old man's code some I gave omitted, do not know what will happen, anyway I did not find, if you found out, please tell me. And there is the specific operation of this calculator step is, you first 4-> enter, and 5-> carriage return, multiplication sign display calculation results 20

The first is the interface of the building, is 0-9 altogether 10 buttons, and then subtraction four operation keys, as well, a return key (used to store the number of inputs in the stack, anyway, the old man said, he put in the number of input into an array of var operandstack = array <Double> (), there is a label that shows the number of inputs and the input results. These are all done in the storyboard (except, of course, the array), and then the label is associated with the code

@IBOutlet weak var resultlabel:uilabel!

In fact, specifying the type here is considered a bad way to write, and if you can infer the type, you should let the compiler infer it yourself.

var Isstart:bool = False

var Isstart = false//The user is not already entered, the foreigner used a long string of characters Userisinthemiddleoftypinganumber

Associate 0-9 of these 10-digit Click events with your code

@IBAction func Buttontap (Sender:uibutton) {

Let digit = Sender.titlelabel?. text//Note that this digit is an optional value (opentional), which is the number on the button you clicked,

If isstart{

Resultlabel.text = resultlabel.text! + digit! Optional values are not spliced strings, so in this place, unpacking is required

}else{

Resultlabel.text = digit//User First point (as I understand it), the result box shows the number you pressed

Isstart = true//This means that the user has started to enter the

}

}

var operandstack = array<double> ()

This variable is an internal stack that stores the numbers you enter, his type is an array, the array is a double variable, note that the place to initialize, (we can not use without initialization, will be error)

@IBAction Func Enter () {

Isstart = false//Because we need to put the number just entered in the stack and lose the second number when we finish typing, so we need to set this Boolean value to False

var value = (resultlabel.text! as nsstring). Doublevalue

Operandstack.append (Resultvalue)

println ("Operandstack: \ (operandstack)")

}

Computed properties

var resultvalue:double{

get{

Return Nsnumberformatter (). numberfromstring (resultlabel.text!)!. Doublevalue

}

set{

Resultlabel.text = "\ (newvalue)"//newvalue is the system, that is, the value that you want to set 2 converts a double type to a string

Isstart = false//This sentence is written in this do not know what the meaning, how to understand it?

}

}

@IBAction func operate (Sender:uibutton) {

Let operation = sender.currenttitle!

Switch operation{

Case "??": Performoperation {$ * $}

Case "??": Performoperation {$ * $}//This place, this place really pit, $ * $, I thought the pass into a random two number on the line, the old man to this code of the continuous simplification, just wrote this way, Make the simplification process separate tomorrow (Tomorrow's Day)

Case "?": Performoperation {$/$}

Case "?": Performoperation {$-$}

Case "?": Performoperation {$ + $}

Default:break

}

}

Func performoperation (Operation: (double,double)->double) {//function parameter is a function

If Operandstack.count >= 2{

Resultvalue = operation (Operandstack.removelast (), Operandstack.removelast ())//operandstack.removelast () does that mean that This method deletes and returns the last number?

}

}

Func multipy (op1:double,op2:double)->double{

//

return OP1 * OP2

//    }

swift-Calculator (Stanford Open Class)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.