类和文件

AppDelegate.m

#import "AppDelegate.h"
#import "CalculatorController.h"
//#import "ViewController.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.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    CalculatorController *calculator = [[CalculatorController alloc] init];
    self.window.rootViewController = calculator;
    [calculator release];
//    ViewController *view = [[ViewController alloc]init];
//    self.window.rootViewController = view;
    
    
    [_window release];
    return YES;
}
- (void)dealloc
{
    [_window release];
    [super dealloc];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end

CalculatorController.h

#import <UIKit/UIKit.h>
@interface CalculatorController : UIViewController
//@property (nonatomic , retain)UILabel *label;
//@property (nonatomic , assign)NSInteger firstNumber;
存储当前选择的符号(1:+ 2:- 3:* 4:/)
//@property (nonatomic , assign)NSInteger sumbol;
是不是刚刚点击过符号键
//@property (nonatomic , assign)BOOL isSymbol;
@property(retain,nonatomic)UIButton *button;
@property(retain,nonatomic)UILabel *label;
@property(nonatomic,retain)UILabel *label1;
@property(retain,nonatomic)NSMutableString *string;
@property(assign,nonatomic)double num1,num2,num3,num4;
@end

CalculatorController.m

#import "CalculatorController.h"
@interface CalculatorController ()
@end
@implementation CalculatorController
@synthesize button,label,label1,string,num1,num2,num3,num4;//string保存字符,显示数值。num1是存输入的数值,num2是存运算符前的数值,num3是运算结果,num4是判断进行何种运算
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.alpha = 0.3;
    // Do any additional setup after loading the view.
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20, 50, 280, 330)];
    view.layer.cornerRadius = 15;
    view.alpha = 0.8;
    view.backgroundColor = [UIColor brownColor];
    [self.view addSubview:view];
    [view release];
   
    
    self.label=[[UILabel alloc]initWithFrame:CGRectMake(40 , 75, 240, 30)];
    self.label.backgroundColor=[UIColor blackColor];//清空背景颜色
    self.label.textColor=[UIColor whiteColor];//字体颜色
    self.label.textAlignment =  NSTextAlignmentRight;;//字体居右
    self.label.font=[UIFont systemFontOfSize:20];
    [self.view addSubview:label];
    
    self.label1=[[UILabel alloc]initWithFrame:CGRectMake(40 , 60, 240, 15)];
    self.label1.text = @"";
    self.label1.backgroundColor=[UIColor blackColor];//清空背景颜色
    self.label1.textColor=[UIColor whiteColor];//字体颜色
    self.label1.textAlignment =  NSTextAlignmentRight;;//字体居右
    self.label1.font=[UIFont systemFontOfSize:15];
    [self.view addSubview:self.label1];
    
    
//    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20 , 20, 255, 35)];
//    label.text = @"";
//    label.textColor = [UIColor yellowColor];
//    label.font = [UIFont systemFontOfSize:18];
//    label.textAlignment = NSTextAlignmentCenter;
//    [self.view addSubview:label];
//    [label release];
    
//    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(30 , 70, 255, 35)];
//    textField.layer.borderWidth = 1;
//    textField.layer.cornerRadius = 10;
//    textField.backgroundColor = [UIColor whiteColor];
    textField.alpha = 0.2;
//    [self.view addSubview:textField];
//    [textField release];
    
    
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(30, 110, 260, 260)];
    view1.layer.cornerRadius = 15;
//    view1.alpha = 0.8;
    view1.backgroundColor = [UIColor blackColor];
    [self.view addSubview:view1];
    [view1 release];
    
    
    
    //C
    UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 55, 40)];
    [button1 setTitle:@"C" forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button1 setShowsTouchWhenHighlighted:YES];
    button1.backgroundColor = [UIColor orangeColor];
