2013-02-16 44 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> 
}