实现一个函数,计算一个字符串的值,该字符串中只有+ - * /四种运算符, 没有括号。 //参数mathString:要计算的字符串; //返回值:把计算出来的结果返回 例如:传入:@"1+2-1
来源:互联网 发布:品茗软件是什么意思 编辑:程序博客网 时间:2024/05/19 09:17
- (NSString *)calcString:(NSString *)mathString
{
int sum=0;
NSMutableString *strOperator=[[NSMutableStringalloc] init];
NSMutableString *strOperand=[[NSMutableStringalloc] init];
NSMutableArray *arrOperand=[mathStringcomponentsSeparatedByCharactersInSet:[NSCharacterSetcharacterSetWithCharactersInString:@"+-/*"]];
NSMutableArray *arrOperator=[mathStringcomponentsSeparatedByCharactersInSet:[NSCharacterSetcharacterSetWithCharactersInString:@"0123456789 "]];
int f1=(int)[mathStringcharacterAtIndex:0];
if (f1>48&&f1<57) {//第一个字母是数字
[arrOperatorremoveObjectAtIndex:0];
[arrOperatorremoveLastObject];
}else
{//第一个字母是运算符
[arrOperandremoveObjectAtIndex:0];
[arrOperand insertObject:[NSStringstringWithFormat:@"0"]atIndex:0];
[arrOperatorremoveLastObject];
}
for(int i=0;i<[arrOperatorcount];i++)
{
if ([[arrOperator objectAtIndex:i] compare:@""]==NSOrderedSame) {
[arrOperatorremoveObjectAtIndex:i];
i=-1;
}
}
// NSLog(@"%@",arrOperand);
// NSLog(@"%@",arrOperator);
while ([arrOperator count]) {
for (int i=0; i<[arrOperatorcount]; i++) {
//先运算符中的*、/
if ([[arrOperator objectAtIndex:i] characterAtIndex:0]=='*') {
//计算运算符'*'两边的数字
int count=
[[arrOperandobjectAtIndex:i] intValue]*([[arrOperandobjectAtIndex:i+1]intValue]);
// NSLog(@"%d-%d-%d",[[arrOperand objectAtIndex:i] intValue],[[arrOperand objectAtIndex:i+1] intValue],i);
NSRange range={i,2};
[arrOperandremoveObjectsInRange:range];
[arrOperandinsertObject:[NSStringstringWithFormat:@"%d",count]atIndex:i];
[arrOperatorremoveObjectAtIndex:i];
// NSLog(@"%@",arrOperand);
// NSLog(@"%@",arrOperator);
i=-1;//从头检测
}elseif([[arrOperator objectAtIndex:i]characterAtIndex:0]=='/')
{
int count=
([[arrOperandobjectAtIndex:i] intValue])/([[arrOperandobjectAtIndex:i+1]intValue]);
//计算运算符'/'两边的数字
NSRange range={i,2};
[arrOperandremoveObjectsInRange:range];
[arrOperandinsertObject:[NSStringstringWithFormat:@"%d",count]atIndex:i];
[arrOperatorremoveObjectAtIndex:i];
i=-1;//从头检测
}
}
for (int i=0; i<[arrOperatorcount]; i++) {
if([[arrOperator objectAtIndex:i]characterAtIndex:0]=='-')
{
//计算运算符'-'两边的数字
int count=
([[arrOperandobjectAtIndex:i] intValue])-([[arrOperandobjectAtIndex:i+1]intValue]);
NSRange range={i,2};
[arrOperandremoveObjectsInRange:range];
[arrOperandinsertObject:[NSStringstringWithFormat:@"%d",count]atIndex:i];
[arrOperatorremoveObjectAtIndex:i];
i=-1;
}elseif ([[arrOperator objectAtIndex:i] characterAtIndex:0]=='+') {
//计算运算符'+'两边的数字
int count=
([[arrOperandobjectAtIndex:i] intValue])+([[arrOperandobjectAtIndex:i+1]intValue]);
NSRange range={i,2};
[arrOperandremoveObjectsInRange:range];
[arrOperandinsertObject:[NSStringstringWithFormat:@"%d",count]atIndex:i];
[arrOperatorremoveObjectAtIndex:i];
i=-1;
}
}
}
return [arrOperand objectAtIndex:0];
}
- 实现一个函数,计算一个字符串的值,该字符串中只有+ - * /四种运算符, 没有括号。 //参数mathString:要计算的字符串; //返回值:把计算出来的结果返回 例如:传入:@"1+2-1
- 函数 Swift 使用函数多个返回值的特点完成一个count函数,该函数用于计算一个字符串中元音、辅音以及其他字母的个数。
- 重写sum函数,“无视”参数里的字符串并返回正确的计算结果
- 给定一个表示分数加减表达式的字符串,需要以字符串格式返回计算结果。结果表达式也是分数式
- 取得一个字符串表达式的计算结果(实现方法一)
- 取得一个字符串表达式的计算结果(实现方法二)
- javascript 计算一个字符串表达式的值
- 计算一个字符串的MD5
- 编写一个函数,实现从一个字符串中,查找另一个字符串的位置(笔试题) 例如int func("12345", "34")返回值为2,即在2号位置找到字符串“34”。
- 计算一个字符串中包含某个字符串的个数
- 计算一个字符串中包含另一个字符串的个数
- 计算字符串数学公式的结果
- c#字符串中包含的运算符按正常计算 例如按四则运算等,类似公式计算,很好很强大
- c#字符串中包含的运算符按正常计算 例如按四则运算等,类似公式计算,很好很强大
- sql语句 设计一个函数用来计算一个字符串中,另一个字符串出现的次数
- 统计一个长度为2的子字符串在另一个字符串中出现的次数.例如:假定输入的字符串为“asd asasdfg asd as zx67 asd mklo”,子字符串为“as”,函数返回值为6。
- 计算一个字符串中最大的子串的长度,并将它返回。如“abc12345sdfsasdfsd”最大的子串是sdfsasdfsd
- 输入一个只包含个位数字的简单四则表达式字符串,计算该表达式的值
- JQuery源码浅析: Callbacks
- Ubuntu VIM安装Ctags
- 我要开微店能否成功
- C#中静态与非静态方法比较
- 周赛3题目
- 实现一个函数,计算一个字符串的值,该字符串中只有+ - * /四种运算符, 没有括号。 //参数mathString:要计算的字符串; //返回值:把计算出来的结果返回 例如:传入:@"1+2-1
- 结构型模式之代理模式
- python是一门动态语言
- (转载)在C++程序中使用pbrt进行渲染
- SAP成本收集器
- swift 中,protocol的optional方法
- POJ 3414 Pots
- groovy gsp g标签
- 机房收费系统之结账