//    button1.alpha = 0.5;
    button1.layer.cornerRadius = 8;
    [button1 addTarget:self action:@selector(clean:) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:button1];
    [button1 release];
    //DEL
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    button2.frame = CGRectMake(70, 10, 55, 40);
    [button2 setTitle:@"DEL" forState:UIControlStateNormal];
    [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button2 setShowsTouchWhenHighlighted:YES];
    button2.backgroundColor = [UIColor orangeColor];
    button2.layer.cornerRadius = 8;
    [button2 addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:button2];
    //除号
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem];
    button3.frame = CGRectMake(130, 10, 55, 40);
    [button3 setTitle:@"/" forState:UIControlStateNormal];
    [button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button3 setShowsTouchWhenHighlighted:YES];
    button3.backgroundColor = [UIColor orangeColor];
    button3.layer.cornerRadius = 8;
    [button3 addTarget:self action:@selector(two:) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:button3];
    
    //*
    UIButton *button4 = [UIButton buttonWithType:UIButtonTypeSystem];
    button4.frame = CGRectMake(190, 10, 55, 40);
    [button4 setTitle:@"*" forState:UIControlStateNormal];
    [button4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button4 setShowsTouchWhenHighlighted:YES];
    button4.backgroundColor = [UIColor orangeColor];
    button4.layer.cornerRadius = 8;
    [button4 addTarget:self action:@selector(two:) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:button4];
    //7
    UIButton *button5 = [UIButton buttonWithType:UIButtonTypeSystem];
    button5.frame = CGRectMake(10 , 60, 55, 40);
    [button5 setTitle:@"7" forState:UIControlStateNormal];
    [button5 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button5 setShowsTouchWhenHighlighted:YES];
    button5.backgroundColor = [UIColor whiteColor];
