import SwiftUI
import Foundation
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(.largeTitle)
.padding()
Spacer()
TextField("Enter a number", text: $input, onCommit: {
calculateResult()
})
.font(.title)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
HStack {
VStack {
Button(action: {
appendInput("7")
}, label: {
Text("7")
.font(.title)
.frame(width: 60, height: 60)
.background(Color.gray)
.foregroundColor(.white)
.cornerRadius(30)
})
Button(action: {
appendInput("4")
}, label: {
Text("4")
.font(.title)
.frame(width: 60, height: 60)
.background(Color.gray)
.foregroundColor(.white)
.cornerRadius(30)
})
Button(action: {
appendInput("1")
}, label: {
Text("1")
.font(.title)
.frame(width: 60, height: 60)
.background(Color.gray)
.foregroundColor(.white)
.cornerRadius(30)
})
Button(action: {
appendInput("0")
}, label: {
Text("0")
.font(.title)
.frame(width: 60, height: 60)
.background(Color.gray)
.foregroundColor(.white)
.cornerRadius(30)
})
}
.padding()
VStack {
Button(action: {
appendInput("8")
}, label: {
Text("8")
.font(.title)
.frame(width: 60, height: 60)
.background(Color.gray)
.foregroundColor(.white)
.cornerRadius(30)
})
Button(action: {
appendInput("5")
}, label: {
Text("5")
.font(.title)
.frame(width: 60, height: 60)
.background(Color.gray)
.foregroundColor(.white)
.cornerRadius(30)
})
Button(action: {
appendInput("2")
}, label: {
Text("2")
.font(.title)
.frame(width: 60, height: 60)
.background(Color.gray)
.foregroundColor(.white)
.cornerRadius(30)
})
Button(action: {
appendInput(".")
}, label: {
Text(",")
.font(.title)
.frame(width: 60, height: 60)
.background(Color.gray)
.foregroundColor(.white)
.cornerRadius(30)
})
}
.padding()
VStack {
Button(action: {
appendInput("9")
}, label: {
Text("9")
.font(.title)
.frame(width: 60, height: 60)
.background(Color.gray)
.foregroundColor(.white)
.cornerRadius(30)
})
Button(action: {
appendInput("6")
}, label: {
Text("6")
.font(.title)
.frame(width: 60, height: 60)
.background(Color.gray)
.foregroundColor(.white)
.cornerRadius(30)
})
Button(action: {
appendInput("3")
}, label: {
Text("3")
.font(.title)
.frame(width: 60, height: 60)
.background(Color.gray)
.foregroundColor(.white)
.cornerRadius(30)
})
Button(action: {
deleteLastInput()
}, label: {
Image(systemName: "delete.left")
.font(.title)
.frame(width: 60, height: 60)
.background(Color.gray)
.foregroundColor(.white)
.cornerRadius(30)
})
}
.padding()
VStack {
Button(action: {
appendInput("+")
}, label: {
Text("+")
.font(.title)
.frame(width: 60, height: 60)
.background(Color.orange)
.foregroundColor(.white)
.cornerRadius(30)
})
Button(action: {
appendInput("-")
}, label: {
Text("-")
.font(.title)
.frame(width: 60, height: 60)
.background(Color.orange)
.foregroundColor(.white)
.cornerRadius(30)
})
Button(action: {
appendInput("*")
}, label: {
Text("×")
.font(.title)
.frame(width: 60, height: 60)
.background(Color.orange)
.foregroundColor(.white)
.cornerRadius(30)
})
Button(action: {
appendInput("/")
}, label: {
Text("÷")
.font(.title)
.frame(width: 60, height: 60)
.background(Color.orange)
.foregroundColor(.white)
.cornerRadius(30)
})
}
.padding()
}
HStack {
VStack {
Button(action: {
appendInput("(")
}, label: {
Text("(")
.font(.title)
.frame(width: 60, height: 60)
.background(Color.orange)
.foregroundColor(.white)
.cornerRadius(30)
})
Button(action: {
appendInput("√(")
}, label: {
Text("√")
.font(.title)
.frame(width: 60, height: 60)
.background(Color.orange)
.foregroundColor(.white)
.cornerRadius(30)
})
}
.padding()
VStack {
Button(action: {
appendInput(")")
}, label: {
Text(")")
.font(.title)
.frame(width: 60, height: 60)
.background(Color.orange)
.foregroundColor(.white)
.cornerRadius(30)
})
Button(action: {
appendInput("^")
}, label: {
Text("^")
.font(.title)
.frame(width: 60, height: 60)
.background(Color.orange)
.foregroundColor(.white)
.cornerRadius(30)
})
}
.padding()
VStack {
Button(action: {
clearInput()
}, label: {
Text("Clear")
.font(.title)
.frame(width: 160, height: 60)
.background(Color.red)
.foregroundColor(.white)
.cornerRadius(30)
})
Button(action: {
calculateResult()
}, label: {
Text("=")
.font(.title)
.frame(width: 160, height: 60)
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(30)
})
}
.padding()
}
Text("Result: \(formattedResult)")
.font(.title)
.padding()
Spacer()
}
.padding()
}
func appendInput(_ character: String) {
input += character
}
func calculateResult() {
var formattedInput = input.replacingOccurrences(of: "√(", with: "sqrt(")
formattedInput = formattedInput.replacingOccurrences(of: "^", with: "**")
let expression = NSExpression(format: formattedInput)
if let calculatedResult = expression.expressionValue(with: nil, context: nil) as? Double {
result = calculatedResult
input = formattedResult
} else {
result = 0
}
}
func clearInput() {
input = ""
result = 0
currentOperation = .none
}
func deleteLastInput() {
if !input.isEmpty {
input.removeLast()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}