我在向计算器添加历史记录时遇到问题(lblHistory
标签)
我想将以前的输入存储在lblHistory : UILabel
但它所做的就是存储第一个数字及其操作。
是否有任何算法可以存储计算器的所有先前输入?
这是我的班级MyCalculator.swift
Class MyCalculator
//
// MyCalculator.swift
// Calculator
//
// Created by
// abcdefgh
//
import Foundation
class MyCalculator
{
var memory: Float = 0
var percentnum: Float = 0
var currentNum: Float = 0
var result: Float = 0
var operation: String = ""
var isOperation: Bool = false
var isRepeatOperation: Bool = false
var isNum: Bool = false
func operation (operation:String) -> Float
{
if operation == "+" {
result = result + currentNum
}
else if operation == "-" {
result = result - currentNum
}
else if operation == "*" {
result = result * currentNum
}
else if operation == "/" {
result = result / currentNum
}
return result
}
func percent(percentNum:Float) ->Float
{
let valpercent = percentNum * 0.01
result = result * valpercent
return result
}
func clearAll() {
result = 0
currentNum = 0
isNum = false
}
}
这是我的计算器的视图控制器。
查看控制器
/ ViewController.swift
// Calculator
//
// Created by
// abcdefgh
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//Start Calculator
//Label History: This is where the history is displayed
@IBOutlet weak var lblHistory: UILabel!
//Label Result: This is where the result is displayed
@IBOutlet weak var lblResult: UILabel!
//Initialization
let calc = MyCalculator() //call the class MyCalculator
//Function for Number Inputs
@IBAction func btnNumInput(sender: AnyObject)
{
let num = sender.currentTitle
if calc.isNum == false
{
lblResult.text = num
calc.isNum = true
}
else
{
lblResult.text = lblResult.text! + num!!
}
calc.isRepeatOperation = false
lblResult.adjustsFontSizeToFitWidth = true
}
//Function for Operations
@IBAction func btnOperations(sender: AnyObject)
{
calc.isNum = false
if calc.isOperation && !calc.isRepeatOperation
{
calc.currentNum = Float(lblResult.text!)!
if calc.operation == "+"
{
calc.result = calc.operation("+")
}
else if calc.operation == "-"
{
calc.result = calc.operation("-")
}
else if calc.operation == "*"
{
calc.result = calc.operation("*")
}
else if calc.operation == "/"
{
calc.result = calc.operation("/")
}
if calc.operation == "/" && calc.currentNum == 0
{
lblResult.text = "Cannot divide by zero"
lblResult.adjustsFontSizeToFitWidth = true
}
else
{
let stringresult = String(format:"%g" , calc.result)
lblResult.text = "\(stringresult)"
calc.isOperation = false
}
}
else
{
calc.result = Float(lblResult.text!)!
}
let strresult = String(format:"%g", calc.result)
calc.operation = sender.currentTitle!!
lblHistory.text = strresult + calc.operation
calc.isOperation = true
calc.isRepeatOperation = true
}
从这里的代码:
let strresult = String(format:"%g", calc.result)
calc.operation = sender.currentTitle!!
lblHistory.text = strresult + calc.operation
这应该存储历史标签的任何输入,但它只存储第一个输入及其运算符。
结果:
2+2 =4 //this is the input variables and operation
//the result is 4 and display on lblResult: UILabel
2+ // the "2+" is only stored in lblHistory: UILabel not the whole input itself