//    button5.alpha = 0.3;
    button5.layer.cornerRadius = 8;
    [button5 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:button5];
    //8
    UIButton *button7 = [UIButton buttonWithType:UIButtonTypeSystem];
    button7.frame = CGRectMake(70 , 60, 55, 40);
    [button7 setTitle:@"8" forState:UIControlStateNormal];
    [button7 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button7 setShowsTouchWhenHighlighted:YES];
    button7.backgroundColor = [UIColor whiteColor];
    button7.layer.cornerRadius = 8;
    [button7 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:button7];
    //9
    UIButton *button8 = [UIButton buttonWithType:UIButtonTypeSystem];
    button8.frame = CGRectMake(130 , 60, 55, 40);
    [button8 setTitle:@"9" forState:UIControlStateNormal];
    [button8 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button8 setShowsTouchWhenHighlighted:YES];
    button8.backgroundColor = [UIColor whiteColor];
    button8.layer.cornerRadius = 8;
    [button8 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:button8];
    //-
    UIButton *button9 = [UIButton buttonWithType:UIButtonTypeSystem];
    button9.frame = CGRectMake(190 , 60, 55, 40);
    [button9 setTitle:@"-" forState:UIControlStateNormal];
    [button9 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button9 setShowsTouchWhenHighlighted:YES];
    button9.backgroundColor = [UIColor orangeColor];
    button9.layer.cornerRadius = 8;
    [button9 addTarget:self action:@selector(two:) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:button9];
    //4
    UIButton *button10 = [UIButton buttonWithType:UIButtonTypeSystem];
    button10.frame = CGRectMake(10 , 110, 55, 40);
    [button10 setTitle:@"4" forState:UIControlStateNormal];
    [button10 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button10 setShowsTouchWhenHighlighted:YES];
    button10.backgroundColor = [UIColor whiteColor];
    //    button5.alpha = 0.3;
    button10.layer.cornerRadius = 8;
    [button10 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:button10];
    //5
    UIButton *button11 = [UIButton buttonWithType:UIButtonTypeSystem];
    button11.frame = CGRectMake(70 , 110, 55, 40);
    [button11 setTitle:@"5" forState:UIControlStateNormal];
    [button11 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button11 setShowsTouchWhenHighlighted:YES];
    button11.backgroundColor = [UIColor whiteColor];
    button11.layer.cornerRadius = 8;
    [button11 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:button11];
    //6
    UIButton *button12 = [UIButton buttonWithType:UIButtonTypeSystem];
    button12.frame = CGRectMake(130 , 110, 55, 40);
    [button12 setTitle:@"6" forState:UIControlStateNormal];
    [button12 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button12 setShowsTouchWhenHighlighted:YES];
    button12.backgroundColor = [UIColor whiteColor];
    button12.layer.cornerRadius = 8;
    [button12 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:button12];
    
    //+
    UIButton *button13 = [UIButton buttonWithType:UIButtonTypeSystem];
    button13.frame = CGRectMake(190 , 110, 55, 40);
    [button13 setTitle:@"+" forState:UIControlStateNormal];
    [button13 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button13 setShowsTouchWhenHighlighted:YES];
    button13.backgroundColor = [UIColor orangeColor];
    button13.layer.cornerRadius = 8;
    [button13 addTarget:self action:@selector(two:) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:button13];
    
    
    //1
    UIButton *button14 = [UIButton buttonWithType:UIButtonTypeSystem];
    button14.frame = CGRectMake(10 , 160, 55, 40);
    [button14 setTitle:@"1" forState:UIControlStateNormal];
    [button14 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button14 setShowsTouchWhenHighlighted:YES];
    button14.backgroundColor = [UIColor whiteColor];
    //    button5.alpha = 0.3;
    button14.layer.cornerRadius = 8;
    [button14 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:button14];
    //2
    UIButton *button15 = [UIButton buttonWithType:UIButtonTypeSystem];
    button15.frame = CGRectMake(70 , 160, 55, 40);
    [button15 setTitle:@"2" forState:UIControlStateNormal];
    [button15 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button15 setShowsTouchWhenHighlighted:YES];
    button15.backgroundColor = [UIColor whiteColor];
    button15.layer.cornerRadius = 8;
    [button15 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:button15];
    //3
    UIButton *button16 = [UIButton buttonWithType:UIButtonTypeSystem];
    button16.frame = CGRectMake(130 , 160, 55, 40);
    [button16 setTitle:@"3" forState:UIControlStateNormal];
    [button16 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button16 setShowsTouchWhenHighlighted:YES];
    button16.backgroundColor = [UIColor whiteColor];
    button16.layer.cornerRadius = 8;
    [button16 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside];
     [view1 addSubview:button16];
    //0
    UIButton *button17 = [UIButton buttonWithType:UIButtonTypeSystem];
    button17.frame = CGRectMake(10 , 210, 110, 40);
    [button17 setTitle:@"0" forState:UIControlStateNormal];
    [button17 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button17 setShowsTouchWhenHighlighted:YES];
    button17.backgroundColor = [UIColor whiteColor];
    button17.layer.cornerRadius = 8;
    [button17 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:button17];
    //.
    UIButton *button18 = [UIButton buttonWithType:UIButtonTypeSystem];
    button18.frame = CGRectMake(130 , 210, 55, 40);
    [button18 setTitle:@"." forState:UIControlStateNormal];
    [button18 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button18 setShowsTouchWhenHighlighted:YES];
    button18.backgroundColor = [UIColor whiteColor];
    button18.layer.cornerRadius = 8;
    [button18 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:button18];
    
    
    //=
    UIButton *button20 = [UIButton buttonWithType:UIButtonTypeSystem];
    button20.frame = CGRectMake(190 , 160, 55, 90);
    [button20 setTitle:@"=" forState:UIControlStateNormal];
    [button20 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button20 setShowsTouchWhenHighlighted:YES];
    button20.backgroundColor = [UIColor orangeColor];
    button20.layer.cornerRadius = 8;
    [button20 addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:button20];
    
    
    self.string=[[NSMutableString alloc]init];
}
-(void)one:(id)sender
{
    //保证是符号时在输入数字时隐藏
    if ([self.string hasPrefix:@"+"]||[self.string hasPrefix:@"-"]||[self.string hasPrefix:@"*"]||[self.string hasPrefix:@"/"])//判断是否为运算符
    {
        [self.string setString:@""];//字符串清零
    }
    [self.string appendString:[sender currentTitle]];//数字连续输入
//    self.label1.text = [NSString stringWithString:string];
    self.label1.text = [self.label1.text stringByAppendingString:[sender currentTitle]];//将计算公式显示在label1中
    self.label.text=[NSString stringWithString:string];//显示数值
    
    self.num1=[self.label.text doubleValue];//保存输入的数值
    NSLog(@"%f",self.num1);
    
}
-(void)two:(id)sender
{
    
    [self.string setString:@""];//字符串清零
    [self.string appendString:[sender currentTitle]];
    self.label.text=[NSString stringWithString:string];
    self.label1.text = [self.label1.text stringByAppendingString:[sender currentTitle]]; //显示输入的符号
    
    //判断输入是+号
    if ([self.string hasPrefix:@"+"])//hasPrefix:判断字符串以加号开头
    {
        self.num2=self.num1;//将前面的数值保存在num2里
        self.num4=1;
        
    }
    //判断输入是-号
    else if([self.string hasPrefix:@"-"])//hasPrefix:判断字符串以减号开头
    {
        self.num2=self.num1;
        self.num4=2;
    }
    //判断输入是*号
    else if([self.string hasPrefix:@"*"])//hasPrefix:判断字符串以乘号开头
    {
        self.num2=self.num1;
        self.num4=3;
    }
    //判断输入是/号
    else if([self.string hasPrefix:@"/"])//hasPrefix:判断字符串以除号开头
    {
        self.num2=self.num1;
        self.num4=4;
    }
}
-(void)go:(id)sender
{
    //判断输入是+号
    if (self.num4==1)
    {
        self.num3=self.num2+[self.label.text doubleValue];//[self.label.text doubleValue]是每次后输入的数字
        self.label.text=[NSString stringWithFormat:@"%g",self.num3];//显示结果
//        self.label1.text = [self.label1.text stringByAppendingString:@"="];   //将等于好在label1中显示出来
        self.num1=[self.label.text doubleValue];//为了可以连加。保存结果
        self.label1.text = @"";   //滞空 ,用来存放结果
        self.label1.text = [NSString stringWithFormat:@"%g",self.num3];  //上面显示结果
        self.num3=0;
        [self.string setString:@""];//保证每次结果正确输出后,再次计算,不用按清除键
    }
    //判断输入是-号
    else if(self.num4==2)
    {
        self.num3=self.num2-[self.label.text doubleValue];
        self.label.text=[NSString stringWithFormat:@"%g",self.num3];
        self.label1.text = [self.label1.text stringByAppendingString:@"="];   //将等于好在label1中显示出来
        self.num1=[self.label.text doubleValue];
        self.label1.text = @"";   //滞空 ,用来存放结果
         self.label1.text = [NSString stringWithFormat:@"%g",self.num3];  //上面显示结果
        self.num3=0;
        [self.string setString:@""];
    }
    //判断输入是*号
    else if(self.num4==3)
    {
        self.num3=self.num2*[self.label.text doubleValue];
        self.label.text=[NSString stringWithFormat:@"%g",self.num3];
        self.label1.text = [self.label1.text stringByAppendingString:@"="];   //将等于好在label1中显示出来
        self.num1=[self.label.text doubleValue];
        self.label1.text = @"";   //滞空 ,用来存放结果
        self.label1.text = [NSString stringWithFormat:@"%g",self.num3];  //上面显示结果
        self.num3=0;
        [self.string setString:@""];
    }
    //判断输入是/号
    else if(self.num4 == 4)
    {
        self.num3=self.num2/[self.label.text doubleValue];
        self.label.text=[NSString stringWithFormat:@"%g",self.num3];//计算结果显示出来
        self.label1.text = [self.label1.text stringByAppendingString:@"="];   //将等于好在label1中显示出来
        self.num1=[self.label.text doubleValue];//把计算的结果保存一下
        self.label1.text = @"";   //滞空 ,用来存放结果
        self.label1.text = [NSString stringWithFormat:@"%g",self.num3];  //上面显示结果
        self.num3=0;
        [self.string setString:@""];
    }
}
//当按下清除建时,所有数据清零
-(void)clean:(id)sender{
    [self.string setString:@""];//清空字符串
    self.num3=0;
    self.num2 = 0;
    self.label.text=@"";//保证下次输入时清零
    self.label1.text = @"";
    
}
//返回键
-(void)back:(id)sender
{
    if ([self.label.text length] > 0 && [self.label1.text length] > 0) {
        self.label.text = [self.label.text substringToIndex:[self.label.text length] - 1];
        self.label1.text = [self.label1.text substringToIndex:[self.label1.text length] - 1];
        
    }else if ([self.label.text length] > 0 || [self.label1.text length] > 0){
//        self.label1.text = [self.label1.text substringToIndex:[self.label1.text length] - 1];
        [self.string setString:@""];
        self.num3=0;
        self.num2 = 0;
        self.label1.text = @"";
        self.label.text=@"";
    }
    self.num1=[self.label.text doubleValue];
    
}
//- (void)numberAction:(UIButton *)button
//{
//    NSLog(@"数字");
//    if (self.isSymbol == YES) {
//        //如果刚刚点击过符号键,就重新输入
//        self.label.text = button.currentTitle;
//    }else{
//        self.label.text = [NSString stringWithFormat:@"%@%@",self.label.text,button.currentTitle];
//    }
//    self.isSymbol = NO;
//    
//}
//
//- (void)addAction:(UIButton *)button
//{
//    //只要点击符号键,就将属性为yes
//    self.isSymbol = YES;
//    //保存第一个数字
//    self.firstNumber = [self.label.text integerValue];
//    //保存当前选择的符号(+ - * /)
//    if ([button.currentTitle isEqualToString:@"+"]) {
//        self.sumbol = 1;
//    }else if ([button.currentTitle isEqualToString:@"-"]){
//        self.sumbol = 2;
//    }else if ([button.currentTitle isEqualToString:@"*"]){
//        self.sumbol = 3;
//    }else if ([button.currentTitle isEqualToString:@"/"]){
//        self.sumbol = 4;
//    }
//    NSLog(@"符号");
//}
//- (void)equalAction:(UIButton *)equal
//{
//    //在等号方法中做计算
//    
//    //计算
//    if (self.sumbol == 1) {
//        //获取第二个数字
//        NSInteger secondNumber = [self.label.text integerValue];
//        NSInteger result = _firstNumber + secondNumber;
//        //将计算结果给label显示
//        self.label.text = [NSString stringWithFormat:@"%d",result];
//
//    }if (self.sumbol == 2) {
//        NSInteger secondNumber = [self.label.text integerValue];
//        NSInteger result = _firstNumber - secondNumber;
//        //将计算结果给label显示
//        self.label.text = [NSString stringWithFormat:@"%d",result];
//    }
//    
//    
//    
//    NSLog(@"等于");
//}
//
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end


