iOS簡易計算器的實現 – iPhone手機開發技術文章 iPhone軟體開發教學課程

//
//  CCLAppDelegate.m
//  TestCalculator
//
//  Created by lanouhn on 14-8-20.
//  Copyright (c) 2014年 vaercly@163.com 陳聰雷. All rights reserved.
//

#import "CCLAppDelegate.h"
#import "CCLCalculatorView.h"

@interface CCLAppDelegate ()
{
    UIView *_backgroundView;
    UILabel *_showResultLable;
}
@end

@implementation CCLAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    [self setupbgView];
    [self setupShowView];
    [self setupCalView];
    [self showCal];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)setupbgView
{
    _backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
    _backgroundView.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:_backgroundView];
    [_backgroundView release];
}

- (void)setupShowView
{
    _showResultLable = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, 280, 80)];
    _showResultLable.backgroundColor = [UIColor whiteColor];
    _showResultLable.font = [UIFont systemFontOfSize:45];
    _showResultLable.layer.cornerRadius = 6;
    _showResultLable.layer.borderColor = [UIColor grayColor].CGColor;
    _showResultLable.layer.borderWidth = 2;
    _showResultLable.textAlignment = NSTextAlignmentRight;
    [_backgroundView addSubview:_showResultLable];
    [_showResultLable release];
}

- (void)setupCalView
{
    NSArray *arr = @[@[@"MC", @"M+", @"M-", @"C"], @[@7, @8, @9, @"+"], @[@4, @5, @6, @"-"], @[@1, @2, @3, @"*"], @[@0, @".", @"/", @"="]];
    for (int i = 0; i < 5; i++) {
        CCLCalculatorView *calKeyView = [[CCLCalculatorView alloc] initWithFrame:CGRectMake(20, 150 + 80 * i, 280, 40) buttonTitle:arr[i]];
        [_backgroundView addSubview:calKeyView];
        [calKeyView release];
    }
}

- (void)showCal
{
//    CCLCalculatorView *calKeyView = [[CCLCalculatorView alloc] init];
//    NSString *str = [calKeyView text];
//    NSLog(@"%@", 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];
}

@end

//
//  CCLCalculatorView.m
//  TestCalculator
//
//  Created by lanouhn on 14-8-20.
//  Copyright (c) 2014年 vaercly@163.com 陳聰雷. All rights reserved.
//

#import "CCLCalculatorView.h"

@interface CCLCalculatorView ()
{
    UIButton *_btn;
}
@end

@implementation CCLCalculatorView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        for (int i = 0; i < 4; i++) {
            _btn = [UIButton buttonWithType:UIButtonTypeSystem];
            _btn.backgroundColor = [UIColor grayColor];
            _btn.frame = CGRectMake(0 + 75 * i, 0, 55, 50);
            [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            _btn.layer.cornerRadius = 6;
            _btn.layer.masksToBounds = YES;
            _btn.titleLabel.font = [UIFont systemFontOfSize:30];
            [_btn addTarget:self action:@selector(show:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:_btn];
        }
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame buttonTitle:(NSArray *)title
{
    self = [self initWithFrame:frame];
    if (self) {
        for (int i = 0; i < 4; i++) {
            UIButton *tempButton = (UIButton *)self.subviews[i];
            NSString *tempTitle = [NSString stringWithFormat:@"%@", title[i]];
            [tempButton setTitle:tempTitle forState:UIControlStateNormal];
        }
    }
    return self;
}

- (NSString *)text:(NSString *)str
{
    return [NSString stringWithFormat:@"%@", _btn.titleLabel];
}

- (NSString *)show:(UIButton *)button
{
    NSString *str = [NSString stringWithFormat:@"%@", button.titleLabel.text];
    UIView *tempView = self.window.subviews[0];
    UILabel *lable = tempView.subviews[0];
    
//    [UIView beginAnimations:nil context:nil];
//    [UIView setAnimationDuration:1];
//    lable.layer.borderColor = [UIColor redColor].CGColor;
//    lable.layer.borderWidth = 2;
//    [UIView commitAnimations];

    static CGFloat a;//存儲第一個操作數
    static char b;//存儲符號
    static char c[10];//依次存儲輸入的每個數字
    static int i;//控制存儲數字的位置
    if ([str isEqualToString:@"+"] || [str isEqualToString:@"-"] || [str isEqualToString:@"*"] || [str isEqualToString:@"/"]) {
        a = [lable.text floatValue];
        b = [str characterAtIndex:0];
        NSLog(@"test1 a = %g str = %@ b = %c", a, str, b);
        for (int i = 0; i < 10; i++) {
            c[i] = '
//
//  CCLAppDelegate.m
//  TestCalculator
//
//  Created by lanouhn on 14-8-20.
//  Copyright (c) 2014年 vaercly@163.com 陳聰雷. All rights reserved.
//

#import "CCLAppDelegate.h"
#import "CCLCalculatorView.h"

@interface CCLAppDelegate ()
{
    UIView *_backgroundView;
    UILabel *_showResultLable;
}
@end

@implementation CCLAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    [self setupbgView];
    [self setupShowView];
    [self setupCalView];
    [self showCal];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)setupbgView
{
    _backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
    _backgroundView.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:_backgroundView];
    [_backgroundView release];
}

- (void)setupShowView
{
    _showResultLable = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, 280, 80)];
    _showResultLable.backgroundColor = [UIColor whiteColor];
    _showResultLable.font = [UIFont systemFontOfSize:45];
    _showResultLable.layer.cornerRadius = 6;
    _showResultLable.layer.borderColor = [UIColor grayColor].CGColor;
    _showResultLable.layer.borderWidth = 2;
    _showResultLable.textAlignment = NSTextAlignmentRight;
    [_backgroundView addSubview:_showResultLable];
    [_showResultLable release];
}

- (void)setupCalView
{
    NSArray *arr = @[@[@"MC", @"M+", @"M-", @"C"], @[@7, @8, @9, @"+"], @[@4, @5, @6, @"-"], @[@1, @2, @3, @"*"], @[@0, @".", @"/", @"="]];
    for (int i = 0; i < 5; i++) {
        CCLCalculatorView *calKeyView = [[CCLCalculatorView alloc] initWithFrame:CGRectMake(20, 150 + 80 * i, 280, 40) buttonTitle:arr[i]];
        [_backgroundView addSubview:calKeyView];
        [calKeyView release];
    }
}

- (void)showCal
{
//    CCLCalculatorView *calKeyView = [[CCLCalculatorView alloc] init];
//    NSString *str = [calKeyView text];
//    NSLog(@"%@", 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];
}

@end

//
//  CCLCalculatorView.m
//  TestCalculator
//
//  Created by lanouhn on 14-8-20.
//  Copyright (c) 2014年 vaercly@163.com 陳聰雷. All rights reserved.
//

#import "CCLCalculatorView.h"

@interface CCLCalculatorView ()
{
    UIButton *_btn;
}
@end

@implementation CCLCalculatorView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        for (int i = 0; i < 4; i++) {
            _btn = [UIButton buttonWithType:UIButtonTypeSystem];
            _btn.backgroundColor = [UIColor grayColor];
            _btn.frame = CGRectMake(0 + 75 * i, 0, 55, 50);
            [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            _btn.layer.cornerRadius = 6;
            _btn.layer.masksToBounds = YES;
            _btn.titleLabel.font = [UIFont systemFontOfSize:30];
            [_btn addTarget:self action:@selector(show:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:_btn];
        }
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame buttonTitle:(NSArray *)title
{
    self = [self initWithFrame:frame];
    if (self) {
        for (int i = 0; i < 4; i++) {
            UIButton *tempButton = (UIButton *)self.subviews[i];
            NSString *tempTitle = [NSString stringWithFormat:@"%@", title[i]];
            [tempButton setTitle:tempTitle forState:UIControlStateNormal];
        }
    }
    return self;
}

- (NSString *)text:(NSString *)str
{
    return [NSString stringWithFormat:@"%@", _btn.titleLabel];
}

- (NSString *)show:(UIButton *)button
{
    NSString *str = [NSString stringWithFormat:@"%@", button.titleLabel.text];
    UIView *tempView = self.window.subviews[0];
    UILabel *lable = tempView.subviews[0];
    
//    [UIView beginAnimations:nil context:nil];
//    [UIView setAnimationDuration:1];
//    lable.layer.borderColor = [UIColor redColor].CGColor;
//    lable.layer.borderWidth = 2;
//    [UIView commitAnimations];

    static CGFloat a;//存儲第一個操作數
    static char b;//存儲符號
    static char c[10];//依次存儲輸入的每個數字
    static int i;//控制存儲數字的位置
    if ([str isEqualToString:@"+"] || [str isEqualToString:@"-"] || [str isEqualToString:@"*"] || [str isEqualToString:@"/"]) {
        a = [lable.text floatValue];
        b = [str characterAtIndex:0];
        NSLog(@"test1 a = %g str = %@ b = %c", a, str, b);
        for (int i = 0; i < 10; i++) {
            c[i] = '\0';
        }
        i = -1;
        lable.text = nil;
    } else if ([str isEqualToString:@"="]) {
        NSLog(@"test2 a = %g str = %@ b = %c", a, str, b);
        if (b == '+') {
            lable.text = [NSString stringWithFormat:@"%g", (a + [lable.text floatValue])];
        } else if (b == '-') {
            lable.text = [NSString stringWithFormat:@"%g", (a - [lable.text floatValue])];
        } else if (b == '*') {
            lable.text = [NSString stringWithFormat:@"%g", (a * [lable.text floatValue])];
        } else {
            lable.text = [NSString stringWithFormat:@"%g", (a / [lable.text floatValue])];
        }
            
    } else if ([str isEqualToString:@"C"]) {
        NSLog(@"C");
        i--;
        c[i] = '\0';
        i--;
        lable.text = [NSString stringWithFormat:@"%s", c];
    } else if ([str isEqualToString:@"MC"]) {
        NSLog(@"MC");
        for (int i = 0; i < 10; i++) {
            c[i] = '\0';
        }
        lable.text = [NSString stringWithFormat:@"%s", c];
        i = -1;
    } else if ([str isEqualToString:@"M+"]) {
        NSLog(@"M+");
        
    } else if ([str isEqualToString:@"M-"]) {
        NSLog(@"M-");
    } else {
        c[i] = [str characterAtIndex:0];
        lable.text = [NSString stringWithFormat:@"%s", c];
//        lable.text = str;
    }
    i++;
    return str;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
'; } i = -1; lable.text = nil; } else if ([str isEqualToString:@"="]) { NSLog(@"test2 a = %g str = %@ b = %c", a, str, b); if (b == '+') { lable.text = [NSString stringWithFormat:@"%g", (a + [lable.text floatValue])]; } else if (b == '-') { lable.text = [NSString stringWithFormat:@"%g", (a - [lable.text floatValue])]; } else if (b == '*') { lable.text = [NSString stringWithFormat:@"%g", (a * [lable.text floatValue])]; } else { lable.text = [NSString stringWithFormat:@"%g", (a / [lable.text floatValue])]; } } else if ([str isEqualToString:@"C"]) { NSLog(@"C"); i--; c[i] = '
//
//  CCLAppDelegate.m
//  TestCalculator
//
//  Created by lanouhn on 14-8-20.
//  Copyright (c) 2014年 vaercly@163.com 陳聰雷. All rights reserved.
//

#import "CCLAppDelegate.h"
#import "CCLCalculatorView.h"

@interface CCLAppDelegate ()
{
    UIView *_backgroundView;
    UILabel *_showResultLable;
}
@end

@implementation CCLAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    [self setupbgView];
    [self setupShowView];
    [self setupCalView];
    [self showCal];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)setupbgView
{
    _backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
    _backgroundView.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:_backgroundView];
    [_backgroundView release];
}

- (void)setupShowView
{
    _showResultLable = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, 280, 80)];
    _showResultLable.backgroundColor = [UIColor whiteColor];
    _showResultLable.font = [UIFont systemFontOfSize:45];
    _showResultLable.layer.cornerRadius = 6;
    _showResultLable.layer.borderColor = [UIColor grayColor].CGColor;
    _showResultLable.layer.borderWidth = 2;
    _showResultLable.textAlignment = NSTextAlignmentRight;
    [_backgroundView addSubview:_showResultLable];
    [_showResultLable release];
}

