码迷,mamicode.com
首页 > 移动开发 > 详细

iOS之计算器

时间:2015-07-20 23:20:17      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

//

//  zyAppDelegate.h

//  Calculator

//

//  Created by nimami on 15/7/20.

//  Copyright (c) 2015年 zhiyou. All rights reserved.

//

 

#import <UIKit/UIKit.h>

 

@interface zyAppDelegate : UIResponder <UIApplicationDelegate>

 

{

    int a,b,c;

    NSString * a1;

    UILabel * _TF;

}

 

@property (strong, nonatomic) UIWindow *window;

 

@end

 

 

 

 

 

 

 

 

 

 

 

 

//

//  zyAppDelegate.m

//  Calculator

//

//  Created by nimami on 15/7/20.

//  Copyright (c) 2015年 zhiyou. All rights reserved.

//

 

#import "zyAppDelegate.h"

 

@implementation zyAppDelegate

 

- (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];

    UIImageView * view = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, 320, 480)];

    view.image = [UIImage imageNamed:@"3.jpg"];

    [self.window addSubview:view];

   

    

    //标题

    UILabel * label0 = [[UILabel alloc]initWithFrame:CGRectMake(130, 70, 150, 30)];

    label0.text = @"计算器";

    [self.window addSubview:label0];

    

    

    

   //用于显示的label

    _TF = [[UILabel alloc]initWithFrame:CGRectMake(60, 130, 190, 30)];

    _TF.backgroundColor = [UIColor lightGrayColor];

    [self.window addSubview:_TF];

    

    

    //按钮

    UIButton * button1 = [[UIButton alloc]initWithFrame:CGRectMake(60, 180, 40, 40)];

    button1.backgroundColor = [UIColor purpleColor];

    [button1 setTitle:@"1" forState:UIControlStateNormal];

    [button1 addTarget:self action:@selector(one) forControlEvents:UIControlEventTouchUpInside];

    [self.window addSubview:button1];

    

    

    UIButton * button2 = [[UIButton alloc]initWithFrame:CGRectMake(110, 180, 40, 40)];

    button2.backgroundColor = [UIColor purpleColor];

     [button2 setTitle:@"2" forState:UIControlStateNormal];

    [button2 addTarget:self action:@selector(two) forControlEvents:UIControlEventTouchUpInside];

    [self.window addSubview:button2];

    

    UIButton * button3 = [[UIButton alloc]initWithFrame:CGRectMake(160, 180, 40, 40)];

    button3.backgroundColor = [UIColor purpleColor];

     [button3 setTitle:@"3" forState:UIControlStateNormal];

    [button3 addTarget:self action:@selector(three) forControlEvents:UIControlEventTouchUpInside];

    [self.window addSubview:button3];

    

    UIButton * sum = [[UIButton alloc]initWithFrame:CGRectMake(210, 180, 40, 40)];

    sum.backgroundColor = [UIColor purpleColor];

     [sum setTitle:@"+" forState:UIControlStateNormal];

    [sum addTarget:self action:@selector(add) forControlEvents:UIControlEventTouchUpInside];

    [self.window addSubview:sum];

    

    UIButton * button4 = [[UIButton alloc]initWithFrame:CGRectMake(60, 230, 40, 40)];

    button4.backgroundColor = [UIColor purpleColor];

     [button4 setTitle:@"4" forState:UIControlStateNormal];

    [button4 addTarget:self action:@selector(four) forControlEvents:UIControlEventTouchUpInside];

    [self.window addSubview:button4];

    

    UIButton * button5 = [[UIButton alloc]initWithFrame:CGRectMake(110, 230, 40, 40)];

    button5.backgroundColor = [UIColor purpleColor];

     [button5 setTitle:@"5" forState:UIControlStateNormal];

    [button5 addTarget:self action:@selector(five) forControlEvents:UIControlEventTouchUpInside];

    [self.window addSubview:button5];

    

    UIButton * button6 = [[UIButton alloc]initWithFrame:CGRectMake(160, 230, 40, 40)];

    button6.backgroundColor = [UIColor purpleColor];

     [button6 setTitle:@"6" forState:UIControlStateNormal];

    [button6 addTarget:self action:@selector(six) forControlEvents:UIControlEventTouchUpInside];

    [self.window addSubview:button6];

    

    UIButton * sub = [[UIButton alloc]initWithFrame:CGRectMake(210, 230, 40, 40)];

    sub.backgroundColor = [UIColor purpleColor];

     [sub setTitle:@"-" forState:UIControlStateNormal];

    [sub addTarget:self action:@selector(sub) forControlEvents:UIControlEventTouchUpInside];

    [self.window addSubview:sub];

    

    

    

    

    UIButton * button7 = [[UIButton alloc]initWithFrame:CGRectMake(60,280 , 40, 40)];

    button7.backgroundColor = [UIColor purpleColor];

    [button7 setTitle:@"7" forState:UIControlStateNormal];

    [button7 addTarget:self action:@selector(seven) forControlEvents:UIControlEventTouchUpInside];

    [self.window addSubview:button7];

    

    UIButton * button8 = [[UIButton alloc]initWithFrame:CGRectMake(110, 280, 40, 40)];

    button8.backgroundColor = [UIColor purpleColor];

    [button8 setTitle:@"8" forState:UIControlStateNormal];

    [button8 addTarget:self action:@selector(eight) forControlEvents:UIControlEventTouchUpInside];

    [self.window addSubview:button8];

    

    UIButton * button9 = [[UIButton alloc]initWithFrame:CGRectMake(160, 280, 40, 40)];

    button9.backgroundColor = [UIColor purpleColor];

    [button9 setTitle:@"9" forState:UIControlStateNormal];

    [button9 addTarget:self action:@selector(nine) forControlEvents:UIControlEventTouchUpInside];

    [self.window addSubview:button9];

    

    UIButton * mul = [[UIButton alloc]initWithFrame:CGRectMake(210, 280, 40, 40)];

    mul.backgroundColor = [UIColor purpleColor];

    [mul setTitle:@"*" forState:UIControlStateNormal];

    [mul addTarget:self action:@selector(mul) forControlEvents:UIControlEventTouchUpInside];

    [self.window addSubview:mul];

    

    UIButton * button0 = [[UIButton alloc]initWithFrame:CGRectMake(60,330 , 40, 40)];

    button0.backgroundColor = [UIColor purpleColor];

    [button0 setTitle:@"0" forState:UIControlStateNormal];

    [button0 addTarget:self action:@selector(ling) forControlEvents:UIControlEventTouchUpInside];

    [self.window addSubview:button0];

    

    UIButton * buttonac = [[UIButton alloc]initWithFrame:CGRectMake(110, 330, 40, 40)];

    buttonac.backgroundColor = [UIColor purpleColor];

    [buttonac setTitle:@"AC" forState:UIControlStateNormal];

    [buttonac addTarget:self action:@selector(zero) forControlEvents:UIControlEventTouchUpInside];

    [self.window addSubview:buttonac];

    

    UIButton * buttonEqual = [[UIButton alloc]initWithFrame:CGRectMake(160, 330, 40, 40)];

    buttonEqual.backgroundColor = [UIColor purpleColor];

    [buttonEqual addTarget:self action:@selector(equal) forControlEvents:UIControlEventTouchUpInside];

    [buttonEqual setTitle:@"=" forState:UIControlStateNormal];

    [self.window addSubview:buttonEqual];

    

    UIButton * div = [[UIButton alloc]initWithFrame:CGRectMake(210, 330, 40, 40)];

    div.backgroundColor = [UIColor purpleColor];

    [div setTitle:@"/" forState:UIControlStateNormal];

    [div addTarget:self action:@selector(div) forControlEvents:UIControlEventTouchUpInside];

    [self.window addSubview:div];

    

 

    

    return YES;

}

 

 

 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    [self.window endEditing:YES];

}

 

