IOS 仿自带计算器

ViewController.h


#import <UIKit/UIKit.h>


@interface ViewController : UIViewController{

    UIButton * btn;

    UILabel *label;

    UIButton *btn0;

    UIButton *btndian;

    int btn_width,btn_height;

    int what;

}


@property(retain,nonatomic) NSMutableString *mString;

@property(retain,nonatomic) NSString *string;

@property(assign,nonatomic) double num1,num2;



@end


ViewController.m


#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    CGRect rx = [ UIScreen mainScreen ].bounds;

    btn_height = 100;

    btn_width = rx.size.width/4+1;

    what = -1;

    

    label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, rx.size.width, rx.size.height-5*btn_height)];

    label.backgroundColor = [UIColor blackColor];

    label.textColor = [UIColor whiteColor];

    label.text = @"";

    label.font = [UIFont fontWithName:@"Times new Roman" size:50];

    label.adjustsFontSizeToFitWidth = YES;

    label.textAlignment = UITextAlignmentRight; //字体居右

    [self.view addSubview:label];

    

    NSArray *array1=[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++)

        {

            btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];

            btn.frame=CGRectMake(btn_width*j, rx.size.height-btn_height*(4-i), btn_width, btn_height);

            btn.backgroundColor = [UIColor colorWithRed:196/255.0 green:200 /255.0 blue:201/255.0 alpha:1];

            btn.font = [UIFont fontWithName:@"STHeitiJ-Light" size:50];

            

            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

            btn.layer.borderWidth = 0.6;

            [btn.layer setBorderColor:[UIColor grayColor].CGColor];


            [btn setTitle:[array1 objectAtIndex:n++] forState:UIControlStateNormal];   //注意:[array objectAtIndex:n++]

            [self.view addSubview:btn];

            [btn addTarget:self action:@selector(shuzi:) forControlEvents:UIControlEventTouchUpInside]; //addTarget:self 的意思是说,这个方法在本类中也可以传入其他类的指针

        }

    }

    n = 0;

    NSArray *array2=[NSArray arrayWithObjects:@"/",@"*",@"-",@"+", @"=",nil];

    for (int i=0; i<5; i++)

    {

        

        btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];

        btn.frame=CGRectMake(btn_width*3, rx.size.height-btn_height*(5-i), btn_width, btn_height);

        btn.font = [UIFont fontWithName:@"STHeitiJ-Light" size:50];

        

        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

        btn.layer.borderWidth = 0.6;

        [btn.layer setBorderColor:[UIColor grayColor].CGColor];

        btn.backgroundColor = [UIColor colorWithRed:249/255.0 green:136 /255.0 blue:16/255.0 alpha:1];

        [btn setTitle:[array2 objectAtIndex:n++] forState:UIControlStateNormal];   //注意:[array objectAtIndex:n++]

        [self.view addSubview:btn];

        [btn addTarget:self action:@selector(cacula:) forControlEvents:UIControlEventTouchUpInside];

        

    }

    btn0 = [[UIButton alloc]initWithFrame:CGRectMake(0, rx.size.height-btn_height, 2*btn_width, btn_height)];

    [btn0 setTitle:@"0" forState:UIControlStateNormal];

    btn0.backgroundColor = [UIColor colorWithRed:196/255.0 green:200 /255.0 blue:201/255.0 alpha:1];

    btn0.font = [UIFont fontWithName:@"STHeitiJ-Light" size:50];

    

    [btn0 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    btn0.layer.borderWidth = 0.6;

    [btn0.layer setBorderColor:[UIColor grayColor].CGColor];

    [self.view addSubview:btn0];

    [btn0 addTarget:self action:@selector(shuzi:) forControlEvents:UIControlEventTouchUpInside];

    

    btndian = [[UIButton alloc]initWithFrame:CGRectMake(2*btn_width, rx.size.height-btn_height, btn_width, btn_height)];

    [btndian setTitle:@"." forState:UIControlStateNormal];

    btndian.backgroundColor = [UIColor colorWithRed:196/255.0 green:200 /255.0 blue:201/255.0 alpha:1];

    btndian.font = [UIFont fontWithName:@"STHeitiJ-Light" size:50];

    

    [btndian setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    btndian.layer.borderWidth = 0.6;

    [btndian.layer setBorderColor:[UIColor grayColor].CGColor];

    [self.view addSubview:btndian];

    [btndian addTarget:self action:@selector(shuzi:) forControlEvents:UIControlEventTouchUpInside];

    

    n = 0;

    NSArray *array3=[NSArray arrayWithObjects:@"AC",@"+/-",@"%",nil];

    for (int i=0; i<3; i++)

    {

        

        btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];

        btn.frame=CGRectMake(btn_width*i, rx.size.height-btn_height*5, btn_width, btn_height);

        btn.backgroundColor = [UIColor colorWithRed:198/255.0 green:202 /255.0 blue:203/255.0 alpha:1];

        btn.font = [UIFont fontWithName:@"STHeitiJ-Light" size:30];

        

        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

        btn.layer.borderWidth = 0.6;

        [btn.layer setBorderColor:[UIColor grayColor].CGColor];

        [btn setTitle:[array3 objectAtIndex:n++] forState:UIControlStateNormal];   //注意:[array objectAtIndex:n++]

        [self.view addSubview:btn];

        [btn addTarget:self action:@selector(qita:) forControlEvents:UIControlEventTouchUpInside];

        

    }

}

