No.4 (code review)复杂计算器V1.0

上一个版本:只能连续+或者连续-或者连续*或者连续/ 不能交叉运算的BUG

此版本为修改版:


ViewController.m:

#import "ViewController.h"
#import "FingerUp.h"
@interface ViewController ()


@end






@implementation ViewController


FingerUp *fing;
NSString *signel = @"";
double numView = 0.0;
float rSum = 0.0f;
int count = 0;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor colorWithRed:102.0/255 green:255.0/255 blue:251.0/255 alpha:1];
    
    
    UITextField *aTextField = [[UITextField alloc] initWithFrame:CGRectMake(30, 100, 250, 50)];
    aTextField.borderStyle = UITextBorderStyleRoundedRect;
    aTextField.keyboardType = UIKeyboardTypeNumberPad;
    aTextField.tag = 101;
    aTextField.layer.cornerRadius = 6.0f;//设置圆角
//    aTextField.layer.borderWidth = 2.0f;//设置边框粗细
//    aTextField.layer.borderColor = [[UIColor redColor] CGColor];//设置边框颜色
    aTextField.backgroundColor = [UIColor whiteColor];
    aTextField.userInteractionEnabled = NO;
    aTextField.textAlignment = UITextAlignmentRight;
    [aTextField setFont:[UIFont fontWithName:@"STHeitiSC-Medium" size:30.00]];
    [self.view addSubview:aTextField];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *buttonPlus = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *button5 = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *button6 = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *buttonSub = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *button7 = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *button8 = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *button9 = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *buttonMult = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *buttonC = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *button0 = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *buttonResult = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *buttonDivision = [UIButton buttonWithType:UIButtonTypeCustom];
    
    
    button1.frame = CGRectMake(30, 200, 40, 40);
    button2.frame = CGRectMake(100, 200, 40, 40);
    button3.frame = CGRectMake(170, 200, 40, 40);
    buttonPlus.frame = CGRectMake(240, 200, 40, 40);
    button4.frame = CGRectMake(30, 280, 40, 40);
    button5.frame = CGRectMake(100, 280, 40, 40);
    button6.frame = CGRectMake(170, 280, 40, 40);
    buttonSub.frame = CGRectMake(240, 280, 40, 40);
    button7.frame = CGRectMake(30, 360, 40, 40);
    button8.frame = CGRectMake(100, 360, 40, 40);
    button9.frame = CGRectMake(170, 360, 40, 40);
    buttonMult.frame = CGRectMake(240, 360, 40, 40);
    buttonC.frame = CGRectMake(30, 440, 40, 40);
    button0.frame = CGRectMake(100, 440, 40, 40);
    buttonResult.frame = CGRectMake(170, 440, 40, 40);
    buttonDivision.frame = CGRectMake(240, 440, 40, 40);
    
    button1.backgroundColor = [UIColor grayColor];
    button2.backgroundColor = [UIColor grayColor];
    button3.backgroundColor = [UIColor grayColor];
    buttonPlus.backgroundColor = [UIColor grayColor];
    button4.backgroundColor = [UIColor grayColor];
    button5.backgroundColor = [UIColor grayColor];
    button6.backgroundColor = [UIColor grayColor];
    buttonSub.backgroundColor = [UIColor grayColor];
    button7.backgroundColor = [UIColor grayColor];
    button8.backgroundColor = [UIColor grayColor];
    button9.backgroundColor = [UIColor grayColor];
    buttonMult.backgroundColor = [UIColor grayColor];
    buttonC.backgroundColor = [UIColor grayColor];
    button0.backgroundColor = [UIColor grayColor];
    buttonResult.backgroundColor = [UIColor grayColor];
    buttonDivision.backgroundColor = [UIColor grayColor];
    
    
    button1.layer.cornerRadius = 5.0f;
    button2.layer.cornerRadius = 5.0f;
    button3.layer.cornerRadius = 5.0f;
    buttonPlus.layer.cornerRadius = 5.0f;
    button4.layer.cornerRadius = 5.0f;
    button5.layer.cornerRadius = 5.0f;
    button6.layer.cornerRadius = 5.0f;
    buttonSub.layer.cornerRadius = 5.0f;
    button7.layer.cornerRadius = 5.0f;
    button8.layer.cornerRadius = 5.0f;
    button9.layer.cornerRadius = 5.0f;
    buttonMult.layer.cornerRadius = 5.0f;
    buttonC.layer.cornerRadius = 5.0f;
    button0.layer.cornerRadius = 5.0f;
    buttonResult.layer.cornerRadius = 5.0f;
    buttonDivision.layer.cornerRadius = 5.0f;
    
    
    
    [button1 setTitle:@"1" forState:UIControlStateNormal];
    [button2 setTitle:@"2" forState:UIControlStateNormal];
    [button3 setTitle:@"3" forState:UIControlStateNormal];
    [buttonPlus setTitle:@"+" forState:UIControlStateNormal];
    [button4 setTitle:@"4" forState:UIControlStateNormal];
    [button5 setTitle:@"5" forState:UIControlStateNormal];
    [button6 setTitle:@"6" forState:UIControlStateNormal];
    [buttonSub setTitle:@"-" forState:UIControlStateNormal];
    [button7 setTitle:@"7" forState:UIControlStateNormal];
    [button8 setTitle:@"8" forState:UIControlStateNormal];
    [button9 setTitle:@"9" forState:UIControlStateNormal];
    [buttonMult setTitle:@"*" forState:UIControlStateNormal];
    [buttonC setTitle:@"C" forState:UIControlStateNormal];
    [button0 setTitle:@"0" forState:UIControlStateNormal];
    [buttonResult setTitle:@"=" forState:UIControlStateNormal];
    [buttonDivision setTitle:@"/" forState:UIControlStateNormal];
    
    
    [button1 addTarget:self action:@selector(downClick:) forControlEvents:UIControlEventTouchDown];
    [button1 addTarget:self action:@selector(upClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [button2 addTarget:self action:@selector(downClick:) forControlEvents:UIControlEventTouchDown];
    [button2 addTarget:self action:@selector(upClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [button3 addTarget:self action:@selector(downClick:) forControlEvents:UIControlEventTouchDown];
    [button3 addTarget:self action:@selector(upClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [buttonPlus addTarget:self action:@selector(downClick:) forControlEvents:UIControlEventTouchDown];
    [buttonPlus addTarget:self action:@selector(upClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [button4 addTarget:self action:@selector(downClick:) forControlEvents:UIControlEventTouchDown];
    [button4 addTarget:self action:@selector(upClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [button5 addTarget:self action:@selector(downClick:) forControlEvents:UIControlEventTouchDown];
    [button5 addTarget:self action:@selector(upClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [button6 addTarget:self action:@selector(downClick:) forControlEvents:UIControlEventTouchDown];
    [button6 addTarget:self action:@selector(upClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [buttonSub addTarget:self action:@selector(downClick:) forControlEvents:UIControlEventTouchDown];
    [buttonSub addTarget:self action:@selector(upClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [button7 addTarget:self action:@selector(downClick:) forControlEvents:UIControlEventTouchDown];
    [button7 addTarget:self action:@selector(upClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [button8 addTarget:self action:@selector(downClick:) forControlEvents:UIControlEventTouchDown];
    [button8 addTarget:self action:@selector(upClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [button9 addTarget:self action:@selector(downClick:) forControlEvents:UIControlEventTouchDown];
    [button9 addTarget:self action:@selector(upClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [buttonMult addTarget:self action:@selector(downClick:) forControlEvents:UIControlEventTouchDown];
    [buttonMult addTarget:self action:@selector(upClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [buttonC addTarget:self action:@selector(downClick:) forControlEvents:UIControlEventTouchDown];
    [buttonC addTarget:self action:@selector(upClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [button0 addTarget:self action:@selector(downClick:) forControlEvents:UIControlEventTouchDown];
    [button0 addTarget:self action:@selector(upClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [buttonResult addTarget:self action:@selector(downClick:) forControlEvents:UIControlEventTouchDown];
    [buttonResult addTarget:self action:@selector(upClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [buttonDivision addTarget:self action:@selector(downClick:) forControlEvents:UIControlEventTouchDown];
    [buttonDivision addTarget:self action:@selector(upClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    [self.view addSubview:button1];
    [self.view addSubview:button2];
    [self.view addSubview:button3];
    [self.view addSubview:buttonPlus];
    [self.view addSubview:button4];
    [self.view addSubview:button5];
    [self.view addSubview:button6];
    [self.view addSubview:buttonSub];
    [self.view addSubview:button7];
    [self.view addSubview:button8];
    [self.view addSubview:button9];
    [self.view addSubview:buttonMult];
    [self.view addSubview:buttonC];
    [self.view addSubview:button0];
    [self.view addSubview:buttonResult];
    [self.view addSubview:buttonDivision];
    
   
    
}
- (void)downClick:(UIButton *)button
{
    fing = [[FingerUp alloc] init];
    
    NSString *number1 = [button titleForState:UIControlStateNormal];
    UITextField *textField = (UITextField *) [self.view viewWithTag:101];
    if ([number1 isEqualToString:@"+"] || [number1 isEqualToString:@"-"] || [number1 isEqualToString:@"*"] || [number1 isEqualToString:@"/"]) {
        signel = number1;//用signel临时记录用户输入的运算符号
    }
    
    int number = number1.intValue;//讲按钮上的值转换成整形
    if ((number > 0 || [number1 isEqualToString:@"0"]) && count < 9) {//数字在0-9的范围时候(如果是数字再参加运算)
        
        if (0.0 == numView) {//如果值是0(也就是判断键盘上传进来的数是不是第一个数字)
            textField.text = [NSString stringWithFormat:@"%d", number];
            numView = number;
        }
        else if (count < 8) {//本计算器只支持8位运算
            numView *= 10.0;
            numView = numView + (double)number;
            long numViewL = (long)numView;
            textField.text = [NSString stringWithFormat:@"%ld", numViewL];
        }
        count ++;
    }
    else if (![signel isEqual:@""]) {
        count = 0;        //讲计位器清0
        [fing finger:numView sign:signel];//把数值传过去进行计算
        numView = 0; //数值模版清0(里面原来存的是要参加计算的数值)
    }


    if ([number1 isEqualToString:@"="]) {
        rSum = [fing count:signel];
        if (rSum != -1) {//如果没进行任何运算就点=号,那么返回的值是-1
            if (rSum * 100 == ((int)rSum) * 100) {//判断小数位是否有数字
                textField.text = [NSString stringWithFormat:@"%ld", (long)rSum];
            }
            else {
                textField.text = [NSString stringWithFormat:@"%.6f", rSum];
            }
            
        }
        //以下是清0
        [FingerUp afterRes];
        rSum = 0.0f;
        signel = @"";
        count = 0;
        numView = 0.0f;
    }
    
    if ([number1 isEqualToString:@"C"]) {
        //以下是清0
        rSum = 0.0f;
        signel = @"";
        numView = 0.0f;
        count = 0;
        [FingerUp afterRes];
        textField.text = @"";
    }
    [fing release];
    
   
   
    
    button.backgroundColor = [UIColor blueColor];
    
}


- (void)upClick:(UIButton *)button
{
    button.backgroundColor = [UIColor grayColor];
}


FingerUp.m


#import "FingerUp.h"


double a = 100000000.0f;
double b = 100000000.0f;
NSString *k = @"";
NSString *f = @"";
//double sSum = 0.0f;
//double mSum = 0.0f;
//double dSum = 0.0f;


@implementation FingerUp


+ (void)afterRes
{
    a = 100000000.0f;
    b = 100000000.0f;
//    sSum = 0.0f;
//    mSum = 0.0f;
//    dSum = 0.0f;
  
}




- (void)finger:(double)firstNumber
          sign:(NSString *)sign
{
     {//如果传进来的是数字,而不是符号
        if (100000000 == a) {
            a = firstNumber;
        }
        else if (100000000 == b) {
            b = firstNumber;
        }
    }
    
    if ([k isEqual:@""]) {
        k = sign;
    }
    else {
        f = sign;
    }
    
    
    if (![k isEqual:@""] && 100000000 != a && 100000000 != b) {
        if ([k isEqualToString:@"+"]) {
            a = a + b;
            b = 100000000.0f;
        }
        else if ([k isEqualToString:@"-"]) {
            a = a - b;
            b = 100000000.0f;
        }
        else if ([k isEqualToString:@"*"]) {
            a = a * b;
            b = 100000000.0f;
        }
        else if ([k isEqualToString:@"/"]) {
            a = a / b;
            b = 100000000.0f;
        }
        k = f;
        b = 100000000.0f;
    }
    NSLog(@"%f, %f", a, b);
}


- (double)count:(NSString *)sign
{
    if ([sign isEqualToString:@"+"]) {
        return a;
    }
    if ([sign isEqualToString:@"-"]) {
        return a;
    }
    if ([sign isEqualToString:@"*"]) {
        return a;
    }
    if ([sign isEqualToString:@"/"]) {
        return a;
    }
    return -1;
}




@end




你可能感兴趣的:(ios,UI)