zoukankan      html  css  js  c++  java
  • iOS01 计算器

            
              
            #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:@"%@
    %@",_operator1,title];  
                        _operator2 = @"";  
                        if ([btn.currentTitle isEqualToString:@"="])  
                        {  
                            _operator = @"";  
                        }  
                        else  
                        {  
                            _operator = title;  
                        }  
                    }  
                    else  
                    {  
                        _operator = title;  
                        _display.text = [NSString stringWithFormat:@"%@
    %@",_operator1,_operator];  
                    }  
                }  
                else//是操作数  
                {  
                    if (_operator.length)//判断是哪个操作数  
                    {  
                        if ([_operator2 containsString:@"."] && [title isEqualToString:@"."])  
                        {  
                              
                        }  
                        else  
                        {  
                            _operator2 = [_operator2 stringByAppendingString:title];  
                            _display.text = [NSString stringWithFormat:@"%@
    %@%@",_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 
  • 相关阅读:
    2块硬盘宝塔只能读取一块?宝塔一键磁盘挂载命令
    Python爬虫入门笔记
    linux搭建web(网站)环境
    织梦dedecms管理模块管理不动卡死解决办法
    php7.1安装swoole扩展
    Linux下centos7、PHP7.1安装Redis扩展教程
    centos7利用yum安装lnmp的教程(linux+nginx+php7.1+mysql5.7)
    Linux各目录及每个目录的详细介绍
    Linux samba的配置和使用
    PHP abstract与interface之间的区别
  • 原文地址:https://www.cnblogs.com/ytmaylover/p/5050122.html
Copyright © 2011-2022 走看看