func mulAdd(mul1:Int,mul2:Int,add:Int) -> Int {
let result = mul1 * mul2 + add
return result
}
var result = 0
result = mulAdd(mul1: 3, mul2: 5, add: 3)
print("The result of mulAdd is \(result)")
func mulAdd() -> Int {
let mul1,mul2,add : Int
mul1 = 3
mul2 = 5
add = 3
return mul1 * mul2 + add
}
result = mulAdd()
print("The result of mulAdd is \(result)")
func mulAdd(mul1:Int,mul2:Int,add:Int){
print("The result of mulAdd is \(mul1 * mul2 + add)")
}
mulAdd(mul1:3, mul2:5, add:3)
func printCapitalInfo(name: String, country: String, population: Int) {
print("\(name) is the capital of \(country) and its population is \(population) million.")
}
printCapitalInfo(name: "Beijing", country: "China", population: 23)
func calculateCapitalInfo(name: String, country: String, population: Int) -> (String, Int) {
let capitalInfo = name + " is the capital of "
+ country + " and its population is "
+ String(population) + " million."
let lengthOfInfo = capitalInfo.count
return (capitalInfo, lengthOfInfo)
}
let capitalInfo = calculateCapitalInfo(name: "Beijing", country: "China", population: 23)
print(capitalInfo)
func climate(city:String)->(averageTemperature:Int,weather:String,wind:String) {
var averageTemperature : Int
var weather,wind : String
switch city {
case "beijing" : averageTemperature = 25; weather = "dry"; wind = "strong"
case "shanghai" : averageTemperature = 15; weather = "wet"; wind = "weak"
default : averageTemperature = 10; weather = "sunny"; wind = "normal"
}
return (averageTemperature,weather,wind)
}
var climateTemp = (0, "", "")
climateTemp = climate(city: "beijing")
print(climateTemp)
func sum(numbers : Int...) -> Int{
var result = 0
for number in numbers {
result = result + number
}
return result
}
var sums = sum(numbers: 1,2,3,4,5,6,7,8,9)
print(sums)
sums = sum(numbers: 10,11,12)
print(sums)
func swap(a:inout Int, b:inout Int) {
let temp = a
a = b
b = temp
}
var a = 5
var b = 6
swap(a: &a, b: &b)
print("a is \(a)")
print("b is \(b)")
let add: (Int, Int) -> Int = { (a, b) in
return a + b
}
let result = add(3, 5)
print(result)
func sum(a: Int , b: Int, fn: (Int, Int) -> Int){
print(fn(a,b))
}
sum(a: 30, b: 40, fn: {
(a:Int, b:Int) -> Int in
a + b
} )
sum(a: 40, b: 30, fn: {
a,b in
a + b
})
sum(a: 30, b: 40, fn: {$0 + $1})
sum(a: 50, b: 40, fn: + )
let numbers = [1, 2, 3, 4, 5]
let mapped = numbers.map { $0 * 2 }
func printIfTrue(_ predicate: @autoclosure () -> Bool) {
if predicate() {
print("条件为真")
}
}
printIfTrue(2 > 1)
struct Book {
var name = ""
var price = 0
var category = "common"
func description() {
print("\(name)'s price is \(price), category is \(category)")
}
}
var theBook = Book(name: "Life of Pi", price: 62, category: "adventure")
print("\(theBook.name)'s category is \(theBook.category) and price is \(theBook.price)RMB")
let newBook = Book(name: "Life of Pi", price: 62, category: "adventure")
newBook.description()
var anotherBook = theBook
print(theBook)
print(anotherBook)
anotherBook.category = "history"
anotherBook.price = 136
anotherBook.name = "Empiror Kangxi"
print(theBook)
print(anotherBook)
struct Coordinate {
let x : Double
let y : Double
}
struct Line {
let startPoint: Coordinate
let endPoint: Coordinate
func length() -> Double {
let x = startPoint.x - endPoint.x
let y = startPoint.y - endPoint.y
return (x * x + y * y).squareRoot()
}
}
let pointA = Coordinate(x: 1, y: 2)
let pointB = Coordinate(x: 3, y: 6)
let lineAB = Line(startPoint: pointA, endPoint: pointB)
print("The length of line AB is \(lineAB.length())")
class Student {
var name = ""
var age = 0
var id = ""
var basicInfo : String {
return "\(name) is \(age) years old, the id is \(id)"
}
func chooseClass(){
print("\(name) choose a class.")
}
func haveClass(){
print("\(name) have a class.")
}
}
let theStudent = Student()
theStudent.name = "Tommy"
theStudent.age = 19
theStudent.id = "37060115"
print(theStudent.basicInfo)
class Graduate : Student {
var supervisor = ""
var researchTopic = ""
func chooseSuperVisor(superVisor:String){
self.supervisor = superVisor
}
}
let theGraduate = Graduate()
theGraduate.name = "Sam"
theGraduate.age = 23
theGraduate.id = "SY0602115"
theGraduate.haveClass()
theGraduate.researchTopic = "Graphics"
theGraduate.chooseSuperVisor(superVisor: "Ian")
print("Graduate \(theGraduate.name) is \(theGraduate.age) and the id is \(theGraduate.id), The research topic is \(theGraduate.researchTopic) and supervisor is \(theGraduate.supervisor)")
class Doctor: Graduate {
var articles = [String]()
func publishArticle(article : String){
articles.append(article)
}
}
let theDoctor = Doctor()
theDoctor.name = "Pennie"
theDoctor.age = 26
theDoctor.id = "BY0607120"
print(theDoctor.basicInfo)
theDoctor.chooseSuperVisor(superVisor: "Ellis")
print(theDoctor.supervisor)
theDoctor.publishArticle(article: "Petri nets theory")
theDoctor.publishArticle(article: "Process management")
print(theDoctor.articles)
class Student {
var name = ""
var age = 0
var id = ""
var basicInfo : String {
return "\(name) is \(age) years old, the id is \(id)"
}
func chooseClass(){
print("\(name) choose a class.")
}
func haveClass(){
print("\(name) have a class.")
}
}
class Graduate : Student {
var supervisor = ""
var researchTopic = ""
override var age : Int {
didSet {
print("age is set from \(oldValue) to \(age)")
}
willSet {
print("original age will be set to \(newValue)")
}
}
override var basicInfo : String{
return super.basicInfo + ", supervisor is \(supervisor), research topic is \(researchTopic)"
}
func chooseSuperVisor(superVisor : String){
self.supervisor = superVisor
}
override func chooseClass() {
print("graduate \(name) choose a class")
}
}
func sportGameRoster(stu:Student)->String{
return "Athlete name:\(stu.name),age:\(stu.age),id:\(stu.id)"
}
let studenteTom = Student()
studenteTom.name = "Tom"
studenteTom.age = 19
studenteTom.id = "37060116"
let graduateJim = Graduate()
graduateJim.name = "Jim"
graduateJim.age = 24
graduateJim.id = "SY060218"
let rosterTom = sportGameRoster(stu: studenteTom)
print(rosterTom)
let rosterJim = sportGameRoster(stu: graduateJim)
print(rosterJim)