1

我想在 Objective CI 中找到排列和组合,但它们在我的代码中存在一些问题,我不明白它发生在哪里。实际上我想在文本字段中打印值,但它会打印我输入的最后一个值。我使用了下面给出的代码。请任何人帮助我..

    typedef enum{Power,Exponent,nCr,nPr} CalcOperation;
    NSString *storage;
    CalcOperation operation;
   -(IBAction)combination{ 
    operation = nPr; 
    storage = label.text;
    textField.text=@"";
    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;  `
                  }
                }
4

1 回答 1

0

//计算 nCr 其中 n = tempo, 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;
于 2013-04-20T07:12:50.123 回答