我想要得到Objective C中的排列和组合。我已经发现了这两种情况,但我的代码中有一些问题,我不知道它发生在哪里。实际上,我想打印文本字段中的值,但它打印的是我输入的最后一个值。我使用的代码如下所示。请帮帮我..
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; `
}
}发布于 2013-04-20 15:12:50
//计算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;https://stackoverflow.com/questions/16117473
复制相似问题