如何简化我的程序和所有if语句?

时间:2013-02-16 02:53:47

标签: iphone ios objective-c if-statement ios6

我正在创建一个十六进制,十进制和二进制转换器,到目前为止它还顺利。这是我在iPhone上的第二个项目,我是初学者。但是,我想知道如何简化我所拥有的(一堆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 

这对我来说非常重复。所有这些都在一个类中,所有这些if语句彼此非常相似,所以我想知道我能做些什么吗?

感谢您的时间。

7 个答案:

答案 0 :(得分: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”的行设置为不同的方法。

答案 1 :(得分: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 
}

答案 2 :(得分: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
}

答案 3 :(得分:1)

您提到的if语句不相同。

说,按下二进制按钮时,要转换二进制数,你需要两个不同的功能。

  1. 十六进制为二进制(输入为十六进制)
  2. 十进制到二进制(输入是十进制)
  3. 所以,实际上你正在调用不同的函数。

答案 4 :(得分:1)

正如@apurv暗示的那样,你的决定是独一无二的(输入+点击),你真的不能做太多简化或压缩它(好像你有某种重复模式)。你能做的最好就是尽可能让它变得可读,你拥有的就是好的。理解起来很简单。这是任何“简化”或使其更优雅的尝试都可能使其变得不必要地复杂和可读性差的情况之一。

答案 5 :(得分: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>
}

答案 6 :(得分:0)

为什么不使用switch语句以提高可读性?