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

简易计算器制作

时间:2014-08-26 09:56:45      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:简易计算器

@interface EricAppDelegate ()
{
    UIView *_containView;
    UILabel *_label;
    UIButton *_butt;
    NSMutableString *str;
}
@property(assign,nonatomic)double num1,num2,num3,num4;

@end
@implementation EricAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    _containView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
    _containView.backgroundColor = [UIColor orangeColor];
    [self.window addSubview:_containView];
    [_containView release];
    // Override point for customization after application launch.
    [self calculator];
    
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

//计算器
- (void)calculator
{
    NSArray *arr = [NSArray arrayWithObjects:@"7",@"8", @"9",@"/",@"4",@"5",@"6",@"*",@"1",@"2",@"3",@"-",@".",@"0",@"=",@"+",nil];
    str = [[NSMutableString alloc] initWithFormat:@""];
    int n = 0;
    for (int i = 0; i < 4; i ++) {
        for (int j = 0; j < 4; j++){
            _butt = [UIButton buttonWithType:UIButtonTypeSystem];
            _butt.frame = CGRectMake(3 + j *80, 270 + i *75, 75, 70);
            _butt.backgroundColor = [UIColor grayColor];
            _butt.layer.cornerRadius = 20;
            _butt.titleLabel.font = [UIFont systemFontOfSize:35];
            [_butt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
            [_butt setTitle:arr[n++ ] forState:UIControlStateNormal];
            _butt.tag = 100 + n;
            [_containView addSubview:_butt];
        }
    }
    //显示窗口
    _label = [[UILabel alloc] initWithFrame:CGRectMake(3, 80, 313, 100)];
    _label.layer.cornerRadius = 5;
    _label.layer.masksToBounds= YES;
    _label.textAlignment= NSTextAlignmentRight;
    _label.textColor = [UIColor blackColor];
    _label.font = [UIFont systemFontOfSize:30];
    _label.backgroundColor = [UIColor greenColor];
    [_containView addSubview:_label];
    [_label release];
    //清除按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(243, 193, 75, 70);
    button.backgroundColor = [UIColor grayColor];
    button.layer.cornerRadius = 20;
    button.titleLabel.font = [UIFont systemFontOfSize:25];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [button setTitle:@"C" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clear:) forControlEvents:UIControlEventTouchUpInside];
    [_containView addSubview:button];
    //回删按钮
    UIButton *goButton = [UIButton buttonWithType:UIButtonTypeSystem];
    goButton.frame = CGRectMake(163, 193, 75, 70);
    goButton.backgroundColor = [UIColor grayColor];
    goButton.layer.cornerRadius = 20;
    goButton.titleLabel.font = [UIFont systemFontOfSize:25];
    [goButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [goButton setTitle:@"Dele" forState:UIControlStateNormal];
    [goButton addTarget:self action:@selector(delete:) forControlEvents:UIControlEventTouchUpInside];
    [_containView addSubview:goButton];

    
    //数字按钮
    NSArray *arra = [NSArray arrayWithObjects:(UIButton *) [_containView viewWithTag:101],(UIButton *) [_containView viewWithTag:102],(UIButton *) [_containView viewWithTag:103],(UIButton *) [_containView viewWithTag:105],(UIButton *) [_containView viewWithTag:106],(UIButton *) [_containView viewWithTag:107],(UIButton *) [_containView viewWithTag:109],(UIButton *) [_containView viewWithTag:110],(UIButton *) [_containView viewWithTag:111],(UIButton *) [_containView viewWithTag:114], nil];
    for (int i = 0; i < 10; i++) {
        [arra[i] addTarget:self action:@selector(number:) forControlEvents:UIControlEventTouchUpInside];
    }
    //运算符按钮
    NSArray *array = [NSArray arrayWithObjects:(UIButton *) [_containView viewWithTag:104],(UIButton *) [_containView viewWithTag:108],(UIButton *) [_containView viewWithTag:112],(UIButton *) [_containView viewWithTag:116], nil];
    for (int i = 0; i < 4; i++) {
        [array[i] addTarget:self action:@selector(operator:) forControlEvents:UIControlEventTouchUpInside];
    }
  //运算结果
    UIButton *but =(UIButton *) [_containView viewWithTag:115];
    [but addTarget:self action:@selector(cal:) forControlEvents:UIControlEventTouchUpInside];
    self.num1= [str doubleValue];
    //点运算
    UIButton *butt =(UIButton *) [_containView viewWithTag:113];
    [butt addTarget:self action:@selector(number:) forControlEvents:UIControlEventTouchUpInside];
    [str setString:@""];
}

- (void)clear:(UIButton *)clear
{
       _label.text = @"0";
    [str setString:@""];
    self.num3=0;
}
- (void)delete:(UIButton *)del
{
    if ([str length] !=0) {
        [str deleteCharactersInRange:NSMakeRange([str length] - 1, 1)];
    }
    _label.text = str;
}
//计算操作


-(void)number:(UIButton *)but
    {
        //保证是符号时在输入数字时隐藏
        if ([but.currentTitle hasPrefix:@"+"]||[but.currentTitle hasPrefix:@"-"]||[but.currentTitle hasPrefix:@"*"]||[but.currentTitle hasPrefix:@"/"])//判断是否为运算符
        {
            _label.text = @" ";
           [str setString:@""];//字符串清零
        }else{
            //判断第一次输入的是否是.  , 若是,则添加个0,
            if ([str isEqualToString:@""] &&[but.currentTitle isEqualToString:@"."]) {
                [str appendFormat:@"0%@",but.currentTitle];
            }else{
                [str  appendFormat: @"%@",but.currentTitle];
            };
        }
        _label.text =str;
        self.num1 = [str doubleValue];
    }

- (void)operator:(UIButton *)but
{
    [str setString:@""];
      if ([but.currentTitle hasPrefix:@"+"]){
          self.num2 = self.num1;
          self.num4 = 1;
          }
    if ([but.currentTitle hasPrefix:@"-"]){
        self.num2 = self.num1;
        self.num4 = 2;
    }
    if ([but.currentTitle hasPrefix:@"*"]){
        self.num2 = self.num1;
        self.num4 = 3;
    }
    if ([but.currentTitle hasPrefix:@"/"]){
        self.num2 = self.num1;
        self.num4 = 4;
    }
}
- (void)cal:(id)sender
{
    if (self.num4 ==1) {
      self.num3=  self.num1 + self.num2;
        _label.text = [NSString stringWithFormat:@"%g",self.num3];
        self.num1 = self.num3;
        [str setString:@""];
    }
    if (self.num4 ==2) {
        self.num3=  self.num2 - self.num1;
        _label.text = [NSString stringWithFormat:@"%g",self.num3];
        self.num1 = self.num3;
        [str setString:@""];
    }
    if (self.num4 ==3) {
        self.num3=  self.num1 * self.num2;
        _label.text = [NSString stringWithFormat:@"%g",self.num3];
        self.num1 = self.num3;
        [str setString:@""];
    }
    if (self.num4 ==4) {
        self.num3=  self.num2 / self.num1;
        _label.text = [NSString stringWithFormat:@"%g",self.num3];
        self.num1 = self.num3;
        [str setString:@""];
    }
    
}
bubuko.com,布布扣

简易计算器制作

标签:简易计算器

原文地址:http://blog.csdn.net/xiaomeng_os/article/details/38827743

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