IOS 创建计算器

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>{
    //上次输入的内容
    CGFloat _prefixValue;
    
    //上次所输入的类型,Yes代表操作符,No代表数字
    BOOL _lastEnterType;
    
    NSString * _char;
}

@property (retain, nonatomic) UIWindow *window;

@property (retain, nonatomic) UILabel * label;

@property (retain, nonatomic) UIButton * aButton;
@property (retain, nonatomic) UIButton * aButton2;
@property (retain, nonatomic) UIButton * aButton3;
@property (retain, nonatomic) UIButton * aButton4;
@property (retain, nonatomic) UIButton * aButton5;
@property (retain, nonatomic) UIButton * aButton6;
@property (retain, nonatomic) UIButton * aButton7;
@property (retain, nonatomic) UIButton * aButton8;
@property (retain, nonatomic) UIButton * aButton9;
@property (retain, nonatomic) UIButton * aButton0;
@property (retain, nonatomic) UIButton * cButton;
@property (retain, nonatomic) UIButton * addButton;
@property (retain, nonatomic) UIButton * minButton;
@property (retain, nonatomic) UIButton * mulButton;
@property (retain, nonatomic) UIButton * divButton;

@end


#import "AppDelegate.h"
//在.h里全部用@class
//在.m里全部用#import

@implementation AppDelegate

- (void) dealloc
{
    [_window release];
    [_label release];
    [_aButton release];
    [_aButton2 release];
    [_aButton3 release];
    [_aButton4 release];
    [_aButton5 release];
    [_aButton6 release];
    [_aButton7 release];
    [_aButton8 release];
    [_aButton9 release];
    [_aButton0 release];
    [_addButton release];
    [_minButton release];
    [_mulButton release];
    [_divButton release];

    _window = nil;
    _label = nil;
    _aButton = nil;
    _aButton2 = nil;
    _aButton3 = nil;
    _aButton4 = nil;
    _aButton5 = nil;
    _aButton5 = nil;
    _aButton6 = nil;
    _aButton7 = nil;
    _aButton8 = nil;
    _aButton9 = nil;
    _aButton0 = nil;
    _addButton = nil;
    _minButton = nil;
    _mulButton = nil;
    _divButton = nil;
    
    [super dealloc];
}



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

