Untitled
unknown
plain_text
6 months ago
8.7 kB
2
Indexable
Never
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 { let formatter = NumberFormatter() formatter.maximumFractionDigits = result.truncatingRemainder(dividingBy: 1) == 0 ? 0 : 2 return formatter.string(from: NSNumber(value: 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: { appendInput("+") }, 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: { appendInput("-") }, 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: { appendInput("*") }, 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: { appendInput("/") }, 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(_ character: String) { input += character } 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() { 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 } input = String(result) // Die berechnete Zahl wird wieder in das Eingabefeld gesetzt } else { result = 0 } currentOperation = .none } func clearInput() { input = "" result = 0 currentOperation = .none } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }