用swiftUI写一个简单的计算器
直接上代码:
struct ContentView: View {
@State var number:String = "0"
var body: some View {
VStack {
HStack{
Text("Calculator").font(.largeTitle)
Spacer()
}
Spacer()
HStack{
Spacer()
Text(number)
.font(.system(size: 50, weight: .thin, design: .rounded))
}
Spacer().frame(height:20)
NumberPad(number: $number)
}
.padding()
.background(.black)
.foregroundColor(.white)
}
}
下面是主要负责处理按键的试图
struct NumberPad:View{
@Binding var number:String
@State var lastOperation:operation = .none
@State var lastNumber:String? = nil
@State var operated:Bool = false
var body: some View{
VStack{
HStack{
Button(action: {
number = "0"
lastNumber = nil
lastOperation = .none
}, label: {
Text("AC").font(.largeTitle)
.frame(width:80)
.background(Circle().frame(width:80,height: 80).foregroundColor(.gray))
})
Spacer()
Button(action: {
lastNumber = "-1"
lastOperation = .multiplication
cal(operation: .none)
}, label: {
Text("+/-").font(.largeTitle)
.frame(width:80)
.background(Circle().frame(width:80,height: 80).foregroundColor(.gray))
})
Spacer()
Button(action: {
lastNumber = number
number = "100"
lastOperation = .division
cal(operation: .none)
}, label: {
Text("%").font(.largeTitle)
.frame(width:80)
.background(Circle().frame(width:80,height: 80).foregroundColor(.gray))
})
Spacer()
Button(action: {
cal(operation: .division)
}, label: {
Text("÷").font(.largeTitle)
.frame(width:80)
.background(Circle().frame(width:80,height: 80).foregroundColor(.gray))
})
}.frame(height:80)
HStack{
Button(action: {
input(num: "7")
}, label: {
Text("7").font(.largeTitle)
.frame(width:80)
.background(Circle().frame(width:80,height: 80).foregroundColor(.gray))
})
Spacer()
Button(action: {
input(num: "8")
}, label: {
Text("8").font(.largeTitle)
.frame(width:80)
.background(Circle().frame(width:80,height: 80).foregroundColor(.gray))
})
Spacer()
Button(action: {
input(num: "9")
}, label: {
Text("9").font(.largeTitle)
.frame(width:80)
.background(Circle().frame(width:80,height: 80).foregroundColor(.gray))
})
Spacer()
Button(action: {
cal(operation: .multiplication)
}, label: {
Text("X").font(.largeTitle)
.frame(width:80)
.background(Circle().frame(width:80,height: 80).foregroundColor(.gray))
})
}.frame(height:80)
HStack{
Button(action: {
input(num: "4")
}, label: {
Text("4").font(.largeTitle)
.frame(width:80)
.background(Circle().frame(width:80,height: 80).foregroundColor(.gray))
})
Spacer()
Button(action: {
input(num: "5")
}, label: {
Text("5").font(.largeTitle)
.frame(width:80)
.background(Circle().frame(width:80,height: 80).foregroundColor(.gray))
})
Spacer()
Button(action: {
input(num: "6")
}, label: {
Text("6").font(.largeTitle)
.frame(width:80)
.background(Circle().frame(width:80,height: 80).foregroundColor(.gray))
})
Spacer()
Button(action: {
cal(operation: .minus)
}, label: {
Text("-").font(.largeTitle)
.frame(width:80)
.background(Circle().frame(width:80,height: 80).foregroundColor(.gray))
})
}.frame(height:80)
HStack{
Button(action: {
input(num: "1")
}, label: {
Text("1").font(.largeTitle)
.frame(width:80)
.background(Circle().frame(width:80,height: 80).foregroundColor(.gray))
})
Spacer()
Button(action: {
input(num: "2")
}, label: {
Text("2").font(.largeTitle)
.frame(width:80)
.background(Circle().frame(width:80,height: 80).foregroundColor(.gray))
})
Spacer()
Button(action: {
input(num: "3")
}, label: {
Text("3").font(.largeTitle)
.frame(width:80)
.background(Circle().frame(width:80,height: 80).foregroundColor(.gray))
})
Spacer()
Button(action: {
cal(operation: .add)
}, label: {
Text("+").font(.largeTitle)
.frame(width:80)
.background(Circle().frame(width:80,height: 80).foregroundColor(.gray))
})
}.frame(height:80)
HStack{
Button(action: {
if number.count > 1 {
number.remove(at: number.index(before: number.endIndex))
}else{
number = "0"
}
}, label: {
Text("del").font(.largeTitle)
.frame(width:80)
.background(Circle().frame(width:80,height: 80).foregroundColor(.gray))
})
Spacer()
Button(action: {
input(num: "0")
}, label: {
Text("0").font(.largeTitle)
.frame(width:80)
.background(Circle().frame(width:80,height: 80).foregroundColor(.gray))
})
Spacer()
Button(action: {
input(num: ".")
}, label: {
Text(".").font(.largeTitle)
.frame(width:80)
.background(Circle().frame(width:80,height: 80).foregroundColor(.gray))
})
Spacer()
Button(action: {
cal(operation: .none)
}, label: {
Text("=").font(.largeTitle)
.frame(width:80)
.background(Circle().frame(width:80,height: 80).foregroundColor(.gray))
})
}.frame(height:80)
}.foregroundColor(.white)
}
func input(num:String){
if number == "0" || operated {
number = num
operated = false
}else{
number = number + num
}
}
func cal(operation:operation){
if lastNumber == nil {
lastNumber = number
lastOperation = operation
}else{
switch lastOperation {
case .add:
number = ((lastNumber?.toDouble ?? 0 ) + number.toDouble).toString
case .minus:
number = ((lastNumber?.toDouble ?? 0 ) - number.toDouble).toString
case .multiplication:
number = ((lastNumber?.toDouble ?? 0 ) * number.toDouble).toString
case .division:
number = ((lastNumber?.toDouble ?? 0 ) / number.toDouble).toString
case .none: break
}
lastNumber = number
lastOperation = operation
}
operated = true
}
}
enum operation{
case add,minus,multiplication,division,none
}
extension String{
var toDouble:Double{
return Double(self) ?? 0
}
}
extension Double{
var toString:String{
if self - Double(Int(self)) == 0.0 {
return String(Int(self))
}else{
return String(self)
}
}
}