首页 > 代码库 > 一个垃圾计算器

一个垃圾计算器

//

//  ViewController.m

//  calculator

//

//  Created by apple on 14-7-10.

//  Copyright (c) 2014年 apple. All rights reserved.

//

 

#import "ViewController.h"

#import "calculat.h"

@interface ViewController ()

{

    float result;

    int i;

}

            

@property (weak, nonatomic) IBOutlet UILabel *lab;

 

@end

 

@implementation ViewController

            

- (void)viewDidLoad

{

    [super viewDidLoad];

    cal = [[calculat alloc] init];

    

}

- (IBAction)click:(UIButton *)sender

 

{

    NSLog(@"%d, %@", sender.tag, sender.currentTitle);

 // self.lab.text = [NSString stringWithFormat:@"%d", sender.tag];

    if (cal.opsb == ‘+‘

        ||cal.opsb==‘-‘

        ||cal.opsb==‘/‘

        ||cal.opsb==‘*‘)

    {

        cal.opvalue2 = cal.opvalue2 * 10 + sender.tag;

        self.lab.text = [NSString stringWithFormat:@"%f",cal.opvalue2];

       

    }

    else

    {

        cal.opvalue1 = cal.opvalue1 * 10 + sender.tag;

       self.lab.text = [NSString stringWithFormat:@"%f",cal.opvalue1];

        

    }

}

- (IBAction)sb:(id)sender{cal.opsb=‘+‘;

    

self.lab.text = [NSString stringWithFormat:@"+"];

    i=1;

    

}

- (IBAction)sbm:(id)sender{cal.opsb=‘-‘;

    

    self.lab.text = [NSString stringWithFormat:@"-"];

    i=2;

}

- (IBAction)sbc:(id)sender {cal.opsb=‘/‘;

    

    self.lab.text = [NSString stringWithFormat:@"/"];

    i=3;

 

}

- (IBAction)sbh:(id)sender {cal.opsb=‘*‘;

    

    self.lab.text = [NSString stringWithFormat:@"*"];

    i=4;

 

}

- (IBAction)didClickedEqual:(id)sender {

    

    //计算结果

    if(i==1)

    {

     result = [cal work1];

    self.lab.text  = [NSString stringWithFormat:@"%f", result];

    }

    

    else if(i==2)

    {

        result = [cal work2];

        self.lab.text  = [NSString stringWithFormat:@"%f", result];

    }

    

    else if(i==3)

    {

        result = [cal work3];

        self.lab.text  = [NSString stringWithFormat:@"%f", result];

    }

    

   else  if(i==4)

    {

        result = [cal work4];

        self.lab.text  = [NSString stringWithFormat:@"%f", result];

    }

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

- (IBAction)cd:(id)sender

{

    cal.opsb = 0;

    cal.opvalue1 = 0;

    cal.opvalue2 = 0;

    self.lab.text  = [NSString stringWithFormat:@"0"];

    

}

 

@end