AppDelegate.h

#import 
@class CustomButton;//在.h引入头文件全部用@class,@class作用是声明有这么一个类enum fuhaomeiju { jia, jian, cheng, chu, kong};@interface AppDelegate : UIResponder
{ CGFloat _perfixValue; //perfixValue:上次所输入的内容 BOOL _lastEnterType; //上次所输入的类型,YES代表操作符,NO代表数字}@property (nonatomic,assign) int fuhao;@property (strong, nonatomic) UIWindow * window;@property (retain,nonatomic) UILabel * label;@end

AppDelegate.m

#import "AppDelegate.h"#import "CustomButton.h"@implementation AppDelegate- (void)dealloc{    [_window release];    _window = nil;    [self.label release];    _label = nil;                           [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 grayColor];    [self.window makeKeyAndVisible];                           _fuhao = kong;                           [self Calculator];    return YES;}- (void)Calculator{    _lastEnterType = NO;//设置默认初始值为NO;在整个程序开始的时候写                           //显示框    self.label = [[UILabel alloc]initWithFrame:CGRectMake(20, 30, 280, 50) ];    [_label setBackgroundColor:[UIColor whiteColor]];    [_label setText:@" "];//开头显示null解决办法1    self.label.layer.borderWidth =4;//边框宽度=4;layer属于UIView    self.label.layer.borderColor = [UIColor redColor].CGColor;//边框颜色红色    UIFont * font = [UIFont systemFontOfSize:30];    [self.label setFont:font];    [self.window addSubview:self.label];    [self.label release];    //1    CustomButton * button = [[CustomButton alloc]initWithFrame:CGRectMake(20, 100, 60, 60) name:@"1"];    [button addTarget:self action:@selector(buttonAciton:) forControlEvents:UIControlEventTouchUpInside];    [self.window addSubview:button];                           //2    CustomButton * button3 = [[CustomButton alloc]initWithFrame:CGRectMake(95, 100, 60, 60) name:@"2"];    [button3 addTarget:self action:@selector(buttonAciton:) forControlEvents:UIControlEventTouchUpInside];    [self.window addSubview:button3];    //3    CustomButton * button4 = [[CustomButton alloc]initWithFrame:CGRectMake(170, 100, 60, 60) name:@"3"];    [button4 addTarget:self action:@selector(buttonAciton:) forControlEvents:UIControlEventTouchUpInside];    [self.window addSubview:button4];    //4    CustomButton * button5 = [[CustomButton alloc]initWithFrame:CGRectMake(20, 180, 60, 60) name:@"4"];    [button5 addTarget:self action:@selector(buttonAciton:) forControlEvents:UIControlEventTouchUpInside];    [self.window addSubview:button5];    //5    CustomButton * button6 = [[CustomButton alloc]initWithFrame:CGRectMake(95, 180, 60, 60) name:@"5"];    [button6 addTarget:self action:@selector(buttonAciton:) forControlEvents:UIControlEventTouchUpInside];    [self.window addSubview:button6];    //6    CustomButton * button7 = [[CustomButton alloc]initWithFrame:CGRectMake(170, 180, 60, 60) name:@"6"];    [button7 addTarget:self action:@selector(buttonAciton:) forControlEvents:UIControlEventTouchUpInside];    [self.window addSubview:button7];    //7    CustomButton * button8 = [[CustomButton alloc]initWithFrame:CGRectMake(20, 260, 60, 60) name:@"7"];    [button8 addTarget:self action:@selector(buttonAciton:) forControlEvents:UIControlEventTouchUpInside];    [self.window addSubview:button8];    //8    CustomButton * button9 = [[CustomButton alloc]initWithFrame:CGRectMake(95, 260, 60, 60) name:@"8"];    [button9 addTarget:self action:@selector(buttonAciton:) forControlEvents:UIControlEventTouchUpInside];    [self.window addSubview:button9];    //9    CustomButton * button0 = [[CustomButton alloc]initWithFrame:CGRectMake(170, 260, 60, 60) name:@"9"];    [button0 addTarget:self action:@selector(buttonAciton:) forControlEvents:UIControlEventTouchUpInside];    [self.window addSubview:button0];    //0    CustomButton * button11 = [[CustomButton alloc]initWithFrame:CGRectMake(20, 340, 60, 60) name:@"0"];    [button11 addTarget:self action:@selector(buttonAction0:) forControlEvents:UIControlEventTouchUpInside];    button11.layer.borderWidth = 3;    button11.layer.borderColor=[UIColor cyanColor].CGColor;    [self.window addSubview:button11];    //C    CustomButton * c = [[CustomButton alloc]initWithFrame:CGRectMake(95, 340, 60, 60) name:@"C"];    [c addTarget:self action:@selector(buttonActionC)forControlEvents:UIControlEventTouchUpInside];    c.layer.borderWidth = 3;    c.layer.borderColor=[UIColor yellowColor].CGColor;    [self.window addSubview:c];    //+    CustomButton * button1 = [[CustomButton alloc]initWithFrame:CGRectMake(240, 100, 60, 60) name:@"+"];                           [button1 addTarget:self action:@selector(addAction:) forControlEvents:UIControlEventTouchUpInside];    button1.layer.borderWidth = 3;    button1.layer.borderColor=[UIColor blueColor].CGColor;    [self.window addSubview:button1];    //-    CustomButton * button12 = [[CustomButton alloc]initWithFrame:CGRectMake(240, 180, 60, 60) name:@"-"];                           [button12 addTarget:self action:@selector(addAction:) forControlEvents:UIControlEventTouchUpInside];    button12.layer.borderWidth = 3;    button12.layer.borderColor=[UIColor blueColor].CGColor;    [self.window addSubview:button12];    //*    CustomButton * button13 = [[CustomButton alloc]initWithFrame:CGRectMake(240, 260, 60, 60) name:@"*"];                           [button13 addTarget:self action:@selector(addAction:) forControlEvents:UIControlEventTouchUpInside];    button13.layer.borderWidth = 3;    button13.layer.borderColor=[UIColor blueColor].CGColor;    [self.window addSubview:button13];    //"/"    CustomButton * button14 = [[CustomButton alloc]initWithFrame:CGRectMake(240, 340, 60, 60) name:@"/"];                           [button14 addTarget:self action:@selector(addAction:) forControlEvents:UIControlEventTouchUpInside];    button14.layer.borderWidth = 3;    button14.layer.borderColor=[UIColor blueColor].CGColor;    [self.window addSubview:button14];                                                  //=    CustomButton * button2 = [[CustomButton alloc]initWithFrame:CGRectMake(170, 340, 60, 60) name:@"="];                           [button2 addTarget:self action:@selector(equaAction) forControlEvents:UIControlEventTouchUpInside];    button2.layer.borderWidth = 3;    button2.layer.borderColor=[UIColor orangeColor].CGColor;    [self.window addSubview:button2];                               }- (void)buttonAciton:(CustomButton *)button//形参{                           NSString * value = button.currentTitle;                           if (_lastEnterType) {        [self.label setText:@""];//如果输入的是数字label清空        [self.label setText:value];//值赋给value        _lastEnterType = NO;    }else{                                   if ([_label.text length] == 0) {//开头显示null解决办法2            [_label setText:@""];        }        NSString * endValue = [NSString stringWithFormat:@"%@%@",_label.text,value];//拼接字符串        [_label setText:endValue];                               }}- (void)buttonAction0:(CustomButton *)button0{    NSString * value = button0.currentTitle;    if (_lastEnterType) {        [_label setText:value];        _lastEnterType = NO;    }else{        if (_label.text == nil) {            [_label setText:@"0"];        }        NSString * endValue = [NSString stringWithFormat:@"%@%@",_label.text,value];        [_label setText:endValue];    }}- (void)buttonActionC{    _label.text = @"";    _lastEnterType = YES;}//+- (void)addAction:(CustomButton *)button{    NSString * current1 = _label.text;//保存屏幕上的内容                           _perfixValue = [current1 floatValue];//把NSString的类型转换成float类型;                           _lastEnterType = YES;    if ([button.currentTitle isEqualToString:@"+"]) {        _fuhao = jia;    }    if ([button.currentTitle isEqualToString:@"-"]) {        _fuhao = jian;    }    if ([button.currentTitle isEqualToString:@"*"]) {        _fuhao = cheng;    }    if ([button.currentTitle isEqualToString:@"/"]) {        _fuhao = chu;    }}// =- (void)equaAction{                           NSString * current = _label.text;    CGFloat currentValue = [current floatValue];//转换成float类型                           CGFloat endValue = 0;    switch (_fuhao) {        case jia:            endValue = _perfixValue + currentValue;            break;                                       case jian:            endValue = _perfixValue - currentValue;            break;                                       case cheng:            endValue = _perfixValue * currentValue;            break;                                       case chu:            endValue = _perfixValue / currentValue;            break;                                       default:            break;    }    NSString * endStr = [NSString stringWithFormat:@"%.3f",endValue];//把相加的值转换为float类型    [_label setText:endStr];//相加后的值赋给label    _lastEnterType = YES;//等号后清零                       }@end

CustomButton.h

#import 
@interface CustomButton : UIButton- (id)initWithFrame:(CGRect)frame name:(NSString *)name;@end

CustomButton.m

#import "CustomButton.h"@implementation CustomButton- (void)dealloc{    [super dealloc];}- (id)initWithFrame:(CGRect)frame name:(NSString *)name{    self = [super initWithFrame:frame];    if (self) {        // Initialization code        [self setTitle:name forState:UIControlStateNormal];                  [self setBackgroundColor:[UIColor whiteColor]];        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];        self.layer.borderWidth = 3;        self.layer.borderColor=[UIColor greenColor].CGColor;    }    return self;}@end