- (void)setupCalView
{
    NSArray *arr = @[@[@"MC", @"M+", @"M-", @"C"], @[@7, @8, @9, @"+"], @[@4, @5, @6, @"-"], @[@1, @2, @3, @"*"], @[@0, @".", @"/", @"="]];
    for (int i = 0; i < 5; i++) {
        CCLCalculatorView *calKeyView = [[CCLCalculatorView alloc] initWithFrame:CGRectMake(20, 150 + 80 * i, 280, 40) buttonTitle:arr[i]];
        [_backgroundView addSubview:calKeyView];
        [calKeyView release];
    }
}

- (void)showCal
{
//    CCLCalculatorView *calKeyView = [[CCLCalculatorView alloc] init];
//    NSString *str = [calKeyView text];
//    NSLog(@"%@", 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];
}

@end

//
//  CCLCalculatorView.m
//  TestCalculator
//
//  Created by lanouhn on 14-8-20.
//  Copyright (c) 2014年 vaercly@163.com 陳聰雷. All rights reserved.
//

#import "CCLCalculatorView.h"

@interface CCLCalculatorView ()
{
    UIButton *_btn;
}
@end

@implementation CCLCalculatorView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        for (int i = 0; i < 4; i++) {
            _btn = [UIButton buttonWithType:UIButtonTypeSystem];
            _btn.backgroundColor = [UIColor grayColor];
            _btn.frame = CGRectMake(0 + 75 * i, 0, 55, 50);
            [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            _btn.layer.cornerRadius = 6;
            _btn.layer.masksToBounds = YES;
            _btn.titleLabel.font = [UIFont systemFontOfSize:30];
            [_btn addTarget:self action:@selector(show:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:_btn];
        }
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame buttonTitle:(NSArray *)title
{
    self = [self initWithFrame:frame];
    if (self) {
        for (int i = 0; i < 4; i++) {
            UIButton *tempButton = (UIButton *)self.subviews[i];
            NSString *tempTitle = [NSString stringWithFormat:@"%@", title[i]];
            [tempButton setTitle:tempTitle forState:UIControlStateNormal];
        }
    }
    return self;
}

- (NSString *)text:(NSString *)str
{
    return [NSString stringWithFormat:@"%@", _btn.titleLabel];
}

- (NSString *)show:(UIButton *)button
{
    NSString *str = [NSString stringWithFormat:@"%@", button.titleLabel.text];
    UIView *tempView = self.window.subviews[0];
    UILabel *lable = tempView.subviews[0];
    
//    [UIView beginAnimations:nil context:nil];
//    [UIView setAnimationDuration:1];
//    lable.layer.borderColor = [UIColor redColor].CGColor;
//    lable.layer.borderWidth = 2;
//    [UIView commitAnimations];

    static CGFloat a;//存儲第一個操作數
    static char b;//存儲符號
    static char c[10];//依次存儲輸入的每個數字
    static int i;//控制存儲數字的位置
    if ([str isEqualToString:@"+"] || [str isEqualToString:@"-"] || [str isEqualToString:@"*"] || [str isEqualToString:@"/"]) {
        a = [lable.text floatValue];
        b = [str characterAtIndex:0];
        NSLog(@"test1 a = %g str = %@ b = %c", a, str, b);
        for (int i = 0; i < 10; i++) {
            c[i] = '\0';
        }
        i = -1;
        lable.text = nil;
    } else if ([str isEqualToString:@"="]) {
        NSLog(@"test2 a = %g str = %@ b = %c", a, str, b);
        if (b == '+') {
            lable.text = [NSString stringWithFormat:@"%g", (a + [lable.text floatValue])];
        } else if (b == '-') {
            lable.text = [NSString stringWithFormat:@"%g", (a - [lable.text floatValue])];
        } else if (b == '*') {
            lable.text = [NSString stringWithFormat:@"%g", (a * [lable.text floatValue])];
        } else {
            lable.text = [NSString stringWithFormat:@"%g", (a / [lable.text floatValue])];
        }
            
    } else if ([str isEqualToString:@"C"]) {
        NSLog(@"C");
        i--;
        c[i] = '\0';
        i--;
        lable.text = [NSString stringWithFormat:@"%s", c];
    } else if ([str isEqualToString:@"MC"]) {
        NSLog(@"MC");
        for (int i = 0; i < 10; i++) {
            c[i] = '\0';
        }
        lable.text = [NSString stringWithFormat:@"%s", c];
        i = -1;
    } else if ([str isEqualToString:@"M+"]) {
        NSLog(@"M+");
        
    } else if ([str isEqualToString:@"M-"]) {
        NSLog(@"M-");
    } else {
        c[i] = [str characterAtIndex:0];
        lable.text = [NSString stringWithFormat:@"%s", c];
//        lable.text = str;
    }
    i++;
    return str;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
'; i--; lable.text = [NSString stringWithFormat:@"%s", c]; } else if ([str isEqualToString:@"MC"]) { NSLog(@"MC"); for (int i = 0; i < 10; i++) { c[i] = '
//
//  CCLAppDelegate.m
//  TestCalculator
//
//  Created by lanouhn on 14-8-20.
//  Copyright (c) 2014年 vaercly@163.com 陳聰雷. All rights reserved.
//

#import "CCLAppDelegate.h"
#import "CCLCalculatorView.h"

@interface CCLAppDelegate ()
{
    UIView *_backgroundView;
    UILabel *_showResultLable;
}
@end

@implementation CCLAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    [self setupbgView];
    [self setupShowView];
    [self setupCalView];
    [self showCal];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)setupbgView
{
    _backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
    _backgroundView.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:_backgroundView];
    [_backgroundView release];
}

- (void)setupShowView
{
    _showResultLable = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, 280, 80)];
    _showResultLable.backgroundColor = [UIColor whiteColor];
    _showResultLable.font = [UIFont systemFontOfSize:45];
    _showResultLable.layer.cornerRadius = 6;
    _showResultLable.layer.borderColor = [UIColor grayColor].CGColor;
    _showResultLable.layer.borderWidth = 2;
    _showResultLable.textAlignment = NSTextAlignmentRight;
    [_backgroundView addSubview:_showResultLable];
    [_showResultLable release];
}

