實作按下 0 以外的數字要怎麼處理

This commit is contained in:
2020-05-06 06:33:39 +08:00
parent e7f861c77d
commit 4bbf187cd2

View File

@@ -37,8 +37,18 @@ class Calculator {
formula.append(formula.popLast()! + text)
}
case "1", "2", "3", "4", "5", "6", "7", "8", "9":
if formula.last == "0" {
formula.removeLast()
formula.append(text)
} else if ["+", "-", "×", "÷"].firstIndex(of: formula.last!) != nil {
formula.append(text)
} else {
formula.append(formula.popLast()! + text)
}
default:
formula.append(text)
print(formula)
}
}
}