4

我正在创建一个十六进制、十进制和二进制转换器,到目前为止进展顺利。这是我在 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 语句都非常相似,所以我想知道是否有什么我可以做的?

谢谢你的时间。

4

7 回答 7

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”行的方法都不同。

于 2013-02-16T04:27:41.773 回答
1

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

于 2013-02-16T04:43:34.863 回答
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 
}
于 2013-02-16T03:36:48.823 回答
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>
}
于 2013-02-17T13:31:54.187 回答
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
}
于 2013-02-16T03:48:06.540 回答
1

您提到的 if 语句不一样。

说,当按下二进制按钮时,要将数字转换为二进制,您将需要两个不同的功能。

  1. 十六进制转二进制(输入为十六进制)
  2. 十进制转二进制(输入为十进制)

因此,实际上您正在调用不同的函数。

于 2013-02-16T04:23:19.550 回答
0

为什么不使用 switch 语句以获得更好的可读性?

于 2013-02-16T03:18:02.420 回答