- (void)setupCalView
{
    NSArray *arr = @[@[@"MC", @"M+", @"M-", @"C"], @[@7, @8, @9, @"+"], @[@4, @5, @6, @"-"], @[@1, @2, @3, @"*"], @[@0, @".", @"/", @"="]];
    for (int i = 0; i < 5; i++) {
        CCLCalculatorView *calKeyView = [[CCLCalculatorView alloc] initWithFrame:CGRectMake(20, 150 + 80 * i, 280, 40) buttonTitle:arr[i]];
        [_backgroundView addSubview:calKeyView];
        [calKeyView release];
    }
}

- (void)showCal
{
//    CCLCalculatorView *calKeyView = [[CCLCalculatorView alloc] init];
//    NSString *str = [calKeyView text];
//    NSLog(@"%@", 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];
}

@end

//
//  CCLCalculatorView.m
//  TestCalculator
//
//  Created by lanouhn on 14-8-20.
//  Copyright (c) 2014年 vaercly@163.com 陳聰雷. All rights reserved.
//

#import "CCLCalculatorView.h"

@interface CCLCalculatorView ()
{
    UIButton *_btn;
}
@end

@implementation CCLCalculatorView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        for (int i = 0; i < 4; i++) {
            _btn = [UIButton buttonWithType:UIButtonTypeSystem];
            _btn.backgroundColor = [UIColor grayColor];
            _btn.frame = CGRectMake(0 + 75 * i, 0, 55, 50);
            [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            _btn.layer.cornerRadius = 6;
            _btn.layer.masksToBounds = YES;
            _btn.titleLabel.font = [UIFont systemFontOfSize:30];
            [_btn addTarget:self action:@selector(show:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:_btn];
        }
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame buttonTitle:(NSArray *)title
{
    self = [self initWithFrame:frame];
    if (self) {
        for (int i = 0; i < 4; i++) {
            UIButton *tempButton = (UIButton *)self.subviews[i];
            NSString *tempTitle = [NSString stringWithFormat:@"%@", title[i]];
            [tempButton setTitle:tempTitle forState:UIControlStateNormal];
        }
    }
    return self;
}

- (NSString *)text:(NSString *)str
{
    return [NSString stringWithFormat:@"%@", _btn.titleLabel];
}

- (NSString *)show:(UIButton *)button
{
    NSString *str = [NSString stringWithFormat:@"%@", button.titleLabel.text];
    UIView *tempView = self.window.subviews[0];
    UILabel *lable = tempView.subviews[0];
    
//    [UIView beginAnimations:nil context:nil];
//    [UIView setAnimationDuration:1];
//    lable.layer.borderColor = [UIColor redColor].CGColor;
//    lable.layer.borderWidth = 2;
//    [UIView commitAnimations];

    static CGFloat a;//存儲第一個操作數
    static char b;//存儲符號
    static char c[10];//依次存儲輸入的每個數字
    static int i;//控制存儲數字的位置
    if ([str isEqualToString:@"+"] || [str isEqualToString:@"-"] || [str isEqualToString:@"*"] || [str isEqualToString:@"/"]) {
        a = [lable.text floatValue];
        b = [str characterAtIndex:0];
        NSLog(@"test1 a = %g str = %@ b = %c", a, str, b);
        for (int i = 0; i < 10; i++) {
            c[i] = '\0';
        }
        i = -1;
        lable.text = nil;
    } else if ([str isEqualToString:@"="]) {
        NSLog(@"test2 a = %g str = %@ b = %c", a, str, b);
        if (b == '+') {
            lable.text = [NSString stringWithFormat:@"%g", (a + [lable.text floatValue])];
        } else if (b == '-') {
            lable.text = [NSString stringWithFormat:@"%g", (a - [lable.text floatValue])];
        } else if (b == '*') {
            lable.text = [NSString stringWithFormat:@"%g", (a * [lable.text floatValue])];
        } else {
            lable.text = [NSString stringWithFormat:@"%g", (a / [lable.text floatValue])];
        }
            
    } else if ([str isEqualToString:@"C"]) {
        NSLog(@"C");
        i--;
        c[i] = '\0';
        i--;
        lable.text = [NSString stringWithFormat:@"%s", c];
    } else if ([str isEqualToString:@"MC"]) {
        NSLog(@"MC");
        for (int i = 0; i < 10; i++) {
            c[i] = '\0';
        }
        lable.text = [NSString stringWithFormat:@"%s", c];
        i = -1;
    } else if ([str isEqualToString:@"M+"]) {
        NSLog(@"M+");
        
    } else if ([str isEqualToString:@"M-"]) {
        NSLog(@"M-");
    } else {
        c[i] = [str characterAtIndex:0];
        lable.text = [NSString stringWithFormat:@"%s", c];
//        lable.text = str;
    }
    i++;
    return str;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
'; } lable.text = [NSString stringWithFormat:@"%s", c]; i = -1; } else if ([str isEqualToString:@"M+"]) { NSLog(@"M+"); } else if ([str isEqualToString:@"M-"]) { NSLog(@"M-"); } else { c[i] = [str characterAtIndex:0]; lable.text = [NSString stringWithFormat:@"%s", c]; // lable.text = str; } i++; return str; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end

發佈留言