欢迎来到代码驿站!

iOS代码

当前位置:首页 > 移动开发 > iOS代码

iOS开发实现简单计算器功能

时间:2023-02-02 10:13:31|栏目: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开发中TabBar再次点击实现刷新效果

栏    目:iOS代码

下一篇:没有了

本文标题:iOS开发实现简单计算器功能

本文地址:http://www.codeinn.net/misctech/224970.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有