2013-04-20 38 views
1

我想获得Objective C中的排列和组合。我发现两者都存在,但是它们在我的代码中存在一些问题,我不明白它在哪里出现。其实我想在文本框中打印这个值,但是它打印了我输入的最后一个值。我使用下面给出的代码。帮我请人..如何找到任何整数值的排列和组合?

typedef enum{Power,Exponent,nCr,nPr} CalcOperation; 
    NSString *storage; 
    CalcOperation operation; 
    -(IBAction)combination{ 
    operation = nPr; 
    storage = label.text; 
    [email protected]""; 
    label.text = @""; 
    } 

    -(int) factorial:(int)n{ 
    int d = n; 
    int temp = 1; 
    for(int b = 2; b <= d; b++){ 
    temp = temp*b; 
    } 
    //NSLog(@"the factorials is = %i",temp); 

    return temp; 
} 

-(IBAction)equalButton{ 
    float tempo = [storage floatValue]; 
    float b = [label.text floatValue]; 
    NSString *val = [[NSString alloc]initWithFormat:@"%f" , b]; 

    switch(operation) { 
    case nCr: 

    textField.text = [[NSString alloc]initWithFormat:@"%f",[self factorial:tempo]/([self factorial:[val floatValue]] * [self factorial:tempo-[val floatValue]])]; 
      //textField.text = [[NSString alloc]initWithFormat:@"%f",[val floatValue]+tempo+10]; 
      NSLog(@"combination %d",[self factorial:tempo]/([self factorial:[val floatValue]] * [self factorial:tempo-[val floatValue]])); 
      break; 
    case nPr: 

      textField.text = [[NSString alloc]initWithFormat:@"%f",[self factorial:tempo]/[self factorial:tempo-[val floatValue]]]; 
      NSLog(@"permutations %d",[self factorial:tempo]/([self factorial:tempo-[val floatValue]])); 
      break; ` 
        } 
       } 
+0

你试过调试?尝试设置断点并逐步执行代码。 – ColinE 2013-04-20 06:38:22

+0

是的,很多时候它采用阶乘方法并找到阶乘,但无法合并结果并打印出我在文本框中输入的最后一个值。 – 2013-04-20 06:42:50

+0

试着让'factorial:'方法返回一个'float'而不是'int'也许吧?此外,请尝试记录杜松子酒的每一个单独的值以及它们的组合,以确定您是否至少能够正确地获得这些值。 – msgambel 2013-04-20 07:01:10

回答

0

//计算nCr的,其中n =节奏,R = B

int tempo = [storage intValue]; 
int b = [label.text intValue]; 

float tempCb = (float)[self factorial:tempo]/([self factorial:b] * [self factorial:tempo-b])]; 

      break; 
+0

textField.text = [[NSString alloc] initWithFormat:@“%d”,[self factorial:tempo] /([factorial:[val floatValue]] * [self factorial:tempo- [val floatValue]])]; //你的方法也是正确的,但我已经改正了它只是在我的代码中将%f更改为%d – 2013-04-20 07:23:48

+0

您可以使用float来进行阶乘存储。但是面板是用于数字的,结果应该是一个数字。 – ssinganamalla 2013-04-21 14:17:41