转换为浮动和计算 [英] Convert to Float and Calculate
问题描述
我有一个包含多个数字和运算符的字符串
I have a string with multiple digits and operators
@ 5 + 4-9 / 10
。
如何从中得到结果?
我想在我使用的计算器中使用它。我必须在按下数字或运算符时动态显示结果。
I want to use it within the calculator i am using. I will have to display the result dynamically when a digit or operator is pressed.
我在 android 中使用了arity jar文件。但是我无法在 iPhone 中实现类似的功能。
I used arity jar file in android. But i am not able to achieve something like that in iPhone.
推荐答案
这个怎么样 -
NSString *formula = @"1+5*6";
NSExpression *exp = [NSExpression expressionWithFormat:formula];
NSNumber *resultForCustomFormula = [exp expressionValueWithObject:nil context:nil];
NSLog(@"%f", [resultForCustomFormula floatValue]);
编辑:
现在我考虑了你的要求并做了使用 NSScanner的方法
在 NSScanner stackoverflow.com/users/1155387/borrrden\">Mr。 Borrrden 建议我使用它,我发现它很棒。见下面的方法 -
EDIT :
Now I thought about your requirement and made a method using NSScanner
You will not believe I didn't use NSScanner
before Mr. Borrrden suggested me to use it and I found it awesome. See below method -
-(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 * .13 输入(2.222000 / 0.400000)+ 9999.000000-7.000000 + 0.700000 * 0.130000 。
在使用 NSExpression
之前调用此方法。
This will convert (2.222/.4)+9999-7+0.7*.13 in to (2.222000/0.400000)+9999.000000-7.000000+0.700000*0.130000.
Just call this method before using 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
注意:我并不是说它适用于所有公式字符串。可能在某些情况下不起作用。但它适用于一般方程式。
Note: I'm not saying that it will work in all formula strings. May be it will not work in some case. But it will work in general equations.
这篇关于转换为浮动和计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!