iOS计算器的代码实现


//QQ:2810418812@qq.com

// ViewController.h

// 计算器

// Created by ccj on 15/12/2.

// Copyright © 2015年 ccj. All rights reserved.

//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property(nonatomic,strong)UIButton *button;

@property(nonatomic,strong)UILabel *label;

@property(nonatomic,strong)NSMutableString *string;

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

@property(nonatomic,strong)NSMutableString *str;

@property(nonatomic,assign)int number;

@end

//

// ViewController.m

// 计算器

//

// Created by ccj on 15/12/2.

// Copyright © 2015年 ccj. All rights reserved.

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

-(NSMutableString *)string

{

if(_string==nil)

{

_string=[NSMutableString string];

}

return _string;

}

-(NSMutableString *)str

{

if(_str==nil)

{

_str=[[NSMutableString alloc]initWithString:@""];

}

return _str;

}

- (void)viewDidLoad {

[super viewDidLoad];

self.number=0;

CGFloat buttonWidth=self.view.bounds.size.width/4;

/**

设置label

*/

self.label=[[UILabel alloc]initWithFrame:CGRectMake(0, 50, self.view.bounds.size.width, 100)];

[self.view addSubview:_label];

self.label.backgroundColor=[UIColor grayColor];

self.label.textColor=[UIColor whiteColor];

self.label.textAlignment=NSTextAlignmentRight;

//设置文本的字体

self.label.font=[UIFont systemFontOfSize:50];

self.label.text=@"0";

self.label.layer.masksToBounds=YES;

self.label.layer.cornerRadius=10;

/**

添加数字1~9

*/

NSArray *array=[NSArray arrayWithObjects:@"1",@"4",@"7",@"2",@"5",@"8",@"3",@"6",@"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(buttonWidth*i, 150+buttonWidth+buttonWidth*j, buttonWidth, buttonWidth);

[self.button setTitle:[array objectAtIndex:n++] forState:UIControlStateNormal];

[self.view addSubview:_button];

self.button.titleLabel.font=[UIFont systemFontOfSize:50];

self.button.layer.masksToBounds=YES;

self.button.layer.cornerRadius=10.0;

self.button.tintColor=[UIColor blackColor];

self.button.tintColor=[UIColor blackColor];

self.button.layer.borderWidth=1.0;

self.button.backgroundColor=[UIColor colorWithRed:223.0/255.0f green:223.0/255.0f blue:223.0/255.0f alpha:1.0];

[self.button addTarget:self action:@selector(number:) forControlEvents:UIControlEventTouchUpInside];

}

}

/**

单独添加数字0

*/

UIButton *button0=[UIButton buttonWithType:UIButtonTypeRoundedRect];

[self.view addSubview:button0];

button0.frame=CGRectMake(0, 150+buttonWidth*4, 2*buttonWidth, buttonWidth);

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

button0.titleLabel.font=[UIFont systemFontOfSize:50];

//tintColor

button0.tintColor=[UIColor blackColor];

button0.layer.masksToBounds=YES;

button0.layer.cornerRadius=10.0;

button0.tintColor=[UIColor blackColor];

button0.layer.borderWidth=1.0;

[button0 addTarget:self action:@selector(number:) forControlEvents:UIControlEventTouchUpInside];

button0.backgroundColor=[UIColor colorWithRed:223.0/255.0f green:223.0/255.0f blue:223.0/255.0f alpha:1.0];

/**

添加小数点.

*/

UIButton *button1=[UIButton buttonWithType:UIButtonTypeRoundedRect];

[self.view addSubview:button1];

button1.frame=CGRectMake(2*buttonWidth, 150+buttonWidth*4, buttonWidth, buttonWidth);

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

button1.tintColor=[UIColor blackColor];

button1.titleLabel.font=[UIFont systemFontOfSize:50];

button1.layer.masksToBounds=YES;

button1.layer.cornerRadius=10.0;

button1.tintColor=[UIColor blackColor];

button1.layer.borderWidth=1.0;

button1.backgroundColor=[UIColor colorWithRed:223.0/255.0f green:223.0/255.0f blue:223.0/255.0f alpha:1.0];

[button1 addTarget:self action:@selector(number:) forControlEvents:UIControlEventTouchUpInside];

/**

添加清楚键

*/

UIButton *clearButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];

[self.view addSubview:clearButton];

clearButton.frame=CGRectMake(0, 150, 2*buttonWidth, buttonWidth);

clearButton.backgroundColor=[UIColor colorWithRed:223.0/255.0f green:223.0/255.0f blue:223.0/255.0f alpha:1.0];

[clearButton setTitle:@"AC" forState:UIControlStateNormal];

//设置边框的粗细和颜色

clearButton.tintColor=[UIColor blackColor];

clearButton.layer.masksToBounds=YES;

