Untitled
unknown
plain_text
2 years ago
8.0 kB
5
Indexable
import SwiftUI enum Operation { case none, addition, subtraction, multiplication, division } struct ContentView: View { @State private var input: String = "" @State private var result: Double = 0 @State private var currentOperation: Operation = .none var formattedResult: String { return String(format: "%.2f", result) } var body: some View { VStack { Text("Calculator") .font(.title) .padding() TextField("Enter a number", text: $input) .textFieldStyle(RoundedBorderTextFieldStyle()) .padding() HStack { Button(action: { appendInput("7") }, label: { Text("7") .font(.title) .padding() .background(Color.gray) .foregroundColor(.white) .cornerRadius(10) }) Button(action: { appendInput("8") }, label: { Text("8") .font(.title) .padding() .background(Color.gray) .foregroundColor(.white) .cornerRadius(10) }) Button(action: { appendInput("9") }, label: { Text("9") .font(.title) .padding() .background(Color.gray) .foregroundColor(.white) .cornerRadius(10) }) Button(action: { performOperation(.division) }, label: { Text("/") .font(.title) .padding() .background(Color.orange) .foregroundColor(.white) .cornerRadius(10) }) } .padding() HStack { Button(action: { appendInput("4") }, label: { Text("4") .font(.title) .padding() .background(Color.gray) .foregroundColor(.white) .cornerRadius(10) }) Button(action: { appendInput("5") }, label: { Text("5") .font(.title) .padding() .background(Color.gray) .foregroundColor(.white) .cornerRadius(10) }) Button(action: { appendInput("6") }, label: { Text("6") .font(.title) .padding() .background(Color.gray) .foregroundColor(.white) .cornerRadius(10) }) Button(action: { performOperation(.multiplication) }, label: { Text("*") .font(.title) .padding() .background(Color.orange) .foregroundColor(.white) .cornerRadius(10) }) } .padding() HStack { Button(action: { appendInput("1") }, label: { Text("1") .font(.title) .padding() .background(Color.gray) .foregroundColor(.white) .cornerRadius(10) }) Button(action: { appendInput("2") }, label: { Text("2") .font(.title) .padding() .background(Color.gray) .foregroundColor(.white) .cornerRadius(10) }) Button(action: { appendInput("3") }, label: { Text("3") .font(.title) .padding() .background(Color.gray) .foregroundColor(.white) .cornerRadius(10) }) Button(action: { performOperation(.subtraction) }, label: { Text("-") .font(.title) .padding() .background(Color.orange) .foregroundColor(.white) .cornerRadius(10) }) } .padding() HStack { Button(action: { appendInput("0") }, label: { Text("0") .font(.title) .padding() .background(Color.gray) .foregroundColor(.white) .cornerRadius(10) }) Button(action: { calculateResult() }, label: { Text("=") .font(.title) .padding() .background(Color.blue) .foregroundColor(.white) .cornerRadius(10) }) Button(action: { performOperation(.addition) }, label: { Text("+") .font(.title) .padding() .background(Color.orange) .foregroundColor(.white) .cornerRadius(10) }) } .padding() Button(action: { clearInput() }, label: { Text("Clear") .font(.title) .padding() .background(Color.red) .foregroundColor(.white) .cornerRadius(10) }) .padding() Text("Result: \(formattedResult)") .font(.title) .padding() } .padding() } func appendInput(_ number: String) { input += number } func performOperation(_ operation: Operation) { if let number = Double(input) { switch currentOperation { case .none: result = number case .addition: result += number case .subtraction: result -= number case .multiplication: result *= number case .division: result /= number } } else { result = 0 } currentOperation = operation input = "" } func calculateResult() { performOperation(.none) } func clearInput() { input = "" result = 0 currentOperation = .none } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
Editor is loading...