我有一个有多个数字和运算符的字符串转换为浮点数并计算
@"5+4-9/10"
。
如何从中得到结果?
我想在我使用的计算器内使用它。当按下数字或操作符时,我将不得不动态显示结果。
我使用了arity jar文件android。但我不能在iPhone中实现类似的功能。
我有一个有多个数字和运算符的字符串转换为浮点数并计算
@"5+4-9/10"
。
如何从中得到结果?
我想在我使用的计算器内使用它。当按下数字或操作符时,我将不得不动态显示结果。
我使用了arity jar文件android。但我不能在iPhone中实现类似的功能。
这个什么 -
NSString *formula = @"1+5*6";
NSExpression *exp = [NSExpression expressionWithFormat:formula];
NSNumber *resultForCustomFormula = [exp expressionValueWithObject:nil context:nil];
NSLog(@"%f", [resultForCustomFormula floatValue]);
编辑: 现在我想到了你的要求,并提出使用NSScanner
的方法你不会相信我没有用之前NSScanner
建议Mr. Borrrden我使用它,我发现它很棒。请参阅下面的方法 -
-(NSMutableString *)formatString:(NSString *)formula
{
// Let's check if there any wrong (.) value exm: 1/.2 or .7+3
// 1/0.2 and 0.7+3 are okay but above are incorrect so first fix them
NSString *str = formula;
NSInteger c = 0;
for(int i=0; i<[str length]; i++)
{
if([[NSString stringWithFormat:@"%c",[str characterAtIndex:i]] isEqualToString:@"+"] ||
[[NSString stringWithFormat:@"%c",[str characterAtIndex:i]] isEqualToString:@"-"] ||
[[NSString stringWithFormat:@"%c",[str characterAtIndex:i]] isEqualToString:@"/"] ||
[[NSString stringWithFormat:@"%c",[str characterAtIndex:i]] isEqualToString:@"*"])
{
if([str length] > i+1)
{
if([[NSString stringWithFormat:@"%c",[str characterAtIndex:i+1]] isEqualToString:@"."])
{
formula = [formula stringByReplacingCharactersInRange:NSMakeRange(i+1+c, 1) withString:@"0."];
c++;
}
}
}
}
// Now we will convert all numbers in float
NSString *aString;
float aFloat;
NSMutableString *formattedString = [[NSMutableString alloc]init];
NSScanner *theScanner = [NSScanner scannerWithString:formula];
while ([theScanner isAtEnd] == NO)
{
if([theScanner scanFloat:&aFloat])
{
[formattedString appendString:[NSString stringWithFormat:@"%f",aFloat]];
}
if([theScanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:&aString])
{
[formattedString appendString:aString];
}
}
return formattedString;
}
这将(2.222 /。4)+ 9999-7 + 0.7 * 0.13在转换为(2.222000/0.400000)+ 9999.000000-7.000000 + 0.700000 * 0.130000。
只需在使用NSExpression
之前调用此方法即可。
NSString *formula = @"(2.222/.4)+9999-7+0.7*.13";
NSString *formattedString = [self formatString:formula];
NSExpression *exp = [NSExpression expressionWithFormat:formattedString];
NSNumber *resultForCustomFormula = [exp expressionValueWithObject:nil context:nil];
NSLog(@"Result = %f", [resultForCustomFormula floatValue]);
//OutPut: Result = 9997.646484
注:我不是说,将在所有的公式字符串的工作。可能在某些情况下它不起作用。但它将在一般方程中起作用。
我得到的地板价值。我试过7/4和它的返回1.0000 –
是啊,因为7和4是整数,所以你肯定会得到1.0000尝试它与浮动像7.0/4.0然后看:) – TheTiger
是否有方法来验证表达式,因为我想动态地显示结果 –
Note that :
您必须通过至少一个浮点值。
我知道这不是最好的方式,但是像这样的东西可以为你工作。
NSString *formula = @"5+4-9/10";
NSString *str = [formula lastPathComponent];
formula = [formula stringByReplacingOccurrencesOfString:str withString:[NSString stringWithFormat:@"%@.0",str]];
NSString *strCal=[NSString stringWithString:formula];
NSExpression *exp=[NSExpression expressionWithFormat:strCal];
float result=[[exp expressionValueWithObject:nil context:nil] floatValue];
NSLog(@"result:%f",result);
您可以使用GCMathParser或DDMathParser。
不知道这是否是最有效的方法,但写过无论如何帮助你...... !!!
您不需要虚拟“NSPredicate”,请参阅http://stackoverflow.com/a/12500518/1187415。 –
我收到警告“NSPredicate可能不会回应左表达式” –
雅@MartinR,我只是编辑我的问题 – Bhavin
你可以解析 “全部” 数字飘起:
NSMutableString *numericExpression = [NSMutableString stringWithString:@"4+3/2-7/4"];
NSMutableString *nExp=[NSMutableString stringWithString:@""];
BOOL innum=false;
BOOL dotfound=false;
for (NSInteger charIdx=0; charIdx<numericExpression.length; charIdx++){
NSString *substr=[NSString stringWithFormat:@"%C",[numericExpression characterAtIndex:charIdx]];
if (isdigit([numericExpression characterAtIndex:charIdx])) {
// in number
innum=true;
}else if ([substr isEqualToString:@"."]){
// dot found ...
innum=true;
dotfound=true;
}else{
// not in number
if (innum && dotfound) {
NSLog(@"all good (*).*");
}else if(innum && !dotfound){
NSLog(@"adding .0");
[nExp appendString:@".0"];
}
innum=false;
dotfound=false;
}
[nExp appendString:substr];
NSLog(@"%@",nExp);
}
NSExpression *expression = [NSExpression expressionWithFormat:nExp];
NSNumber *result = [expression expressionValueWithObject:nil context:nil];
NSLog(@"calc %@ = %@",nExp,result);
您的解决方案将是 [这里](https://github.com/davedelong/DDMathParser) – Sumanth
当您选择号码,必须将数字从nsstring转换为int或float,如你所愿.. –
对于简单的表达式,你可以使用'NSExpression',参见eg http://stackoverflow.com/a/12500518/1187415。否则,请使用适当的数学表达式解析器。 –