2012-08-27 40 views
-2

我正在嘗試製作一個計算器應用程序,並且有人爲基本操作(加法,減法...)提出了以下代碼;+/-和計算器應用程序中的根按鈕

  if ([operand isEqualToString:@"+"]) 
      lastKnownValue += currentValue; 

    else if ([operand isEqualToString:@"-"]) 
      lastKnownValue -= currentValue; 

    else if ([operand isEqualToString:@"X"]) 
      lastKnownValue *= currentValue; 

    else if ([operand isEqualToString:@"/"]) 

其中

  lastKnownValue 

用於保存輸入的號碼被用於操作數之前和

  currentValue 

在屏幕上顯示的lastKnownValue。
(操作是單獨的按鈕。)

它確實有效,但我將如何爲諸如+/-,根,方形等操作製作按鈕,而無需輸入第二個數字或點擊「 「EQUALS」按鈕?

+1

請給一點習俗背景下,以你的代碼。它是在一個按鈕上執行還是什麼?什麼是lastKnownValue和currentValue? – JeremyP

+0

我現在編輯它,謝謝你! –

回答

2

您對當前值執行操作,然後使用結果更新currentValue並刷新顯示。將lastknownValue設置爲零。

編輯:

self.currentvalue = sqrt(current value); 
self.lastknownValue = nil 
[self.view setNeedsDisplay]; 
+0

我更新了答案。 –