//功能:将你点击的数字显示到label上;

-(void)ling

{

    a = a * 10 + 0;

    a1 = [NSString stringWithFormat:@"%d",a];

    _TF.text = a1;

}

 

-(void)one

{

    a = a * 10 +1;

    a1 = [NSString stringWithFormat:@"%d",a];

    _TF.text = a1;

}

-(void)two

{

    a = a * 10 +2;

    a1 = [NSString stringWithFormat:@"%d",a];

    _TF.text = a1;

}

-(void)three

{

    a = a * 10 +3;

    a1 = [NSString stringWithFormat:@"%d",a];

    _TF.text = a1;

}

-(void)four

{

    a = a * 10 +4;

    a1 = [NSString stringWithFormat:@"%d",a];

    _TF.text = a1;

}

-(void)five

{

    a = a * 10 +5;

    a1 = [NSString stringWithFormat:@"%d",a];

    _TF.text = a1;

}

-(void)six

{

    a = a * 10 +6;

    a1 = [NSString stringWithFormat:@"%d",a];

    _TF.text = a1;

}

-(void)seven

{

    a = a * 10 +7;

    a1 = [NSString stringWithFormat:@"%d",a];

    _TF.text = a1;

}

-(void)eight

{

    a = a * 10 +8;

    a1 = [NSString stringWithFormat:@"%d",a];

    _TF.text = a1;

}

-(void)nine

{

    a = a * 10 +9;

    a1 = [NSString stringWithFormat:@"%d",a];

    _TF.text = a1;

}

-(void)zero

{

    a = 0;

    a1 = [NSString stringWithFormat:@"%d",a];

    _TF.text = a1;

}

 

//把前一个数a赋值给b,然后把a清零,然后用来记录下一个数,c只用来标志

-(void)add

{

    c = 1;

    b =a;

    a = 0;

}

-(void)sub

{

    c = 2;

    b =a;

    a =0;

}

-(void)mul

{

    c = 3;

    b =a;

    a = 0;

}

-(void)div

{

    c = 4;

    b =a;

    a = 0;

}

 

-(void)equal

{

    if (c ==1)

    {

        a = a +b;

        a1 = [NSString stringWithFormat:@"%d",a];

        _TF.text = a1;

    }

    if (c ==2)

    {

        a = b - a;

        a1 = [NSString stringWithFormat:@"%d",a];

        _TF.text = a1;

    }

    if (c ==3)

    {

        a = a * b;

        a1 = [NSString stringWithFormat:@"%d",a];

        _TF.text = a1;

    }

    if (c ==4)

    {

        if (a != 0)

        {

            a = b / a;

            a1 = [NSString stringWithFormat:@"%d",a];

            _TF.text = a1;

        }

        else

        {

            NSLog(@"无法计算!");

            _TF.text = @"无法计算!";

        }

 

    }file:///Users/nimami/Desktop/屏幕快照%202015-07-20%20下午9.45.38.png

}

@end

 

技术分享

 

技术分享

iOS之计算器

标签:

原文地址:http://www.cnblogs.com/ios111/p/4662817.html

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