clearButton.layer.cornerRadius=10.0;

clearButton.layer.borderWidth=1.0;

clearButton.layer.borderColor=[UIColor blackColor].CGColor;

clearButton.titleLabel.font=[UIFont systemFontOfSize:50];

[clearButton addTarget:self action:@selector(clean:) forControlEvents:UIControlEventTouchUpInside];

/**

添加%符号

*/

UIButton *button2=[UIButton buttonWithType:UIButtonTypeRoundedRect];

[self.view addSubview:button2];

button2.frame=CGRectMake(2*buttonWidth, 150, buttonWidth, buttonWidth);

[button2 setTitle:@"%" forState:UIControlStateNormal];

button2.tintColor=[UIColor blackColor];

button2.titleLabel.font=[UIFont systemFontOfSize:50];

button2.layer.masksToBounds=YES;

button2.layer.cornerRadius=10.0;

button2.tintColor=[UIColor blackColor];

button2.layer.borderWidth=1.0;

button2.backgroundColor=[UIColor colorWithRed:223.0/255.0 green:223.0/255.0 blue:223.0/255.0 alpha:1.0];

[button2 addTarget:self action:@selector(remberSymbol:) forControlEvents:UIControlEventTouchUpInside];

/**

添加运算符+ - * / =

*/

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

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

{

UIButton *buttonSymbol=[UIButton buttonWithType:UIButtonTypeRoundedRect];

[self.view addSubview:buttonSymbol];

[buttonSymbol setTitle:[arraySymbol objectAtIndex:i] forState:UIControlStateNormal];

buttonSymbol.frame=CGRectMake(3*buttonWidth, 150+buttonWidth*i, buttonWidth, buttonWidth);

buttonSymbol.titleLabel.font=[UIFont systemFontOfSize:50];

buttonSymbol.tintColor=[UIColor whiteColor];

buttonSymbol.layer.borderWidth=1.0;

buttonSymbol.layer.masksToBounds=YES;

buttonSymbol.layer.cornerRadius=10.0;

buttonSymbol.backgroundColor=[UIColor orangeColor];

if(i==4)

{

[buttonSymbol addTarget:self action:@selector(calculater:) forControlEvents:UIControlEventTouchUpInside];

return;

}

[buttonSymbol addTarget:self action:@selector(remberSymbol:) forControlEvents:UIControlEventTouchUpInside];

}

/**

添加正负号

*/

}

/**

数字的显示

*/

-(void)number:(id)sender

{

if(![self.str isEqualToString:@""])

{

if(self.number!=1&&self.number!=0)

{

self.num1=self.value;

}

[self.string appendString:[sender currentTitle]];

self.label.text=[NSString stringWithString:self.string];

self.num2=[self.label.text doubleValue];

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

return;

}

[self.string appendString:[sender currentTitle]];

// NSLog(@"%@",self.string);

self.label.text=[NSString stringWithString:self.string];

//将字符串转换成数字

self.num1=[self.label.text doubleValue];

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

}

/**

记下所按下的+ - * /符号

*/

-(void)remberSymbol:(id)sender

{

self.number++;

//NSLog(@"%s",__func__);

[self.str setString:[sender currentTitle]];

self.label.text=@"0";

[self.string setString:@""];

}

/**

计算

*/

-(void)calculater:(id)sender

{

//NSLog(@"%s",__func__);

if([self.str isEqualToString:@"+"])

{

self.value=self.num1+self.num2;

self.label.text=[NSString stringWithFormat:@"%.2f",self.value];

}

else if([self.str isEqualToString:@"-"])

{

self.value=self.num1-self.num2;

self.label.text=[NSString stringWithFormat:@"%.2f",self.value];

}

else if([self.str isEqualToString:@"*"])

{

self.value=self.num1*self.num2;

self.label.text=[NSString stringWithFormat:@"%.2f",self.value];

}

else if([self.str isEqualToString:@"/"])

{

self.value=self.num1/self.num2;

self.label.text=[NSString stringWithFormat:@"%.2f",self.value];

}

else if([self.str isEqualToString:@"%"])

{

self.value=(int)(self.num1)%(int)self.num2;

self.label.text=[NSString stringWithFormat:@"%d",(int)self.value];

}

self.number=0;

[self.string setString:@""];

[self.str setString:@""];

self.num1=self.value;

}

/**

清除键

*/

-(void)clean:sender

{

[self.string setString:@""];

self.num1=0;

self.num2=0;

self.label.text=@"0";

[self.str setString:@""];

self.number=0;

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

file:///Users/ccj/Desktop/ji.tiff

优质内容筛选与推荐>>
1、美丽乡村——大堰镇南溪村
2、Javascript学习------js 简单模拟时钟
3、Mysql 忘记root密码和修改root密码的解决方法(小结)
4、网页布局基础
5、多维高斯分布


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号