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

计算器的实现代码

时间:2014-08-24 14:13:32      阅读:234      评论:0      收藏: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 = @"";

}


计算器的实现代码

标签:计算器

原文地址:http://blog.csdn.net/lixiaoshuai150/article/details/38795707

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