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 
  • 相关阅读:
    CF1172F
    CF506E
    【清华集训2014】玛里苟斯
    CF516E Drazil and His Happy Friends
    [NOI2017]游戏(2-SAT)
    [bzoj2878][Noi2012]迷失游乐园(基环树dp)
    bzoj3545/bzoj3551 [ONTAK2010]Peaks/Peaks加强版
    [bzoj1791][ioi2008]Island 岛屿(基环树、树的直径)
    [AT2306]Rearranging(拓扑序)
    [bzoj5301][Cqoi2018]异或序列
  • 原文地址:https://www.cnblogs.com/ytmaylover/p/5050122.html
Copyright © 2011-2022 走看看