首页 > 代码库 > 简单计算器

简单计算器

#import "ViewController.h"

#import "cal.h"

@interface ViewController ()

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

            

 

@end

   float i=10.0;

@implementation ViewController

- (IBAction)Didclick0:(UIButton *)sender {

    if (calc.op ==‘+‘||calc.op==‘-‘||calc.op==‘*‘||calc.op==‘/‘)

    {

         if(calc.diandian==‘.‘)

         {

            calc.opvalue2=calc.opvalue2+sender.tag/i;

            i=i*10.0;

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

        }

         else

         {

             calc.opvalue2=calc.opvalue2*10.0+sender.tag;

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

         }

    }

    else

    {

        if(calc.diandian==‘.‘)

        {

        calc.opvalue1=calc.opvalue1+sender.tag/i;

            i=i*10.0;

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

        }

        else

        {

            calc.opvalue1=calc.opvalue1*10.0+sender.tag;

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

        }

    }

}

 

- (IBAction)Didclickadd:(UIButton *)sender

{

    calc.op=‘+‘;

    calc.diandian=0;

    i=10;

    

}

- (IBAction)DidlcickCut:(UIButton *)sender

{

    calc.op=‘-‘;

    calc.diandian=0;

    i=10;

}

- (IBAction)Didclickcheng:(UIButton *)sender {

    calc.op=‘*‘;

    calc.diandian=0;

    i=10;

}

- (IBAction)Didclickchu:(UIButton *)sender {

    calc.op=‘/‘;

    calc.diandian=0;

    i=10;

}

- (IBAction)xiaodian:(UIButton *)sender {

    calc.diandian=‘.‘;

}

- (IBAction)esc:(id)sender {

    calc.opvalue1=0;

    calc.opvalue2=0;

    calc.op=0;

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

}

- (IBAction)Didclickequal:(id)sender {

    switch (calc.op)

    {

        case  ‘+‘:

        {

            float result=[calc work];

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

            break;

        }

        case ‘-‘:

        {

            float result=[calc run];

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

            break;

        }

        case ‘*‘:

        {

            float result=[calc start];

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

            break;

        }

        case ‘/‘:

        {

            float result=[calc chufa];

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

            break;

        }

        default:

            break;

    }

    //(clac.op==‘+‘):

    

     //float result=[calc work];

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

    

}

            

- (void)viewDidLoad {

    [super viewDidLoad];

   calc=[[cal alloc]init];//生成计算器;

    //放入动画。

    UIImage *image1=[UIImage imageNamed:@"1.jpg"];

    UIImage *image2=[UIImage imageNamed:@"2.jpg"];

    UIImageView *imageview=[[UIImageView alloc]init];

    imageview.frame=CGRectMake(0, 420, 50, 100);

    imageview.image=image2;

    imageview.highlightedImage=image2;

    imageview.highlighted=NO;

    [self.view addSubview:imageview];

    

    UIImage *image3 = [UIImage imageNamed:@"3.jpg"];

    NSArray *array=[NSArray arrayWithObjects:image1,image2,image3,nil];

    imageview.animationImages=array;

    imageview.animationDuration=3;

    [imageview startAnimating];

    [self.view addSubview:imageview];

    

    // Do any additional setup after loading the view, typically from a nib.

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end