只是要警告你,我对迅捷是全新的,我仍然习惯于这是如何运作的。我一直在努力研究这个计算器项目。问题是,我需要练习简化代码。
现在,当按下数字按钮时,我将它保存在这样的数组中。
@IBAction func buttonPressed(_ sender: UIButton) {
if numBtnPressed == true {
numArray.append(sender.title(for: .normal)!)
}
}
所以如果按下标有1的按钮,它会将[“1”]作为字符串存储在空数组numArray中
我需要一个单独的操作符结构。
struct calcFuncs {
var add : String
var subtract : String
var divide : String
var multiply : String
}
var calc = calcFuncs(add: "+", subtract: "-", divide: "/", multiply: "x")
我正在试图找出如何使用一串数字和运算符(例如1 + 2 x 3 + 4/5 + 5)并让编译器保存该计算,然后在按下相等时打印它。我知道我必须找到一种方法来完成计算,即使现在一切都在字符串中。我希望它能在标签上打印完成的字符串。
我坚持如何从数组中提取项目,并根据点击的操作符显示操作符。 (“[1](操作员)[2](操作员)[3](操作员)”)并打印一笔金额。
我在想这样的事情,但我知道它很乏味,可能不会起作用:
for 0...1 in index:
[0] something [1]
1...2 in index:
etc etc
我知道这听起来很疯狂,但我正在努力告诉电脑我想要什么(按下这些按钮时计算一串数字,保存总数,然后点击相等时打印总数)并将其置于清洁状态 - 切码。
任何建议,即使这意味着更改我的所有代码,都表示赞赏。
答案 0 :(得分:1)
你有一些好主意,但也许有一种更简单的方法。此示例使用您可以在Interface Builder中设置的按钮的tag属性,以及NSExpression
类来评估数学。还有很多其他方法可以做到。
这将遵循标准的操作顺序(例如,加法和减法之前的乘法和除法)。
import UIKit
var calculationString = "" // Declare this outside method in view controller
@IBAction func buttonPressed(_ sender: UIButton) {
// Use the tag property of buttons which can be set in Interface Builder
// Number buttons can have tag values 0-9
// Operator buttons can have higher tag numbers (10: "+", 11: "-", 12: "*" 13: "/")
// Decimal point button tag (14)
// Calculate or equals button tag (15)
// Check for tag number
switch sender.tag {
case 0...9:
// Numbers
calculationString.append(String(sender.tag))
case 10:
// Add
calculationString.append("+")
case 11:
// Subtract
calculationString.append("-")
case 12:
// Multiply
calculationString.append("*") // Need to use asterisk for multiply
case 13:
// Divide
calculationString.append("/")
case 14:
// Decimal
calculationString.append(".")
case 15:
// Perform calculation
let expression = NSExpression(format: calculationString)
let result = expression.expressionValue(with: nil, context: nil) ?? 0
yourLabel.text = "Result: \(result)" // Or do whatever you need with the result.
default:
fatalError("Unknown button tag")
}
yourLabel.text = calculationString // Display user input if desired
}
答案 1 :(得分:0)
快速计算器4
class ViewController: UIViewController {
var numberOnScreen: Double = 0;
var previousNumber: Double = 0;
var performingMath = false;
var operation = 0;
@IBOutlet weak var label1: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func btn_pressed(_ sender: Any) {
if performingMath == true {
label1.text = String((sender as AnyObject).tag-1)
numberOnScreen = Double(label1.text!)!
performingMath = false
}
else {
label1.text = label1.text! + String((sender as AnyObject).tag-1)
numberOnScreen = Double(label1.text!)!
}
}
@IBAction func processbtn(_ sender: Any) {
if label1.text != "" && (sender as AnyObject).tag != 11 && (sender as AnyObject).tag != 16{
// 11= AC (clear) , 16 = equalto(=)
// Use tag numbers correctly
previousNumber = Double(label1.text!)!
if (sender as AnyObject).tag == 12 { //Divide
label1.text = "/";
}
if (sender as AnyObject).tag == 13 { //Multiply
label1.text = "x";
}
if (sender as AnyObject).tag == 14 { //Subtract
label1.text = "-";
}
if (sender as AnyObject).tag == 15 { //Add
label1.text = "+";
}
operation = (sender as AnyObject).tag
performingMath = true;
}
else if (sender as AnyObject).tag == 16 {
if operation == 12{ //Divide
label1.text = String(previousNumber / numberOnScreen)
}
else if operation == 13{ //Multiply
label1.text = String(previousNumber * numberOnScreen)
}
else if operation == 14{ //Subtract
label1.text = String(previousNumber - numberOnScreen)
}
else if operation == 15{ //Add
label1.text = String(previousNumber + numberOnScreen)
}
}
else if (sender as AnyObject).tag == 11{
label1.text = ""
previousNumber = 0;
numberOnScreen = 0;
operation = 0;
}
}
}