如何从NSExpression的expressionValueWithObject:上下文方法获得浮点数? [英] How to get floating point number from the NSExpression's expressionValueWithObject:context method?
问题描述
我已经实现了一个自定义计算器,我正在使用下面的代码来评估类似5 + 3 * 5-3的算术表达式。
< (NSNumber *)evaluateArithmeticStringExpression:(NSString *)表达式{
NSNumber * calculatedResult = nil;
@try {
NSPredicate * parsed = [NSPredicate predicateWithFormat:[expression stringByAppendingString:@= 0]];
NSExpression * left = [(NSComparisonPredicate *)parsed leftExpression];
calculatedResult = [left expressionValueWithObject:nil context:nil];
@catch(NSException * exception){
NSLog(@Input is not a expression ...!);
}
@finally {
return calculatedResult;
$ b但是当我使用除法运算的整数时结果只得到整数。让我们说5/2我得到2结果。由于整数除法对编程的撼动是正确的。
$ b
但是我需要浮点结果。
<我怎样才能得到它,而不是扫描表达式字符串,并将整数除数替换为浮点数。在我们的例子5 / 2.0或5.0 / 2中。
解决方案我自己找到了。 (NSNumber *)evaluateArithmeticStringExpression:(NSString *)表达式{
NSNumber * calculatedResult = nil;
pre $
@try {
NSPredicate * parsed = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@1.0 *%@ = 0,expression]];
NSExpression * left = [(NSComparisonPredicate *)parsed leftExpression];
calculatedResult = [left expressionValueWithObject:nil context:nil];
@catch(NSException * exception){
NSLog(@Input is not a expression ...!);
}
@finally {
return calculatedResult;
$ b $ p
$ b $ p <
NSPredicate * parsed = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@1.0 * %@ = 0,expression]];
注意:感谢@Martin R,但是我的问题不是关于整数除法,而是完全关于NSExpression 。我已经明确地排除了我的最后一句话。
@Zaph,这里使用异常处理是有很强的理由的。这是一个地方,我的方法是接受用户输入,我们的用户可以输入类似w * g和 - expressionValueWithObject:context:将抛出一个异常,我必须避免我的应用程序异常终止。如果用户输入了一个有效的表达式,那么他/她将以NSNumber的形式得到一个答案,否则就是一个零的NSNumber对象。I have already implemented a custom calculator where I am using following code to evaluate the arithmetic expression something like 5+3*5-3.
- (NSNumber *)evaluateArithmeticStringExpression:(NSString *)expression { NSNumber *calculatedResult = nil; @try { NSPredicate * parsed = [NSPredicate predicateWithFormat:[expression stringByAppendingString:@" = 0"]]; NSExpression * left = [(NSComparisonPredicate *)parsed leftExpression]; calculatedResult = [left expressionValueWithObject:nil context:nil]; } @catch (NSException *exception) { NSLog(@"Input is not an expression...!"); } @finally { return calculatedResult; } }
But when I am having integers with division operation I am getting only integer as a result. Let's say for 5/2 I am getting 2 as a result. It's right for the shake of programming because of the integer division.
But I need floating point result.
How can I get it rather scanning the expression string and replace the integer divisor as a floating point. In our example 5/2.0 or 5.0/2.
解决方案I found it by myself.
- (NSNumber *)evaluateArithmeticStringExpression:(NSString *)expression { NSNumber *calculatedResult = nil; @try { NSPredicate * parsed = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"1.0 * %@ = 0", expression]]; NSExpression * left = [(NSComparisonPredicate *)parsed leftExpression]; calculatedResult = [left expressionValueWithObject:nil context:nil]; } @catch (NSException *exception) { NSLog(@"Input is not an expression...!"); } @finally { return calculatedResult; } }
it's simply start the expression with the operand "1.0 * " and everything will be floating point calculation onwards.
NSPredicate * parsed = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"1.0 * %@ = 0", expression]];
NB: Thanks @Martin R but, my question wasn't about integer division, it's totally about NSExpression. I have clearly excluded with my last sentence.
@Zaph, Exception handling is used here for a strong reasons. It is a place where my method is accepting user input wehre user can enter something like w*g and - expressionValueWithObject: context: will throw an exception and I have to avoid the abnormal termination of my App. If the user entered a valid expression then he/she will get an answer in the form of NSNumber otherwise a nil NSNumber object.
这篇关于如何从NSExpression的expressionValueWithObject:上下文方法获得浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!