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

简单计算器设计代码

时间:2015-10-17 13:22:42      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:

#import "AppDelegate.h"

 #define kScreenWidth CGRectGetWidth([[UIScreen mainScreen] bounds])

#define kScreenHeight CGRectGetHeight([[UIScreen mainScreen] bounds])

#define kButtonWidth ((kScreenWidth - 5) / 4)

#define kButtonHeight kButtonWidth 

#define kButtonOffsetX 1

#define kButtonOffsetY 1

#define kButtonOriginY (kScreenHeight - (kButtonHeight + kButtonOffsetY) * 5)

@interface AppDelegate ()

{

    BOOL _isHave;

    BOOL _isResult;

}

@property (retain,nonatomic)NSString *string;

@end

@implementation AppDelegate

-(void)dealloc{

    [_window release];

    [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 blackColor];

    [self.window makeKeyAndVisible]; 

    /*

     AC +- % /

     7  8  9 *

     4  5  6 -

     1  2  3 +

     0      .  =

     */

    NSArray *titles = @[@[@"AC", @"+-", @"baifenhao", @"chuhao"], @[@"7", @"8", @"9", @"chenghao"], @[@"4", @"5", @"6", @"jianhao"], @[@"1", @"2", @"3", @"jiahao"], @[@"0",@"", @"dian", @"dengyu"]];

    for (int i = 0; i < titles.count; i++) {

        NSArray *subTitles = titles[i];

        for (int j = 0; j < subTitles.count; j++) {

            NSString *title = subTitles[j];

            if ([title isEqualToString:@""]) {

                continue;

            }    

            UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];

            aButton.tag=100+(10*i)+j;

            CGRect frame = CGRectMake(kButtonOffsetX + (kButtonWidth + kButtonOffsetX) * j, kButtonOriginY + (kButtonHeight + kButtonOffsetY) * i, kButtonWidth, kButtonHeight);

            if ([title isEqualToString:@"0"]) {

                frame.size.width = frame.size.width * 2 + kButtonOffsetX;

            } 

            aButton.frame = frame;

            NSString *normalImageName = [subTitles[j] stringByAppendingString:@"_btn_nor"];

            NSString *highlightedImageName = [subTitles[j] stringByAppendingString:@"_btn_highlighted"];

            [aButton setImage:[UIImage imageNamed:normalImageName] forState:UIControlStateNormal];

            [aButton setImage:[UIImage imageNamed:highlightedImageName] forState:UIControlStateHighlighted];

            [aButton setTitle:title forState:UIControlStateNormal];  

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

            [self.window addSubview:aButton];           

        }

    }

    UITextField *aLabel=[[[UITextField alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-5*(kButtonHeight+kButtonOffsetY))]autorelease];

    aLabel.tag=1000;

    aLabel.backgroundColor=[UIColor blackColor];

    aLabel.clearsOnBeginEditing=YES;

    aLabel.enabled=NO;

    aLabel.placeholder=@"0";

    aLabel.textColor=[UIColor whiteColor];

    aLabel.font=[UIFont fontWithName:@"Times New Roman" size:70];

    aLabel.textAlignment=NSTextAlignmentRight;

    [_window addSubview:aLabel];

    return YES;

}

-(void)handleButtonAction:(UIButton *)abutton{

    UITextField *label=(UITextField *)[_window viewWithTag:1000];

    switch (abutton.tag) {

        case 100:{

            label.text=@"0";

            label.clearsOnBeginEditing=YES;

            break;

        }case 101:{

            float num=[label.text floatValue];

            NSNumber *number=[NSNumber numberWithFloat:-num];

            label.text=[NSString stringWithFormat:@"%@",number];

            break;

        }case 102:{

            float num=[label.text floatValue];

            NSNumber *number=[NSNumber numberWithFloat:(num/100)];

            label.text=[NSString stringWithFormat:@"%@",number];

            break;

        }case 110:

            case 111:

            case 112:

            case 120:

            case 121:

            case 122:

            case 130:

            case 131:

            case 132:

            case 140:

        case 142:{

            if (_isHave||[label.text isEqualToString:@""]||_isResult) {

                label.text=@"";

                label.text=[label.text stringByAppendingString:abutton.currentTitle];

            }else{label.text=[label.text stringByAppendingString:abutton.currentTitle];

            }

            _isHave=NO;

            _isResult=NO;

            break;

        }case 103:

            case 113:

            case 123:

        case 133:{

            self.string=[label.text stringByAppendingString:abutton.currentTitle];

            _isHave=YES;

            break;

        }case 143:{

            if ([self.string hasSuffix:@"jiahao"]) {

                float value1=[self.string floatValue];

                float value2=[label.text floatValue];

                NSNumber *number=[NSNumber numberWithFloat:(value1+value2)];

                label.text=[NSString stringWithFormat:@"%@",number];

                self.string=@"";

                _isResult=YES;

            }else if([self.string hasSuffix:@"jianhao"]) {

                float value1=[self.string floatValue];

                float value2=[label.text floatValue];

                NSNumber *number=[NSNumber numberWithFloat:(value1-value2)];

                label.text=[NSString stringWithFormat:@"%@",number];

                self.string=@"";

                _isResult=YES;

            }else if([_string hasSuffix:@"chenghao"]) {

                float value1=[self.string floatValue];

                float value2=[label.text floatValue];

                NSNumber *number=[NSNumber numberWithFloat:(value1*value2)];

                label.text=[NSString stringWithFormat:@"%@",number];

                self.string=@"";

                _isResult=YES;

            }else if([self.string hasSuffix:@"chuhao"]) {

                float value1=[self.string floatValue];

                float value2=[label.text floatValue];

                NSNumber *number=[NSNumber numberWithFloat:(value1/value2)];

                label.text=[NSString stringWithFormat:@"%@",number];

                self.string=@"";

                _isResult=YES;

            }

            break;

        }

        default:

            break;

    }

}

 

简单计算器设计代码

标签:

原文地址:http://www.cnblogs.com/sxsy-2015/p/4887226.html

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