- (void)qita:(id)sender{

    if([[sender currentTitle] isEqualToString:@"AC"]){

        what = -1;

        [label setText:@""];

    }

    if([[sender currentTitle] isEqualToString:@"%"]){

        if(what == -1){

            self.num1 = self.num1/100;

            [label setText:[NSString stringWithFormat:@"%.2f",self.num1]];

        }

        else if(what>0 && what<=4){

            self.num2 = self.num2/100;

            [label setText:[NSString stringWithFormat:@"%.2f",self.num2]];

        }else if(what == 5){

            self.num1 = self.num2/100;

            [label setText:[NSString stringWithFormat:@"%.2f",self.num1]];

            what = -1;

        }

        

    }

    if([[sender currentTitle] isEqualToString:@"+/-"]){

        self.string = [[NSString alloc]init];

        if([[label text] rangeOfString:@"-"].location!=NSNotFound){

            self.string = [[label text] substringFromIndex:1];

            [label setText:self.string];

        }

        else{

            self.string = [@"-" stringByAppendingString:[label text]];

            [label setText:self.string];

        }

        if(what == -1){

            self.num1=[label.text doubleValue];               //保存输入的数值

            NSLog(@"self.num1  is  %f",self.num1);

        }

        else if(what >0 && what<=4){

            self.num2=[label.text doubleValue];               //保存输入的数值

            NSLog(@"self.num2  is  %f",self.num2);

        }

    }


}

- (void)cacula:(id)sender{

    double answer = 0;

    if([[sender currentTitle] isEqualToString: @"+"]){

        if(what == 5){

            self.num1 = self.num2;

        }

        what = 1;

        [label setText:@""];

    }

    if([[sender currentTitle] isEqualToString: @"-"]){

        if(what == 5){

            self.num1 = self.num2;

        }

        what = 2;

        [label setText:@""];

    }

    if([[sender currentTitle] isEqualToString: @"*"]){

        if(what == 5){

            self.num1 = self.num2;

        }

        what = 3;

        [label setText:@""];

    }

    if([[sender currentTitle] isEqualToString: @"/"]){

        if(what == 5){

            self.num1 = self.num2;

        }

        what = 4;

        [label setText:@""];

    }

    if([[sender currentTitle] isEqualToString: @"="]){

        if(what == 1){

            answer = self.num1+self.num2;

        }

        else if(what == 2){

            answer = self.num1-self.num2;

        }

        else if(what == 3){

            answer = self.num1*self.num2;

        }

        else if(what == 4){

            answer = self.num1/self.num2;

        }else {

            answer = self.num1;

        }


        [label setText:[NSString stringWithFormat:@"%.2f",answer]];

        self.num2 = answer;

        what = 5;

    }

}

//0-9方法

- (void)shuzi:(id)sender

{

    if(what==5){

        [label setText:@""];

        what = -1;

    }

    self.string = [[NSString alloc]init];

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

    [label setText:self.string];

    

    if(what == -1){

        self.num1=[label.text doubleValue];               //保存输入的数值

        NSLog(@"self.num1  is  %f",self.num1);

    }

    else if(what >0 && what<=4){

        self.num2=[label.text doubleValue];               //保存输入的数值

        NSLog(@"self.num2  is  %f",self.num2);

    }

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我扶奶奶过哈登

您的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值