20150630_UI之计算器练习

本文介绍了一个简单的iOS计算器应用程序的实现过程,使用Objective-C语言编写。文章详细解释了如何创建用户界面,包括按钮和显示屏,并实现了基本的算术运算功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//
//  ViewController.m
//  IOS150630_UI_Button计算器
//
//  Created by PengJunlong on 15/6/30.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    UILabel *_display;
    NSString *_operator1;
    NSString *_operator;
    NSString *_operator2;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //创建UI界面
    _display = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, self.view.frame.size.width-20, 100)];
    _display.backgroundColor = [UIColor grayColor];
    _display.alpha = 0.8;
    _display.textAlignment = NSTextAlignmentRight;
    _display.numberOfLines = 2;
    _display.text = @"";
    _operator = @"";
    _operator1 = @"";
    _operator2 = @"";
    [self.view addSubview:_display ];
    NSArray *titleArray1 = @[@"MC",@"M+",@"M-",@"MR",@"清除"];
    NSArray *titleArray2 = @[@[@"7",@"8",@"9",@"+"],@[@"4",@"5",@"6",@"-"],@[@"1",@"2",@"3",@"*"],@[@"0",@".",@"=",@"/"]];
    CGFloat size1 = (self.view.frame.size.width-60)/5;
    CGFloat size2 = (self.view.frame.size.width-50)/4;
    for (int i=0; i<5; i++)
    {
        CGRect frame = CGRectMake(10+i*(size1+10), 180, size1, 50);
        [self createButtonByFrame:frame withTitle:titleArray1[i]];
    }
    for (int i=0; i<4; i++)
    {
        for (int j=0; j<4; j++)
        {
            [self createButtonByFrame:CGRectMake(10+j*(size2+10), 240+i*60, size2, 50) withTitle:titleArray2[i][j]];
        }
    }
}

- (void)createButtonByFrame:(CGRect)frame withTitle:(NSString *)title
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = frame;
    btn.backgroundColor = [UIColor grayColor];
    btn.alpha = 0.8;
    btn.layer.cornerRadius = 10;
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:25];
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btn];
}

- (void)btnClicked:(UIButton *)btn
{
    NSString *title = btn.currentTitle;
    if ([title hasPrefix:@"M"])
    {
        
    }
    if ([title isEqualToString:@"清除"])
    {
        _display.text = @"";
        _operator = @"";
        _operator1 = @"";
        _operator2 = @"";
        return;
    }
    if ([self isOperator:title])
    {
        if (_operator.length)//判断操作运算符是否已经存在
        {
            float result = 0.0;
            float value1 = [_operator1 floatValue];
            float value2 = [_operator2 floatValue];
            unichar ch = [_operator characterAtIndex:0];
            switch (ch)
            {
                case '+':
                    result = value1+value2;
                    break;
                case '-':
                    result = value1-value2;
                    break;
                case '*':
                    result = value1*value2;
                    break;
                case '/':
                    result = value1/value2;
                    break;
                default:
                    break;
            }
            _operator1 = [NSString stringWithFormat:@"%g",result];
            _display.text = [NSString stringWithFormat:@"%@\n%@",_operator1,title];
            _operator2 = @"";
            if ([btn.currentTitle isEqualToString:@"="])
            {
                _operator = @"";
            }
            else
            {
                _operator = title;
            }
        }
        else
        {
            _operator = title;
            _display.text = [NSString stringWithFormat:@"%@\n%@",_operator1,_operator];
        }
    }
    else//是操作数
    {
        if (_operator.length)//判断是哪个操作数
        {
            if ([_operator2 containsString:@"."] && [title isEqualToString:@"."])
            {
                
            }
            else
            {
                _operator2 = [_operator2 stringByAppendingString:title];
                _display.text = [NSString stringWithFormat:@"%@\n%@%@",_operator1,_operator,_operator2];
            }
        }
        else
        {
            if ([_operator1 containsString:@"."] && [title isEqualToString:@"."])
            {
                
            }
            else
            {
                _operator1 = [_operator1 stringByAppendingString:title];
                _display.text = _operator1;
            }
        }
    }
}

//判断标题是否为运算符+ - * / =
- (BOOL)isOperator:(NSString *)title
{
    if ([[NSCharacterSet characterSetWithCharactersInString:@"+-*/="] characterIsMember:[title characterAtIndex:0]])
    {
        return YES;
    }
    
    return NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值