计算器的实现代码

6 篇文章 0 订阅
4 篇文章 0 订阅

#import "AppDelegate.h"



@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Override point for customization after application launch.

    UIView *containerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];

    containerView.backgroundColor = [UIColor blackColor];

    [self.window addSubview:containerView];

    [containerView release];

    self.currentFirst = @"";

    

    //显示数字

    self.display = [[UILabel alloc]initWithFrame:CGRectMake(0, 80, 320, 63)];

    self.display.backgroundColor = [UIColor whiteColor];

    [containerView addSubview:self.display];

    [self.display release];

    self.display.text = @"";

    //回删键

    UIButton *space = [UIButton buttonWithType:UIButtonTypeSystem];

    space.frame = CGRectMake(0, 163, 235, 65);

    space.backgroundColor = [UIColor grayColor];

    space.layer.cornerRadius = 10;

    [space setTitle:@"回删" forState:UIControlStateNormal];

    [containerView addSubview:space];

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

    

    //删除键

    UIButton *delete = [UIButton buttonWithType:UIButtonTypeSystem];

    delete.frame = CGRectMake(255,163 , 65, 65);

    delete.backgroundColor = [UIColor grayColor];

    [delete setTitle:@"删除" forState:UIControlStateNormal];

    delete.titleLabel.font = [UIFont systemFontOfSize:20];

    delete.layer.cornerRadius = 10;

    [containerView addSubview:delete];

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

    

    NSArray * buttonDisplay = @[@"7",@"8",@"9",@"/",@"4",@"5",@"6",@"*",@"1",@"2",@"3",@"-",@".",@"0",@"=",@"+"];

    int k = 0;

    for (int i = 0; i < 4; i++) {

        for (int j = 0; j < 4; j++) {

            self.button = [UIButton buttonWithType:UIButtonTypeSystem];

            self.button.frame = CGRectMake(85 * j, 248 + 85 * i, 65, 65);

            self.button.backgroundColor = [UIColor grayColor];

            self.button.layer.cornerRadius = 10;

            self.button.layer.borderColor = [UIColor orangeColor].CGColor;

            

            self.button.titleLabel.font = [UIFont systemFontOfSize:30];

            

            [self.button setTitle:buttonDisplay[k] forState:UIControlStateNormal];

            [containerView addSubview:self.button];

            [self.button addTarget:self action:@selector(display:) forControlEvents:UIControlEventTouchUpInside];

            [self.button addTarget:self action:@selector(currentSymbol:) forControlEvents:UIControlEventTouchUpInside];

            [self.button addTarget:self action:@selector(calculate:) forControlEvents:UIControlEventTouchUpInside];

            

            

            k++;

            

        }

    }

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    return YES;

}


//获取当前值和前一个值


- (void)display:(UIButton *)button

{

    NSArray *arry = @[@"7",@"8",@"9",@"4",@"5",@"6",@"1",@"2",@"3",@".",@"0"];

    for (int i = 0; i < 11; i++) {

        if ([button.titleLabel.text  isEqualToString:@"." ])  {

            if ([self.display.text  length] == 0) {

                self.display.text = [@"0" stringByAppendingString:button.titleLabel.text];

                self.display.text = [self.display.text substringToIndex:[self.display.text length] - 1];

                

            }else{

                int k = 0;

                for (int i = 0; i < [self.display.text length]; i++)

                {

                    

                    if ([self.display.text characterAtIndex:i] == '.') {

                        k++;

                    }

                    

                }

                if (k > 0) {

                    ;

                }else{

                    self.display.text = [self.display.text stringByAppendingString:button.titleLabel.text];

                }

            }

        }

        else if ([self.display.text  isEqualToString:@"0" ] && [button.titleLabel.text compare:@"."] != 0)

        {

            self.display.text = button.titleLabel.text;

           self.display.text = [self.display.text substringToIndex:[self.display.text length] - 1];

        }

        else

        {

            if ([arry[i] isEqualToString:button.titleLabel.text ]) {

                self.display.text = [self.display.text stringByAppendingString:button.titleLabel.text];

                    break;

                }

            }

        }

   

        

    

        

    

    

}


//获取当前符号

- (void)currentSymbol:(UIButton *)button

{

    NSArray *arry = @[@"+",@"-",@"*",@"/"];

    for (int i = 0; i < 4; i++) {

        if ([button.titleLabel.text isEqualToString:arry[i]]) {

            self.currentSymbol = button.titleLabel.text;

            self.currentSecond = self.display.text;

            self.display.text = @"";

            break;

        }

        

    }

}

//计算

-(void)calculate:(UIButton *)button

{

    if ([button.titleLabel.text isEqualToString:@"="]) {

        

        if ([self.currentSymbol isEqualToString:@"/"]) {

            self.currentFirst = _display.text;

            float a = [self.currentSecond floatValue] / [self.currentFirst floatValue];

            self.display.text = [NSString stringWithFormat:@"%g",a];

            

        }

        

        else if ([self.currentSymbol isEqualToString:@"*"]) {

            self.currentFirst = self.display.text;

            double a = [self.currentSecond floatValue] * [self.currentFirst floatValue];

            self.display.text = [NSString stringWithFormat:@"%g",a];

            

        }

        else if ([self.currentSymbol isEqualToString:@"-"]) {

            self.currentFirst = _display.text;

            float a = [self.currentSecond floatValue] - [self.currentFirst floatValue];

            self.display.text = [NSString stringWithFormat:@"%g",a];

        }

        else if ([self.currentSymbol isEqualToString:@"+"]) {

            self.currentFirst = self.display.text;

            

            float a = [self.currentSecond floatValue] + [self.currentFirst floatValue];

            self.display.text = [NSString stringWithFormat:@"%g",a];

        }

        

    }

    

}

//回删

- (void)deleteOne:(UIButton *)button

{

    if ([self.display.text length] > 0) {

        self.display.text = [self.display.text substringToIndex:[self.display.text length] - 1];

    }

    

}

//删除

- (void)deletText:(UIButton *)button

{

    self.display.text = @"";

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值