Finish PoC calculator

This commit is contained in:
TheAlgorithm476 2025-05-11 15:48:24 +02:00
parent 5901b5f4c9
commit 17020fcab4
3 changed files with 143 additions and 22 deletions

View File

@ -0,0 +1,8 @@
enum Operand {
case add
case sub
case mul
case div
case pow
case sqrt
}

View File

@ -7,85 +7,125 @@ import CMath
@main
struct PMCalc: App {
let app = AdwaitaApp(id: "me.thealgorithm476.PMCalc.PMCalc")
@State
var vm = ViewModel()
var scene: Scene {
Window(id: "main") { window in
VStack(spacing: 8) {
Text("Calculations here...")
Text(vm.display)
.selectable(false)
.title2()
.halign(.end)
VStack(spacing: 8) {
HStack(spacing: 8) {
Button("AC") {}
Button("AC") {
vm.clear()
}
.keyboardShortcut("c", app: app)
.hexpand()
.destructive()
Button("") {}
Button("^") {
vm.operation(.pow)
}
.hexpand()
.flat()
Button("%") {}
.keyboardShortcut("percent", app: app)
.hexpand()
Button("/") {}
Button("/") {
vm.operation(.div)
}
.keyboardShortcut("slash", app: app)
.hexpand()
}
.vexpand()
HStack(spacing: 8) {
Button("7") {}
Button("7") {
vm.addNumber(7)
}
.keyboardShortcut("7", app: app)
.hexpand()
Button("8") {}
Button("8") {
vm.addNumber(8)
}
.keyboardShortcut("8", app: app)
.hexpand()
Button("9") {}
Button("9") {
vm.addNumber(9)
}
.keyboardShortcut("9", app: app)
.hexpand()
Button("*") {}
Button("*") {
vm.operation(.mul)
}
.keyboardShortcut("asterisk", app: app)
.hexpand()
}
.vexpand()
HStack(spacing: 8) {
Button("4") {}
Button("4") {
vm.addNumber(4)
}
.keyboardShortcut("4", app: app)
.hexpand()
Button("5") {}
Button("5") {
vm.addNumber(5)
}
.keyboardShortcut("5", app: app)
.hexpand()
Button("6") {}
Button("6") {
vm.addNumber(6)
}
.keyboardShortcut("6", app: app)
.hexpand()
Button("-") {}
Button("-") {
vm.operation(.sub)
}
.keyboardShortcut("minus", app: app)
.hexpand()
}
.vexpand()
HStack(spacing: 8) {
Button("1") {}
Button("1") {
vm.addNumber(1)
}
.keyboardShortcut("1", app: app)
.hexpand()
Button("2") {}
Button("2") {
vm.addNumber(2)
}
.keyboardShortcut("2", app: app)
.hexpand()
Button("3") {}
Button("3") {
vm.addNumber(3)
}
.keyboardShortcut("3", app: app)
.hexpand()
Button("+") {}
Button("+") {
vm.operation(.add)
}
.keyboardShortcut("plus", app: app)
.hexpand()
}
.vexpand()
HStack(spacing: 8) {
Button("0") {}
Button("0") {
vm.addNumber(0)
}
.keyboardShortcut("0", app: app)
.hexpand()
Button("") {}
Button("sq") {
vm.operation(.sqrt)
}
.hexpand()
.flat()
Button(",") {}
Button(",") {
vm.addComma()
}
.keyboardShortcut("comma", app: app)
.hexpand()
Button("=") {}
Button("=") {
vm.calculate()
}
.keyboardShortcut("equal", app: app)
.hexpand()
}

View File

@ -0,0 +1,73 @@
import Adwaita
import CMath
struct ViewModel {
private var numberBuffer: String = ""
private var number1: Double? = nil
private var number2: Double? = nil
private var operand: Operand? = nil
@State
private(set) var display: String = ""
mutating func clear() {
numberBuffer = ""
number1 = nil
number2 = nil
operand = nil
display = ""
}
mutating func addComma() {
guard !numberBuffer.isEmpty else { return }
numberBuffer.append(",")
display = numberBuffer
}
mutating func addNumber(_ number: UInt8) {
if numberBuffer.isEmpty { display = "" } // Reset display after new number is added to cleared buffer
numberBuffer.append(String(number))
display = numberBuffer
}
mutating func operation(_ operand: Operand) {
guard !numberBuffer.isEmpty && number1 == nil else { return }
self.operand = operand
if number1 == nil { number1 = Double(numberBuffer) }
else { number2 = Double(numberBuffer) }
numberBuffer = ""
if operand == .sqrt {
display = String(sqrt(number1!))
number1 = nil
self.operand = nil
}
}
mutating func calculate() {
number2 = Double(numberBuffer)
numberBuffer = ""
var result: Double = 0.0
switch operand {
case .add:
result = add_nums(number1!, number2!)
case .sub:
result = sub_nums(number1!, number2!)
case .mul:
result = mul_nums(number1!, number2!)
case .div:
result = div_nums(number1!, number2!)
case .pow:
result = pow(number1!, number2!)
case .sqrt, .none:
// do nothing
print("Attempting to apply Square Root to nothing!")
}
display = String(result)
}
}