IOS开发之简单计算器

Posted cynchanpin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IOS开发之简单计算器相关的知识,希望对你有一定的参考价值。

用Object-C写的一个简单的计算机程序,主要学习按钮的action动作。
以下是主界面:技术分享

以下代码时界面按钮和ViewController.h连接的地方:

- (IBAction)button_0:(UIButton *)sender;
- (IBAction)button_dian:(UIButton *)sender;
- (IBAction)button_dengyu:(UIButton *)sender;
- (IBAction)button_1:(UIButton *)sender;
- (IBAction)button_2:(UIButton *)sender;
- (IBAction)button_3:(UIButton *)sender;

- (IBAction)button_4:(UIButton *)sender;
- (IBAction)button_5:(UIButton *)sender;
- (IBAction)button_6:(UIButton *)sender;
- (IBAction)button_7:(UIButton *)sender;
- (IBAction)button_8:(UIButton *)sender;
- (IBAction)button_9:(UIButton *)sender;
- (IBAction)button_jia:(UIButton *)sender;
- (IBAction)button_jian:(UIButton *)sender;
- (IBAction)button_cheng:(UIButton *)sender;
- (IBAction)button_chu:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UITextField *result;

以下在ViewController.m中来写按钮的点击事件:


- (IBAction)button_0:(UIButton *)sender {
    NSString *resString = self.result.text;
    NSString *newString = [resString stringByAppendingString:@"0"];
    self.result.text = [NSString stringWithFormat:@"%@",newString];

}

- (IBAction)button_dian:(UIButton *)sender {
    NSString *resString = self.result.text;
    NSString *newString = [resString stringByAppendingString:@"."];
    self.result.text = [NSString stringWithFormat:@"%@",newString];
}

- (IBAction)button_dengyu:(UIButton *)sender {
    NSString *resString = self.result.text;
    double sum;
    if([strOperator isEqualToString:@"+"]){
        sum = [str doubleValue] + [resString doubleValue];
        self.result.text =[NSString stringWithFormat:@"%f",sum];
    }else if([strOperator isEqualToString:@"-"]){
        sum = [str doubleValue] - [resString doubleValue];
        self.result.text =[NSString stringWithFormat:@"%f",sum];
    }else if([strOperator isEqualToString:@"+"]){
        sum = [str doubleValue] * [resString doubleValue];
        self.result.text =[NSString stringWithFormat:@"%f",sum];
    }else if([strOperator isEqualToString:@"+"]){
        sum = [str doubleValue] / [resString doubleValue];
        self.result.text =[NSString stringWithFormat:@"%f",sum];
    }
}

- (IBAction)button_1:(UIButton *)sender {
    NSString *resString = self.result.text;
    NSString *newString = [resString stringByAppendingString:@"1"];
    self.result.text = [NSString stringWithFormat:@"%@",newString];
}
- (IBAction)button_2:(UIButton *)sender {
    NSString *resString = self.result.text;
    NSString *newString = [resString stringByAppendingString:@"2"];
    self.result.text = [NSString stringWithFormat:@"%@",newString];

}
- (IBAction)button_3:(UIButton *)sender {
    NSString *resString = self.result.text;
    NSString *newString = [resString stringByAppendingString:@"3"];
    self.result.text = [NSString stringWithFormat:@"%@",newString];

}

- (IBAction)button_4:(UIButton *)sender {
    NSString *resString = self.result.text;
    NSString *newString = [resString stringByAppendingString:@"4"];
    self.result.text = [NSString stringWithFormat:@"%@",newString];

}
- (IBAction)button_5:(UIButton *)sender {
    NSString *resString = self.result.text;
    NSString *newString = [resString stringByAppendingString:@"5"];
    self.result.text = [NSString stringWithFormat:@"%@",newString];

}
- (IBAction)button_6:(UIButton *)sender {
    NSString *resString = self.result.text;
    NSString *newString = [resString stringByAppendingString:@"6"];
    self.result.text = [NSString stringWithFormat:@"%@",newString];

}
- (IBAction)button_7:(UIButton *)sender {
    NSString *resString = self.result.text;
    NSString *newString = [resString stringByAppendingString:@"7"];
    self.result.text = [NSString stringWithFormat:@"%@",newString];

}
- (IBAction)button_8:(UIButton *)sender {
    NSString *resString = self.result.text;
    NSString *newString = [resString stringByAppendingString:@"8"];
    self.result.text = [NSString stringWithFormat:@"%@",newString];

}
- (IBAction)button_9:(UIButton *)sender {
    NSString *resString = self.result.text;
    NSString *newString = [resString stringByAppendingString:@"9"];
    self.result.text = [NSString stringWithFormat:@"%@",newString];

}
- (IBAction)button_jia:(UIButton *)sender {
    str = self.result.text;
    strOperator = @"+";
    self.result.text = [NSString stringWithFormat:@" "];

}
- (IBAction)button_jian:(UIButton *)sender {
    str = self.result.text;
      strOperator = @"-";
    self.result.text = [NSString stringWithFormat:@" "];}
- (IBAction)button_cheng:(UIButton *)sender {
    str = self.result.text;
      strOperator = @"*";
    self.result.text = [NSString stringWithFormat:@" "];
}
- (IBAction)button_chu:(UIButton *)sender {
    str = self.result.text;
      strOperator = @"/";
    self.result.text = [NSString stringWithFormat:@" "];}

注意这里的str 和strOperator在ViewController.h中声明的:

 NSString *strOperator;
     NSString *str;

这就完毕了一个简单的计算器~


以上是关于IOS开发之简单计算器的主要内容,如果未能解决你的问题,请参考以下文章

李洪强iOS开发之应用程序的本质与简单执行过程

Java基础之方法的调用重载以及简单的递归

iOS开发CGRectGetMidX. CGRectGetMidY.CGRectGetMinY. CGRectGetMaxY. CGRectGetMinX. CGRectGetMaxX的使用(代码片段

iOS开发CGRectGetMidX. CGRectGetMidY.CGRectGetMinY. CGRectGetMaxY. CGRectGetMinX. CGRectGetMaxX的使用(代码片段

ios开发之--简单动画效果的添加

初学IOS开发-简单的加法计算器swift版本