- (void) creaBoard
{
    _lastEnterType = NO; //YES 和 NO 都要大写
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(40, 60, 245, 60)];
    [_label setBackgroundColor:[UIColor greenColor]];
    [_label setTextColor:[UIColor blackColor]];
    
    //边框宽度
    _label.layer.borderWidth = 2;
    _label.layer.borderColor = [UIColor whiteColor].CGColor;
    
    [_label setText:@""];
    [_label setTextAlignment:NSTextAlignmentRight];
    
    [self.window addSubview:_label];
    [_label release];
    
    self.aButton = [[UIButton alloc] initWithFrame:CGRectMake(40, 150, 50, 50)];
    [_aButton setBackgroundColor:[UIColor yellowColor]];
    [_aButton setTitle:@"1" forState:UIControlStateNormal];
    [_aButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [_aButton addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    
    _aButton.layer.borderWidth = 2;
    _aButton.layer.borderColor = [UIColor blackColor].CGColor;
    
    [self.window addSubview:_aButton];
    
    self.aButton2 = [[UIButton alloc] initWithFrame:CGRectMake(105, 150, 50, 50)];
    [_aButton2 setBackgroundColor:[UIColor yellowColor]];
    [_aButton2 setTitle:@"2" forState:UIControlStateNormal];
    [_aButton2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [_aButton2 addTarget:self action:@selector(buttonAction2) forControlEvents:UIControlEventTouchUpInside];
    
    _aButton2.layer.borderWidth = 2;
    _aButton2.layer.borderColor = [UIColor blackColor].CGColor;
    
    [self.window addSubview:_aButton2];
    
    self.aButton3 = [[UIButton alloc] initWithFrame:CGRectMake(170, 150, 50, 50)];
    [_aButton3 setBackgroundColor:[UIColor yellowColor]];
    [_aButton3 setTitle:@"3" forState:UIControlStateNormal];
    [_aButton3 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [_aButton3 addTarget:self action:@selector(buttonAction3) forControlEvents:UIControlEventTouchUpInside];
    
    _aButton3.layer.borderWidth = 2;
    _aButton3.layer.borderColor = [UIColor blackColor].CGColor;
    
    [self.window addSubview:_aButton3];
    
    self.aButton4 = [[UIButton alloc] initWithFrame:CGRectMake(40, 230, 50, 50)];
    [_aButton4 setBackgroundColor:[UIColor yellowColor]];
    [_aButton4 setTitle:@"4" forState:UIControlStateNormal];
    [_aButton4 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [_aButton4 addTarget:self action:@selector(buttonAction4) forControlEvents:UIControlEventTouchUpInside];
    
    _aButton4.layer.borderWidth = 2;
    _aButton4.layer.borderColor = [UIColor blackColor].CGColor;
    
    [self.window addSubview:_aButton4];

    self.aButton5 = [[UIButton alloc] initWithFrame:CGRectMake(105, 230, 50, 50)];
    [_aButton5 setBackgroundColor:[UIColor yellowColor]];
    [_aButton5 setTitle:@"5" forState:UIControlStateNormal];
    [_aButton5 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [_aButton5 addTarget:self action:@selector(buttonAction5) forControlEvents:UIControlEventTouchUpInside];
    
    _aButton5.layer.borderWidth = 2;
    _aButton5.layer.borderColor = [UIColor blackColor].CGColor;
    
    [self.window addSubview:_aButton5];

    self.aButton6 = [[UIButton alloc] initWithFrame:CGRectMake(170, 230, 50, 50)];
    [_aButton6 setBackgroundColor:[UIColor yellowColor]];
    [_aButton6 setTitle:@"6" forState:UIControlStateNormal];
    [_aButton6 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [_aButton6 addTarget:self action:@selector(buttonAction6) forControlEvents:UIControlEventTouchUpInside];
    
    _aButton6.layer.borderWidth = 2;
    _aButton6.layer.borderColor = [UIColor blackColor].CGColor;
    
    [self.window addSubview:_aButton6];

    self.aButton7 = [[UIButton alloc] initWithFrame:CGRectMake(40, 310, 50, 50)];
    [_aButton7 setBackgroundColor:[UIColor yellowColor]];
    [_aButton7 setTitle:@"7" forState:UIControlStateNormal];
    [_aButton7 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [_aButton7 addTarget:self action:@selector(buttonAction7) forControlEvents:UIControlEventTouchUpInside];
    
    _aButton7.layer.borderWidth = 2;
    _aButton7.layer.borderColor = [UIColor blackColor].CGColor;
    
    [self.window addSubview:_aButton7];

    self.aButton8 = [[UIButton alloc] initWithFrame:CGRectMake(105, 310, 50, 50)];
    [_aButton8 setBackgroundColor:[UIColor yellowColor]];
    [_aButton8 setTitle:@"8" forState:UIControlStateNormal];
    [_aButton8 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [_aButton8 addTarget:self action:@selector(buttonAction8) forControlEvents:UIControlEventTouchUpInside];
    
    _aButton8.layer.borderWidth = 2;
    _aButton8.layer.borderColor = [UIColor blackColor].CGColor;
    
    [self.window addSubview:_aButton8];

    self.aButton9 = [[UIButton alloc] initWithFrame:CGRectMake(170, 310, 50, 50)];
    [_aButton9 setBackgroundColor:[UIColor yellowColor]];
    [_aButton9 setTitle:@"9" forState:UIControlStateNormal];
    [_aButton9 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [_aButton9 addTarget:self action:@selector(buttonAction9) forControlEvents:UIControlEventTouchUpInside];
    
    _aButton9.layer.borderWidth = 2;
    _aButton9.layer.borderColor = [UIColor blackColor].CGColor;
    
    [self.window addSubview:_aButton9];

    self.aButton0 = [[UIButton alloc] initWithFrame:CGRectMake(105, 390, 50, 50)];
    [_aButton0 setBackgroundColor:[UIColor yellowColor]];
    [_aButton0 setTitle:@"0" forState:UIControlStateNormal];
    [_aButton0 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [_aButton0 addTarget:self action:@selector(buttonAction0) forControlEvents:UIControlEventTouchUpInside];
    
    _aButton0.layer.borderWidth = 2;
    _aButton0.layer.borderColor = [UIColor blackColor].CGColor;
    
    [self.window addSubview:_aButton0];
    
    UIButton * cButton = [[UIButton alloc] initWithFrame:CGRectMake(40, 390, 50, 50)];
    [cButton setBackgroundColor:[UIColor yellowColor]];
    [cButton setTitle:@"c" forState:UIControlStateNormal];
    [cButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [cButton addTarget:self action:@selector(cAction) forControlEvents:UIControlEventTouchUpInside];
    
    cButton.layer.borderWidth = 2;
    cButton.layer.borderColor = [UIColor blackColor].CGColor;
    
    [self.window addSubview:cButton];
    
    
    self.addButton = [[UIButton alloc] initWithFrame:CGRectMake(235, 150, 50, 50)];
    [_addButton setBackgroundColor:[UIColor yellowColor]];
    [_addButton setTitle:@"+" forState:UIControlStateNormal];
    [_addButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [_addButton addTarget:self action:@selector(addAction) forControlEvents:UIControlEventTouchUpInside];
    
    _addButton.layer.borderWidth = 2;
    _addButton.layer.borderColor = [UIColor blackColor].CGColor;
    
    [self.window addSubview:_addButton];
    
    self.minButton = [[UIButton alloc] initWithFrame:CGRectMake(235, 230, 50, 50)];
    [_minButton setBackgroundColor:[UIColor yellowColor]];
    [_minButton setTitle:@"-" forState:UIControlStateNormal];
    [_minButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [_minButton addTarget:self action:@selector(minAction) forControlEvents:UIControlEventTouchUpInside];
    
    _minButton.layer.borderWidth = 2;
    _minButton.layer.borderColor = [UIColor blackColor].CGColor;
    
    [self.window addSubview:_minButton];
    
    self.mulButton = [[UIButton alloc] initWithFrame:CGRectMake(235, 310, 50, 50)];
    [_mulButton setBackgroundColor:[UIColor yellowColor]];
    [_mulButton setTitle:@"*" forState:UIControlStateNormal];
    [_mulButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [_mulButton addTarget:self action:@selector(mulAction) forControlEvents:UIControlEventTouchUpInside];
    
    _mulButton.layer.borderWidth = 2;
    _mulButton.layer.borderColor = [UIColor blackColor].CGColor;
    
    [self.window addSubview:_mulButton];
    
    self.divButton = [[UIButton alloc] initWithFrame:CGRectMake(235, 390, 50, 50)];
    [_divButton setBackgroundColor:[UIColor yellowColor]];
    [_divButton setTitle:@"/" forState:UIControlStateNormal];
    [_divButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [_divButton addTarget:self action:@selector(divAction) forControlEvents:UIControlEventTouchUpInside];
    
    _divButton.layer.borderWidth = 2;
    _divButton.layer.borderColor = [UIColor blackColor].CGColor;
    
    [self.window addSubview:_divButton];
    
    UIButton * equalButton = [[UIButton alloc] initWithFrame:CGRectMake(170, 390, 50, 50)];
    [equalButton setBackgroundColor:[UIColor yellowColor]];
    [equalButton setTitle:@"=" forState:UIControlStateNormal];
    [equalButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [equalButton addTarget:self action:@selector(equalAction) forControlEvents:UIControlEventTouchUpInside];

    equalButton.layer.borderWidth = 2;
    equalButton.layer.borderColor = [UIColor blackColor].CGColor;
    
    [self.window addSubview:equalButton];


}

- (void) buttonAction
{
    NSString * value = self.aButton.currentTitle;
    
    if (_lastEnterType) {
//        [_label setText:@""];
        [_label setText:value];
        
        _lastEnterType = NO;
    
    }else{
        
        if ([_label.text length] == 0) {
            [_label setText:@""];
        }
        
        NSString * endValue = [NSString stringWithFormat:@"%@%@",_label.text,value];
        [_label setText:endValue];
    }
}

- (void) buttonAction2
{
    NSString * value2 = self.aButton2.currentTitle;
    
    if (_lastEnterType) {
        [_label setText:value2];
        
        _lastEnterType = NO;
        
    }else{
        if ([_label.text length] == 0) {
            [_label setText:@""];
        }
        NSString * endValue = [NSString stringWithFormat:@"%@%@",_label.text,value2];
        [_label setText:endValue];
    }
}

- (void) buttonAction3
{
    NSString * value3 = self.aButton3.currentTitle;
    
    if (_lastEnterType) {
        [_label setText:value3];
        
        _lastEnterType = NO;
        
    }else{
        if ([_label.text length] == 0) {
            [_label setText:@""];
        }
        NSString * endValue = [NSString stringWithFormat:@"%@%@",_label.text,value3];
        [_label setText:endValue];
    }
}

- (void) buttonAction4
{
    NSString * value4 = self.aButton4.currentTitle;
    
    if (_lastEnterType) {
        [_label setText:value4];
        
        _lastEnterType = NO;
        
    }else{
        if ([_label.text length] == 0) {
            [_label setText:@""];
        }
        NSString * endValue = [NSString stringWithFormat:@"%@%@",_label.text,value4];
        [_label setText:endValue];
    }
}

- (void) buttonAction5
{
    NSString * value5 = self.aButton5.currentTitle;
    
    if (_lastEnterType) {
        [_label setText:value5];
        
        _lastEnterType = NO;
        
    }else{
        if ([_label.text length] == 0) {
            [_label setText:@""];
        }
        NSString * endValue = [NSString stringWithFormat:@"%@%@",_label.text,value5];
        [_label setText:endValue];
    }
}

- (void) buttonAction6
{
    NSString * value6 = self.aButton6.currentTitle;
    
    if (_lastEnterType) {
        [_label setText:value6];
        
        _lastEnterType = NO;
        
    }else{
        if ([_label.text length] == 0) {
            [_label setText:@""];
        }
        NSString * endValue = [NSString stringWithFormat:@"%@%@",_label.text,value6];
        [_label setText:endValue];
    }
}

- (void) buttonAction7
{
    NSString * value7 = self.aButton7.currentTitle;
    
    if (_lastEnterType) {
        [_label setText:value7];
        
        _lastEnterType = NO;
        
    }else{
        if ([_label.text length] == 0) {
            [_label setText:@""];
        }
        NSString * endValue = [NSString stringWithFormat:@"%@%@",_label.text,value7];
        [_label setText:endValue];
    }
}

- (void) buttonAction8
{
    NSString * value8 = self.aButton8.currentTitle;
    
    if (_lastEnterType) {
        [_label setText:value8];
        
        _lastEnterType = NO;
        
    }else{
        if ([_label.text length] == 0) {
            [_label setText:@""];
        }
        NSString * endValue = [NSString stringWithFormat:@"%@%@",_label.text,value8];
        [_label setText:endValue];
    }
}

- (void) buttonAction9
{
    NSString * value9 = self.aButton9.currentTitle;
    
    if (_lastEnterType) {
        [_label setText:value9];
        
        _lastEnterType = NO;
        
    }else{
        if ([_label.text length] == 0) {
            [_label setText:@""];
        }
        NSString * endValue = [NSString stringWithFormat:@"%@%@",_label.text,value9];
        [_label setText:endValue];
    }
}

- (void) buttonAction0
{
    NSString * value0 = self.aButton0.currentTitle;
    
    if (_lastEnterType) {
        [_label setText:value0];
        
        _lastEnterType = NO;
        
    }else{
        if ([_label.text length] == 0) {
            [_label setText:@""];
        }
        NSString * endValue = [NSString stringWithFormat:@"%@%@",_label.text,value0];
        [_label setText:endValue];
    }
}

- (void) addAction
{
    NSString * current = _label.text;
    
    //把NSString类型的字符串转换为CGFloat 类型的
    _prefixValue = [current floatValue];
    
    _char = _addButton.titleLabel.text;
    
    _lastEnterType = YES;
}

- (void) minAction
{
    NSString * current = _label.text;
    
    _prefixValue = [current floatValue];
    
    _char = _minButton.titleLabel.text;
    
    _lastEnterType = YES;
}

- (void) mulAction
{
    NSString * current = _label.text;
    
    _prefixValue = [current floatValue];
    
    _char = _mulButton.titleLabel.text;
    
    _lastEnterType = YES;
}

- (void) divAction
{
    NSString * current = _label.text;
    
    _prefixValue = [current floatValue];
    
    _char = _divButton.titleLabel.text;
    
    _lastEnterType = YES;
}

- (void) equalAction
{
    //存储第一次输入的值
    NSString * current = _label.text;
    //把字符串类型转换为float类型
    CGFloat currentValue = [current floatValue];
//    //把前后输入的两个数想加
//    CGFloat endValue = _prefixValue + currentValue;
    
    CGFloat endValue = 0;
    
    if ([_char isEqualToString:@"+"]){
        endValue = _prefixValue + currentValue;
    }else if([_char isEqualToString:@"-"]){
        endValue = _prefixValue - currentValue;
    }else if ([_char isEqualToString:@"*"]){
        endValue = _prefixValue * currentValue;
    }else{
        endValue = _prefixValue / currentValue;
        NSLog(@"aaa");
    }
    
    NSString * endStr = [NSString stringWithFormat:@"%.2f",endValue];
    
    [_label setText:endStr];
    
    _lastEnterType = YES;
}

- (void) cAction
{
    [_label setText:@""];
}



- (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:.
}

@end



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值