温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iOS是怎样实现简单计算器小功能

发布时间:2022-02-05 10:01:05 来源:亿速云 阅读:172 作者:柒染 栏目:开发技术

本篇文章为大家展示了iOS是怎样实现简单计算器小功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

iOS是怎样实现简单计算器小功能

SimpleCaculatorViewController.h

//
//  SimpleCaculatorViewController.h
//  SimpleCaculator
//
//  Created by LI Junui on 14-2-12.
//  Copyright (c) 2014年 LEE JUNHUI. All rights reserved.
//
 
#import <UIKit/UIKit.h>
 
@interface SimpleCaculatorViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *displayScreen;
- (IBAction)numberBtnClick:(UIButton *)sender;
- (IBAction)clearDS:(UIButton *)sender;
- (IBAction)caculate:(UIButton *)sender;
- (IBAction)hint:(UIButton *)sender;
- (IBAction)act:(UIButton *)sender;
- (IBAction)clearBack:(UIButton *)sender;
 
 
 
@property(assign, nonatomic) BOOL isUserInputingNumber;
@property(assign, nonatomic) int num1;
@property(assign, nonatomic) int num2;
@property(assign, nonatomic) int tagForAct;
 
@end

SimpleCaculatorViewController.m

//
//  SimpleCaculatorViewController.m
//  SimpleCaculator
//
//  Created by LI Junui on 14-2-12.
//  Copyright (c) 2014年 LEE JUNHUI. All rights reserved.
//
 
#import "SimpleCaculatorViewController.h"
 
@interface SimpleCaculatorViewController ()
 
@end
 
@implementation SimpleCaculatorViewController
 
//记录数字按钮点击事件
- (IBAction)numberBtnClick:(UIButton *)sender {
    
    if(self.isUserInputingNumber){
        int re = [_displayScreen.text intValue] * 10 + [sender.currentTitle intValue];
        _displayScreen.text = [NSString stringWithFormat:@"%d",re];
    } else{
        [_displayScreen setText:sender.currentTitle];
        _isUserInputingNumber = YES;//因为第一次进入程序会输入数字,因此为YES
    }
}
 
//清零操作
- (IBAction)clearDS:(UIButton *)sender {
    
    _displayScreen.text = @"0";
    _isUserInputingNumber = NO;//表示没有再输入了
}
 
//得到结果
- (IBAction)caculate:(UIButton *)sender {
    int re = 0;
    _num2 = [_displayScreen.text intValue];
    switch (_tagForAct) {
        case 1: //加法
            re = _num1 + _num2;
            break;
        case 2: //减法
            re = _num1 - _num2;
            break;
        case 3: //乘法
            re = _num1 * _num2;
            break;
        case 4: //除法
            re = _num1 / _num2;
            break;
    }
    _displayScreen.text = [NSString stringWithFormat:@"=%d", re];
    _num1 = 0;
    _num2 = 0;
}
 
//弹出提示对话框
- (IBAction)hint:(UIButton *)sender {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"本计算器由LJH出品" delegate:self cancelButtonTitle:@"返回" otherButtonTitles: nil];
    [alert show];
}
 
//进行四则运算
- (IBAction)act:(UIButton *)sender {
    //1.得到_displayScreen上的数字
    _num1 = [_displayScreen.text intValue];
    _displayScreen.text = sender.currentTitle;
    _isUserInputingNumber =YES;
    switch (sender.tag) {
        case 1: //加法
            _tagForAct = 1;
            break;
        case 2: //减法
            _tagForAct = 2;
            break;
        case 3: //乘法
            _tagForAct = 3;
            break;
        case 4: //除法
            _tagForAct = 4;
            break;
    }
}
 
//进行回删操作
- (IBAction)clearBack:(UIButton *)sender {
    int length = [_displayScreen.text length];
    int temp = [_displayScreen.text intValue];
    temp = temp/length;
}

上述内容就是iOS是怎样实现简单计算器小功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ios
AI