首页 > 代码库 > iOS 收款计算器算法

iOS 收款计算器算法

一个收款计算器算法,从之前高仿有赞Demo里面抽离的一个界面

demo 在这里 https://github.com/L-vinCent/calculView_function

  • 显示计算记录
  • 不能连续输入俩个计算符号
  • 小数点的位数不能超过俩位
  • 单个小数点的时候不能输入 00
  • 点击 + ,直接显示计算结果,正则匹配计算
  • 设置最大显示金额

 

- (IBAction)calculate:(UIButton *)sender {    UIButton *btn=(UIButton *)sender;    NSArray *firstArr=@[@"00",@"+",@".",@"",@"0"];  //首字母输入要忽略的字符    NSArray *LastArr=@[@"+",@"."];  //不同连续输入的字符    NSArray *zeroArr=@[@"+",@""];  // 在.00之后可以点击的字符    if (historyStr.length>=3) {     //x.00 之后不能再输入数字        NSString *dot=[historyStr substringWithRange:NSMakeRange(historyStr.length-3, 1)];        if ([dot isEqualToString:@"."]&&![zeroArr containsObject:btn.titleLabel.text]&&![[historyStr substringWithRange:NSMakeRange(historyStr.length-1, 1)] isEqualToString:@"+"]) {            return;        }        NSLog(@"------%@",dot);    }    if (historyStr.length>=2) {        NSString *dot=[historyStr substringWithRange:NSMakeRange(historyStr.length-2, 1)];        if ([dot isEqualToString:@"."]&&[btn.titleLabel.text isEqualToString:@"00"]) {            return;        }    }    if (historyStr.length==0) {        //首字符        if([firstArr containsObject:btn.titleLabel.text]){            return;        }    }else{        if ([LastArr containsObject:[historyStr substringFromIndex:historyStr.length-1]]) {            if ([LastArr containsObject:btn.titleLabel.text]) {                return;            }        }    }        if ([btn.titleLabel.text isEqualToString:@"收款"]) {            //处理收款按钮事件            if (self.block) {                self.block(_calculatePanel.text.floatValue);            }            return;        }    //处理清除按钮事件    if ([btn.titleLabel.text isEqualToString:@""]) {        if (historyStr.length>0) {        //清除原来的数字,重新输入            NSRange deleteRange = { [historyStr length] - 1, 1 };            [historyStr deleteCharactersInRange:deleteRange];            _historyLabel.text=historyStr;            NSString *result=[self calculatePattern:[self scanPattern:historyStr]];            _calculatePanel.text=[NSString stringWithFormat:@"%.2f",[result floatValue]];            moneyLabel.text=[NSString stringWithFormat:@"应收金额: ¥%.2f",[result floatValue]];            if (historyStr.length==0) {                _historyLabel.text =@"";            }        }else        {        //在为0的时候继续按清除键            _historyLabel.text =@"";            _calculatePanel.text=@"0.00";            moneyLabel.text=kNorMoneyLabelStr;        }        return;    }    if (_calculatePanel.text.floatValue>=300000) {        _calculatePanel.text=@"300000.00";        moneyLabel.text=@"应收金额: 300000.00";         _historyLabel.text=@"300000";        historyStr=@"300000".mutableCopy;        [PPHUDHelp showMessage:@"最高金额不能超过30w"];        return;    }//    NSString *moneyStr=[NSString stringWithFormat:@"%.2f",sender.titleLabel.text.floatValue];    [historyStr appendString:sender.titleLabel.text];    _historyLabel.text=[historyStr copy];    NSString *result=[self calculatePattern:[self scanPattern:historyStr]];    _calculatePanel.text=[NSString stringWithFormat:@"%.2f",[result floatValue]];    moneyLabel.text=[NSString stringWithFormat:@"应收金额: ¥%.2f",[result floatValue]];}

 

 

-(NSArray *)scanPattern:(NSString *)pattern {    NSUInteger header = 0;    NSMutableString *middleData = http://www.mamicode.com/[NSMutableString stringWithFormat:@""];    NSMutableArray *middleArray = [NSMutableArray array];    for (; header < pattern.length; header++) {        unichar letter = [pattern characterAtIndex:header];//        NSLog(@"%d", letter);        if ((letter >= 48 && letter <= 57) || letter == 46) {            char ch = (char)letter;            [middleData appendFormat:@"%c", ch];        }else {            if ([middleData length] <= 0) {                _error = YES;                break;            }            [middleArray addObject:[middleData copy]];            char ch = (char)letter;            NSString *character = [NSString stringWithFormat:@"%c", ch];            BOOL isHighLevel = (ch == / || ch == * || ch == %);            if([_operators count] == 0) {                [_operators addObject:character];            } else if(isHighLevel && ([[_operators lastObject] isEqualToString:@"+"] || [[_operators lastObject] isEqualToString:@"-"])) {                [_operators addObject:character];            }else {                [middleData appendString:[_operators lastObject]];                [middleArray addObject:[_operators lastObject]];                [_operators removeLastObject];                [_operators addObject:character];            }            NSRange range = NSMakeRange(0, [middleData length]);            [middleData deleteCharactersInRange:range];        }    }    [middleArray addObject:[middleData copy]];    while ([_operators count]) {        [middleArray addObject:[_operators lastObject]];        [_operators removeLastObject];    }    return [middleArray copy];}

 

 

- (NSString *)calculatePattern:(NSArray *)pattern {    //    Stack<Double> intStack = new Stack<Double>();    NSMutableArray *intStack = [NSMutableArray array];    double result=0;    for (int i = 0; i < [pattern count]; i++) {        NSString *letter = pattern[i];//        NSLog(@"%@", letter);        NSString *string = [letter stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];        if (string.length == 0 || [string containsString:@"."]) {            NSNumber *number = [NSNumber numberWithDouble:[letter doubleValue]];            [intStack addObject:number];        } else {            NSNumber *topNum = [intStack lastObject];            [intStack removeLastObject];            NSNumber *secondNum = [intStack lastObject];            [intStack removeLastObject];            double top = [topNum doubleValue];            double second = [secondNum doubleValue];            switch([pattern[i] characterAtIndex:0])            {                case *:                    result = top * second;                    break;                case /:                    result = second / top;                    break;                case +:                    result = top + second;                    break;                case -:                    result = second - top;                    break;            }            [intStack addObject:[NSNumber numberWithDouble:result]];        }    }    NSNumber *finalResult = [intStack lastObject];    [intStack removeLastObject];    return [NSString stringWithFormat:@"%@", finalResult];}

 

 

iOS 收款计算器算法