用swiftUI写一个简单的计算器

用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)
        }
        
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值