码迷,mamicode.com
首页 > 其他好文 > 详细

创建简易计算器

时间:2014-08-22 10:58:46      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:application   uiview   interface   

@interface AppDelegate ()
{
    UIView *_containerView;
    UILabel *_lable;
    CGFloat _fristNum;
    CGFloat _secondNum;
    NSInteger _tempNum;
    NSMutableString *_str;
}
@end
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    _containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
    _containerView.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:_containerView];
    [_containerView release];
    
    _lable = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 50)];
    _lable.backgroundColor = [UIColor blackColor];
    _lable.text = @"0";
    _lable.textColor = [UIColor whiteColor];
    _lable.textAlignment = NSTextAlignmentRight;
    _lable.font = [UIFont systemFontOfSize:30];
    _lable.enabled = YES;
    [_containerView addSubview:_lable];
    [_lable release];
    
    NSArray *number = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
    for (int i = 0; i < 3; i++) {
        for (int j = 0 ; j < 3; j++) {
            UIButton *bun = [[UIButton alloc] initWithFrame:CGRectMake(10 + 80 * j, 450 - i * 90 , 70, 65)];
            bun.backgroundColor = [UIColor grayColor];
            bun.layer.cornerRadius = 5;
            bun.titleLabel.font = [UIFont systemFontOfSize:30];
            [bun setTitle:number[j + i * 3] forState:UIControlStateNormal];
            [bun addTarget:self action:@selector(click:) forControlEvents: UIControlEventTouchUpInside];
            [_containerView addSubview:bun];
        }
    }
    
    NSArray *operat = @[@"+",@"-",@"*",@"/"];
    for (int i = 0; i < 4; i++) {
        UIButton *opBun = [[UIButton alloc] initWithFrame:CGRectMake(10 + i * 78, 180, 70, 65)];
        opBun.backgroundColor = [UIColor blueColor];
        opBun.layer.cornerRadius = 5;
        opBun.titleLabel.font = [UIFont systemFontOfSize:30];
        [opBun setTitle:operat[i] forState:UIControlStateNormal];
        [opBun addTarget:self action:@selector(ract:) forControlEvents:UIControlEventTouchUpInside];
        [_containerView addSubview:opBun];
    }
    
    //创建=号
    UIButton *equal = [[UIButton alloc] initWithFrame:CGRectMake(250, 450, 65, 65)];
    equal.backgroundColor = [UIColor redColor];
    equal.layer.cornerRadius = 5;
    equal.titleLabel.font = [UIFont systemFontOfSize:20];
    [equal setTitle:@"=" forState:UIControlStateNormal];
    [equal addTarget:self action:@selector(equal:) forControlEvents:UIControlEventTouchUpInside];
    [_containerView addSubview:equal];
    
    //创建0
    UIButton *zero = [[UIButton alloc] initWithFrame:CGRectMake(250, 360, 65, 65)];
    zero.backgroundColor = [UIColor grayColor];
    zero.layer.cornerRadius = 5;
    zero.titleLabel.font = [UIFont systemFontOfSize:30];
    [zero setTitle:@"0" forState:UIControlStateNormal];
    [zero addTarget:self action:@selector(zero:) forControlEvents:UIControlEventTouchUpInside];
    [_containerView addSubview:zero];

    //创建.
    UIButton *point = [[UIButton alloc] initWithFrame:CGRectMake(250, 270, 65, 65)];
    point.backgroundColor = [UIColor grayColor];
    point.layer.cornerRadius = 5;
    point.titleLabel.font = [UIFont systemFontOfSize:30];
    [point setTitle:@"." forState:UIControlStateNormal];
    [point addTarget:self action:@selector(point:) forControlEvents:UIControlEventTouchUpInside];
    [_containerView addSubview:point];
    
    //创建AC
    UIButton *ac = [[UIButton alloc] initWithFrame:CGRectMake(170, 100, 65, 65)];
    ac.backgroundColor = [UIColor grayColor];
    ac.layer.cornerRadius = 5;
    ac.titleLabel.font = [UIFont systemFontOfSize:20];
    [ac setTitle:@"AC" forState:UIControlStateNormal];
    [ac addTarget:self action:@selector(ac:) forControlEvents:UIControlEventTouchUpInside];
    [_containerView addSubview:ac];
    
    //创建back
    UIButton *back = [[UIButton alloc] initWithFrame:CGRectMake(245, 100, 65, 65)];
    back.backgroundColor = [UIColor grayColor];
    back.layer.cornerRadius = 5;
    back.titleLabel.font = [UIFont systemFontOfSize:20];
    [back setTitle:@"back" forState:UIControlStateNormal];
    [back addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [_containerView addSubview:back];
    
    _str = [[NSMutableString alloc] init];
    
    return YES;
}
//数字键
- (void)click:(UIButton *)bun
{
    [_str appendString:[bun currentTitle]];
    _lable.text = _str;
}

//符号
- (void)ract:(UIButton *)bun
{
   if ([bun.titleLabel.text isEqualToString:@"+"]) {
        _fristNum = _lable.text.floatValue;
        _tempNum = 1;
    }
    if ([bun.titleLabel.text isEqualToString:@"-"]) {
        _fristNum = _lable.text.floatValue;
        _tempNum = 2;
    }
    if ([bun.titleLabel.text isEqualToString:@"*"]) {
        _fristNum = _lable.text.floatValue;
        _tempNum = 3;
    }
    if ([bun.titleLabel.text isEqualToString:@"/"]) {
        _fristNum = _lable.text.floatValue;
        _tempNum = 4;
    }
    [_str setString:@" "];
}

//=号
- (void)equal:(UIButton *)bun
{
    _secondNum = _lable.text.floatValue;
    if (_tempNum == 1) {
        _lable.text = [NSString stringWithFormat:@"%g",_secondNum + _fristNum];
    }
    if (_tempNum == 2) {
        _lable.text = [NSString stringWithFormat:@"%g",_fristNum - _secondNum];
    }
    if (_tempNum == 3) {
        _lable.text = [NSString stringWithFormat:@"%g",_fristNum * _secondNum];
    }
    if (_tempNum == 4) {
        _lable.text = [NSString stringWithFormat:@"%g",_fristNum / _secondNum];
    }
    if (_tempNum != 1 && _tempNum != 2 && _tempNum != 3 &&_tempNum != 4) {
        _lable.text = [NSString stringWithFormat:@"%g",_secondNum];
    }
    _tempNum = 0;
    [_str setString:@" "];
}

//0
- (void)zero:(UIButton *)bun
{
    [_str appendString:[bun currentTitle]];
    _lable.text = _str;
}
//小数点
- (void)point:(UIButton *)bun
{
    if ([_lable.text isEqualToString:@"0"]) {
        [_str appendString:@"0"];
    }
    [_str appendString:[bun currentTitle]];
    _lable.text = _str;
}
//清除
- (void)ac:(UIButton *)bun
{
    _lable.text = @"0";
    [_str setString:@" "];
}
//撤销
- (void)back:(UIButton *)bun
{
    if ([_str length] > 0) {
        [_str deleteCharactersInRange:NSMakeRange([_str length]-1, 1)];
        _lable.text = _str;
    }
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
- (void)dealloc
{
    [_window release];
    [super dealloc];
}
通过以上代码,计算器能进行基本的加减乘除运算.

创建简易计算器,布布扣,bubuko.com

创建简易计算器

标签:application   uiview   interface   

原文地址:http://blog.csdn.net/w_sx_/article/details/38750397

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!