将小数点按钮添加到我的计算器应用程序

时间:2017-11-02 00:58:23

标签: ios swift decimal calculator

编辑:

 @IBAction func decimal(_ sender: UIButton)
  {
      label.text = label.text! + String(decimal)
      numberOnScreen = Double(label.text!)!
      performingMath = true
      do {
      previousNumber = Double(self.label.text!)!

     }
   }

它仍然没有将十进制数添加到已经按下的数字,而是清除数字,只是将标签更改为十进制。它还没有正确执行数学

我想在计算器中添加一个小数点。

一旦我将按钮添加为动作,我该如何将其编码为小数点。 我希望它以相同的格式编码,iv编码其余的按钮。 我需要它按下后显示前一个数字,并使用小数执行数学运算,并显示带小数的Bool。

我是初学者,自学成才 我按照教程对此进行了编码,但教程没有说明如何添加小数

有人可以帮我编写十进制按钮。

@IBOutlet weak var label: UILabel!


@IBAction func numbers(_ sender: UIButton)
{
    if performingMath == true
    {
        label.text = String(sender.tag-1)
        numberOnScreen = Double(label.text!)!
        performingMath = false
    }
    else
    {

    label.text = label.text! + String(sender.tag-1)
    numberOnScreen = Double(label.text!)!
}
}


@IBAction func button(_ sender: UIButton)
{
    if label.text != "" && sender.tag != 11 && sender.tag != 16
   {
    previousNumber = Double(label.text!)!

      if sender.tag == 12 //Divide
      {
        label.text = "/";
      }
    else if sender.tag == 13 //Multiply
      {
        label.text = "x";
      }
    else if sender.tag == 14 //Minus
      {
        label.text = "-";
      }
    else if sender.tag == 15 //Plus
      {
        label.text = "+";
      }

    operation = sender.tag
    performingMath = true;
}
   else if sender.tag == 16
{

if operation == 12
{
   label.text = String(previousNumber / numberOnScreen)
}


  else if operation == 13
{
   label.text = String(previousNumber * numberOnScreen)
}


  else if operation == 14
   {
   label.text = String(previousNumber - numberOnScreen)
   }


  else if operation == 15
   {
   label.text = String(previousNumber + numberOnScreen)
   }
    }
    else if sender.tag == 11
    {
        label.text = ""
        previousNumber = 0;
        numberOnScreen = 0;
        operation = 0;


}
}


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.
}


}

1 个答案:

答案 0 :(得分:0)

//将它加入到代码的底部,这是百分比按钮,注意我的百分比按钮上的标签号是18.记住你必须从故事板中的百分比按钮ctrl拖动到@IBAction func按钮(_ sender:UIButton)

    else if sender.tag == 11
            {
                label.text = ""
                previousNumber = 0;
                numberOnScreen = 0;
                operation = 0;
    }
            else if sender.tag == 18
            {
                if operation == 12
                {
                label.text = String(previousNumber / (previousNumber * (numberOnScreen / 100)))
                }
                else if operation == 13
                {
                    label.text = String(previousNumber * (previousNumber * (numberOnScreen / 100)))
                }
                else if operation == 14
                {
                    label.text = String(previousNumber - (previousNumber * (numberOnScreen / 100)))
                }
                else if operation == 15
                {
                    label.text = String(previousNumber + (previousNumber * (numberOnScreen / 100)))
                }
    }
}