下面是没有实现的,在网上找的代码参考 ........谨慎使用


ViewController.h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property(retain,nonatomic)UIButton *button;
@property(retain,nonatomic)UILabel *label;
@property(retain,nonatomic)NSMutableString *string;
@property(assign,nonatomic)double num1,num2,num3,num4;
@end

ViewController.m

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize button,label,string,num1,num2,num3,num4;//string保存字符,显示数值。num1是存输入的数值,num2是存运算符前的数值,num3是运算结果,num4是判断进行何种运算
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //设置背景图片
    NSBundle *bundle=[NSBundle mainBundle];
    NSData *data=[[NSData alloc]initWithContentsOfFile:
                  [bundle pathForResource:@"3" ofType:@"jpg"]];//找到NSBundle的某一资源
    UIImage *img=[UIImage p_w_picpathWithData:data];//创建了可用的图像对象
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:img]];//UIColor colorWithPatternImage:方法是把图片转化为color类型  将背景换做提供的图片
    
    //创建标签
    self.label=[[UILabel alloc]initWithFrame:CGRectMake(90, 40, 200, 50)];
    [self.view addSubview:label];
    self.label.backgroundColor=[UIColor clearColor];//清空背景颜色
    self.label.textColor=[UIColor blueColor];//字体颜色
    self.label.textAlignment =  NSTextAlignmentRight;;//字体居右
    self.label.font=[UIFont systemFontOfSize:32.4];
    
    //添加1-9数字
    NSArray *array=[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil];
    int n=0;
    for (int i=0; i<3; i++)
    {
        for (int j=0; j<3; j++)
        {
            self.button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
            self.button.frame=CGRectMake(30+65*j, 150+65*i, 60, 60);
            self.button.backgroundColor = [UIColor whiteColor];
            [self.button setTitle:[array objectAtIndex:n++] forState:UIControlStateNormal];
            self.button.titleLabel.font = [UIFont systemFontOfSize:30];
            self.button.layer.cornerRadius = 6;
            [self.view addSubview:button];
            [self.button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
            [self.button addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    //单独添加0
    UIButton *button0=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button0 setFrame:CGRectMake(30, 345, 60, 60)];
    button0.layer.cornerRadius = 6;
    button0.backgroundColor = [UIColor whiteColor];
    [button0 setTitle:@"0" forState:UIControlStateNormal];
    button0.titleLabel.font = [UIFont systemFontOfSize:30];
    [button0 setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [button0 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button0];
    
    
    //添加运算符
    NSArray *array1=[NSArray arrayWithObjects:@"+",@"-",@"*",@"/",nil];
    for (int i=0; i<4; i++)
    {
        UIButton *button1=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        [button1 setFrame:CGRectMake(225, 150+65*i, 60, 60)];
        button1.backgroundColor = [UIColor whiteColor];
        button1.layer.cornerRadius = 6;
        [button1 setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [button1 setTitle:[array1 objectAtIndex:i] forState:UIControlStateNormal];
        [self.view addSubview:button1];
        button1.titleLabel.font = [UIFont systemFontOfSize:30];
        [button1 addTarget:self action:@selector(two:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    //添加=
    
    UIButton *button2=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    button2.backgroundColor = [UIColor whiteColor];
    [button2 setFrame:CGRectMake(160, 410, 125, 35)];
    [button2 setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    button2.titleLabel.font = [UIFont systemFontOfSize:30];
    [button2 setTitle:@"=" forState:UIControlStateNormal];
    button2.layer.cornerRadius = 6;
    [button2 addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
    
    //添加清除键
    
    UIButton *button3=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    button3.backgroundColor = [UIColor whiteColor];
    [button3 setFrame:CGRectMake(30, 410, 125, 35)];
    button3.layer.cornerRadius = 6;
    [button3 setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [button3 setTitle:@"AC" forState:UIControlStateNormal];
    button3.titleLabel.font = [UIFont systemFontOfSize:20];
    [button3 addTarget:self action:@selector(clean:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button3];
    
    //添加.
    
    UIButton *button4=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button4 setFrame:CGRectMake(95, 345, 60, 60)];
    [button4 setTitle:@"." forState:UIControlStateNormal];
    button4.titleLabel.font = [UIFont systemFontOfSize:30];
    button4.layer.cornerRadius = 6;
    [button4 setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    button4.backgroundColor = [UIColor whiteColor];
    [button4 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button4];
    
    //后退
    
    UIButton *button5=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button5 setFrame:CGRectMake(160, 345, 60, 60)];
    [button5 setTitle:@"back" forState:UIControlStateNormal];
    button5.backgroundColor = [UIColor whiteColor];
    button5.layer.cornerRadius = 6;
    [button5 setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    button5.titleLabel.font = [UIFont systemFontOfSize:20];
    [button5 addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:button5];
    
    
    self.string=[[NSMutableString alloc]init];//初始化可变字符串,分配内存
    // Do any additional setup after loading the view, typically from a nib.
}
-(void)one:(id)sender
{
    //保证是符号时在输入数字时隐藏
    if ([self.string hasPrefix:@"+"]||[self.string hasPrefix:@"-"]||[self.string hasPrefix:@"*"]||[self.string hasPrefix:@"/"])//判断是否为运算符
    {
        [self.string setString:@""];//字符串清零
    }
    [self.string appendString:[sender currentTitle]];//数字连续输入
    self.label.text=[NSString stringWithString:string];//显示数值
    self.num1=[self.label.text doubleValue];//保存输入的数值
    NSLog(@"%f",self.num1);
    
}
-(void)two:(id)sender
{
    [self.string setString:@""];//字符串清零
    [self.string appendString:[sender currentTitle]];
    self.label.text=[NSString stringWithString:string];
    
    //判断输入是+号
    if ([self.string hasPrefix:@"+"])//hasPrefix:判断字符串以加号开头
    {
        self.num2=self.num1;//将前面的数值保存在num2里
        self.num4=1;
    }
    //判断输入是-号
    else if([self.string hasPrefix:@"-"])//hasPrefix:判断字符串以减号开头
    {
        self.num2=self.num1;
        self.num4=2;
    }
    //判断输入是*号
    else if([self.string hasPrefix:@"*"])//hasPrefix:判断字符串以乘号开头
    {
        self.num2=self.num1;
        self.num4=3;
    }
    //判断输入是/号
    else if([self.string hasPrefix:@"/"])//hasPrefix:判断字符串以除号开头
    {
        self.num2=self.num1;
        self.num4=4;
    }
}
-(void)go:(id)sender
{
    //判断输入是+号
    if (self.num4==1)
    {
        self.num3=self.num2+[self.label.text doubleValue];//[self.label.text doubleValue]是每次后输入的数字
        self.label.text=[NSString stringWithFormat:@"%g",self.num3];//显示结果
        self.num1=[self.label.text doubleValue];//为了可以连加。保存结果
        self.num3=0;
        [self.string setString:@""];//保证每次结果正确输出后,再次计算,不用按清除键
    }
    //判断输入是-号
    else if(self.num4==2)
    {
        self.num3=self.num2-[self.label.text doubleValue];
        self.label.text=[NSString stringWithFormat:@"%g",self.num3];
        self.num1=[self.label.text doubleValue];
        self.num3=0;
        [self.string setString:@""];
    }
    //判断输入是*号
    else if(self.num4==3)
    {
        self.num3=self.num2*[self.label.text doubleValue];
        self.label.text=[NSString stringWithFormat:@"%g",self.num3];
        self.num1=[self.label.text doubleValue];
        self.num3=0;
        [self.string setString:@""];
    }
    //判断输入是/号
    else if(self.num4 == 4)
    {
        self.num3=self.num2/[self.label.text doubleValue];
        self.label.text=[NSString stringWithFormat:@"%g",self.num3];//计算结果显示出来
        self.num1=[self.label.text doubleValue];//把计算的结果保存一下
        self.num3=0;
        [self.string setString:@""];
    }
}
//当按下清除建时,所有数据清零
-(void)clean:(id)sender{
    [self.string setString:@""];//清空字符串
    self.num3=0;
    self.num2 = 0;
    self.label.text=@"";//保证下次输入时清零
    
}
//返回键
-(void)back:(id)sender
{
    if ([self.label.text length] > 0) {
        self.label.text = [self.label.text substringToIndex:[self.label.text length] - 1];
    }
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end