2013-02-16 15 views
4

我正在創建一個十六進制,十進制和二進制轉換器,目前進展順利。這是我在iPhone上的第二個項目,我是初學者。但是,我想知道如何簡化我的(一堆if語句)。我有:如何簡化我的程序和所有if語句?

if (entered is hex) 
    if (binary button clicked) 
     convert to binary 
    if (decimal button clicked) 
     convert to decimal 
    else (hex button clicked) 
     keep in hex and inform 
else if (entered is binary) 
    if (hex button clicked) 
     convert to hex 
    if (decimal button clicked) 
     convert to decimal 
    else (binary button clicked) 
     keep in binary and inform user 
else if (entered is decimal) 
    if (hex button clicked) 
     convert to binary 
    if (binary button clicked) 
     convert to hex 
    else (decimal button clicked) 
     keep in decimal and inform user  
else 
    give error if something else entered in 

這看起來很重複我。所有這些都屬於同一個班級,所有這些如果陳述彼此非常相似,所以我想知道我能做些什麼嗎?

謝謝你的時間。

+0

什麼是 「六角鍵點擊」? – user523234 2013-02-16 03:05:39

+0

你如何確定輸入是什麼類型?這是基於點擊按鈕還是哪個文本字段用於輸入?還有別的嗎? – rdelmar 2013-02-16 03:48:50

+0

你的代碼已經足夠好讀。不需要額外的代碼分解功能,但可以增加可讀性。 – 2013-02-16 04:47:45

回答

4

我會始終以相同的格式存儲輸入值(內部)(讓我們在這個例子中使用十六進制)。通過這樣寫它(用於檢查刪除錯誤

// Convert the user entered value to hex 
if (enteredValue is hex) 
    internalHexValue = enteredValue 
else if (entered is binary) 
    internalHexValue = convert enteredValue (binary) to hex 
else if (entered is decimal) 
    internalHexValue = convert enteredValue (decimal) to hex 
else 
    error and return 

// Now, you have far less repetition because you only have to convert from hex: 
if (binary button clicked) 
    convertedValue = convert internalHexValue to binary 
else if (decimal button clicked) 
    convertedValue = convert internalHexValue to decimal 
else (hex button clicked) 
    convertedValue = internalHexValue 

// Lastly, see if they selected the same format for input and output: 
if (enteredValue == convertedValue) 
    inform user 

你也可以打破上面的例子中爲多個方法,以使其更易於閱讀:然後,你可以使用這樣的事情,這是更爲流線型清晰度):

internalHexValue = [self convertEnteredValueToHex:enteredValue]; 
convertedValue = [self convertHexValueToUserSelectedFormat:internalHexValue]; 
if (enteredValue == convertedValue) 
    inform user 

我也會讓所有「將XXX轉換爲XXX」行的不同方法在您的類中。

+0

我會首先移動typingType == targetType檢查,但否則這是要走的路,如果使用常見的內部表單。代碼分解幾乎總是朝着正確的方向邁出的一步。 – 2013-02-16 14:00:39

+0

@HotLicks:我通常會首先檢查它,但是在這種情況下,它會花費幾乎相同的代碼來首先進行檢查,因爲它可以計算出值,所以這樣更快,更容易閱讀... – lnafziger 2013-02-16 17:48:59

+0

檢查指定的輸入和輸出類型是否相同應該幾乎是免費的。 – 2013-02-16 18:27:27

0

爲什麼不使用switch語句來提高可讀性?

1

使用開關,這是非常光滑的,不喜歡下面

switch (entered) 
{ 
case hex: 
    if (binary button clicked) 
     convert to binary 
    else if (decimal button clicked) 
     convert to decimal 
    else (hex button clicked) 
     keep in hex and inform 
break; 

case binary: 

    if (hex button clicked) 
     convert to hex 
    else if (decimal button clicked) 
     convert to decimal 
    else (binary button clicked) 
     keep in binary and inform user 
break; 

case decimal: 

    if (hex button clicked) 
     convert to binary 
    else if (binary button clicked) 
     convert to hex 
    else (decimal button clicked) 
     keep in decimal and inform user 
break; 

default: 

    give error if something else entered in 
} 
+1

這真的不是任何清潔工! – duskwuff 2013-02-16 03:44:19

+1

更不用說十進制/十六進制/ etc可能不是一個可以在switch語句中使用的值.... – lnafziger 2013-02-16 04:28:35

+0

@lnafziger - 十進制/十六進制/ etc指示可以轉換爲一個簡單的整數(儘管轉換可能很複雜)。而且你可以將in和out類型轉換爲整數,轉換一個,並添加,將整個事物簡化爲一個簡單的開關,而不需要內部的if語句。 – 2013-02-17 13:22:42

1

把它分解成幾個方法,下面是比較概念性的,不解決支架的不必要的飽食或缺乏:

if (entered is hex) 
     [self isHex]; 
    else if (entered is binary) 
     [self isBinary]; 
    else if (entered is decimal) 
     [self isDecimal]; 
     keep in decimal and inform user 
    else 
     give error if something else entered in 
    return 0; 
} 

- (void)isHex { 
    if (binary button clicked) 
     convert to binary 
    else if (decimal button clicked) 
     convert to decimal 
    else (hex button clicked) 
     keep in hex and inform 
} 

- (void)isBinary { 
    if (hex button clicked) 
     convert to hex 
    else if (decimal button clicked) 
     convert to decimal 
    else (binary button clicked) 
     keep in binary and inform user 
} 

- (void)isDecimal { 
    if (hex button clicked) 
     convert to binary 
    else if (binary button clicked) 
     convert to hex 
    else (decimal button clicked) 
     keep in decimal and inform user 
} 
1

您提到的if語句不相同。

說,按二進制按鈕時,要轉換二進制數,你需要兩個不同的功能。

  1. 六角二進制(輸入十六進制)
  2. 十進制二進制(輸入十進制)

因此,有效地你調用不同的功能。

+0

我不認爲這真的回答了他的問題.... – lnafziger 2013-02-16 05:17:37

1

正如@apurv所暗示的,你的決定是獨一無二的(輸入+點擊),你真的不能做太多的事情來簡化它或者壓低它(就像你有某種重複模式一樣)。你所能做的最好的事情就是儘可能讓它變得可讀,而你擁有的就好。這很容易理解。這是任何試圖「簡化」或使其更加優雅的嘗試可能會使其不必要地複雜和不太可讀。

1

替代(不一定是我的最愛):

int inputFmt = <input format reduced to integer 0..2> 
int outputFmt = <output format reduced to integer 0..2> 

int switchValue = (inputFmt * 4) + outputFmt; 

switch (switchValue) { 
    case BinaryFmtConst * 4 + BinaryFmtConst: 
     <convert binary -> binary> 
     break; 
    case BinaryFmtConst * 4 + DecimalFmtConst: 
     <convert binary -> decimal> 
     break; 
. . . 
    case DecimalFmtConst * 4 + BinaryFmtConst: 
     <convert decimal -> binary> 
     break; 
. . . 
    case HexFmtConst * 4 + HexFmtConst: 
     <convert hex -> hex> 
     break; 
    default: 
     <error message> 
}