简易计算器,不知道为啥 有一些效果和系统的不一样
//
// RootViewController.m
// 计算器
//
// Created by dllo on 15/8/1.
// Copyright (c) 2015年 dllo. All rights reserved.
//
#import "RootViewController.h"
@interface RootViewController ()
@property(nonatomic, assign)double a;
@property(nonatomic, assign)double b;
@property(nonatomic, assign)double(^p)(double, double);
@property(nonatomic, retain)UILabel *label;
@property(nonatomic, assign)BOOL isOn; // 输入前是否清零
@end
@implementation RootViewController
- (void)dealloc{
[_label release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_label = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 200, 30)];
self.label.layer.borderWidth = 1;
self.label.textAlignment = NSTextAlignmentRight;
[self.view addSubview:self.label];
[_label release];
// 数字 0 - 9
CGRect frame1 = CGRectMake(50, 150, 50, 50);
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
button1.frame = frame1;
button1.layer.borderWidth = 1;
[button1 setTitle:@"1" forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(clickOne:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
CGRect frame2 = CGRectMake(100, 150, 50, 50);
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
button2.frame = frame2;
button2.layer.borderWidth = 1;
[button2 setTitle:@"2" forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(clickOne:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
CGRect frame3 = CGRectMake(150, 150, 50, 50);
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem];
button3.frame = frame3;
button3.layer.borderWidth = 1;
[button3 setTitle:@"3" forState:UIControlStateNormal];
[button3 addTarget:self action:@selector(clickOne:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button3];
CGRect frame4 = CGRectMake(50, 200, 50, 50);
UIButton *button4 = [UIButton buttonWithType:UIButtonTypeSystem];
button4.frame = frame4;
button4.layer.borderWidth = 1;
[button4 setTitle:@"4" forState:UIControlStateNormal];
[button4 addTarget:self action:@selector(clickOne:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button4];
CGRect frame5 = CGRectMake(100, 200, 50, 50);
UIButton *button5 = [UIButton buttonWithType:UIButtonTypeSystem];
button5.frame = frame5;
button5.layer.borderWidth = 1;
[button5 setTitle:@"5" forState:UIControlStateNormal];
[button5 addTarget:self action:@selector(clickOne:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button5];
CGRect frame6 = CGRectMake(150, 200, 50, 50);
UIButton *button6 = [UIButton buttonWithType:UIButtonTypeSystem];
button6.frame = frame6;
button6.layer.borderWidth = 1;
[button6 setTitle:@"6" forState:UIControlStateNormal];
[button6 addTarget:self action:@selector(clickOne:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button6];
CGRect frame7 = CGRectMake(50, 250, 50, 50);
UIButton *button7 = [UIButton buttonWithType:UIButtonTypeSystem];
button7.frame = frame7;
button7.layer.borderWidth = 1;
[button7 setTitle:@"7" forState:UIControlStateNormal];
[button7 addTarget:self action:@selector(clickOne:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button7];
CGRect frame8 = CGRectMake(100, 250, 50, 50);
UIButton *button8 = [UIButton buttonWithType:UIButtonTypeSystem];
button8.frame = frame8;
button8.layer.borderWidth = 1;
[button8 setTitle:@"8" forState:UIControlStateNormal];
[button8 addTarget:self action:@selector(clickOne:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button8];
CGRect frame9 = CGRectMake(150, 250, 50, 50);
UIButton *button9 = [UIButton buttonWithType:UIButtonTypeSystem];
button9.frame = frame9;
button9.layer.borderWidth = 1;
[button9 setTitle:@"9" forState:UIControlStateNormal];
[button9 addTarget:self action:@selector(clickOne:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button9];
CGRect frame0 = CGRectMake(50, 300, 100, 50);
UIButton *button0 = [UIButton buttonWithType:UIButtonTypeSystem];
button0.frame = frame0;
button0.layer.borderWidth = 1;
[button0 setTitle:@"0" forState:UIControlStateNormal];
[button0 addTarget:self action:@selector(clickOne:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button0];
// 清空键
CGRect frameAc = CGRectMake(50, 100, 50, 50);
UIButton *buttonAc = [UIButton buttonWithType:UIButtonTypeSystem];
buttonAc.frame = frameAc;
buttonAc.layer.borderWidth = 1;
[buttonAc setTitle:@"AC" forState:UIControlStateNormal];
[buttonAc addTarget:self action:@selector(clickAc:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonAc];
buttonAc.tag = 1000;
// 正负改变键
CGRect frameNegative = CGRectMake(100, 100, 50, 50);
UIButton *buttonNegative = [UIButton buttonWithType:UIButtonTypeSystem];
buttonNegative.frame = frameNegative;
buttonNegative.layer.borderWidth = 1;
[buttonNegative setTitle:@"+/-" forState:UIControlStateNormal];
[buttonNegative addTarget:self action:@selector(clickNegative:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonNegative];
// 百分号键
CGRect frameRemainder = CGRectMake(150, 100, 50, 50);
UIButton *buttonRemainder = [UIButton buttonWithType:UIButtonTypeSystem];
buttonRemainder.frame = frameRemainder;
buttonRemainder.layer.borderWidth = 1;
[buttonRemainder setTitle:@"%" forState:UIControlStateNormal];
[buttonRemainder addTarget:self action:@selector(clickRemainder:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonRemainder];
// 小数点
CGRect framePoint = CGRectMake(150, 300, 50, 50);
UIButton *buttonPoint = [UIButton buttonWithType:UIButtonTypeSystem];
buttonPoint.frame = framePoint;
buttonPoint.layer.borderWidth = 1;
[buttonPoint setTitle:@"." forState:UIControlStateNormal];
[buttonPoint addTarget:self action:@selector(clickPoint:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonPoint];
// 四则运算键位
CGRect frameAdd = CGRectMake(200, 100, 50, 50);
UIButton *buttonAdd = [UIButton buttonWithType:UIButtonTypeSystem];
buttonAdd.frame = frameAdd;
buttonAdd.layer.borderWidth = 1;
[buttonAdd setTitle:@"+" forState:UIControlStateNormal];
[buttonAdd addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonAdd];
CGRect frameSub = CGRectMake(200, 150, 50, 50);
UIButton *buttonSub = [UIButton buttonWithType:UIButtonTypeSystem];
buttonSub.frame = frameSub;
buttonSub.layer.borderWidth = 1;
[buttonSub setTitle:@"-" forState:UIControlStateNormal];
[buttonSub addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonSub];
CGRect frameMul = CGRectMake(200, 200, 50, 50);
UIButton *buttonMul = [UIButton buttonWithType:UIButtonTypeSystem];
buttonMul.frame = frameMul;
buttonMul.layer.borderWidth = 1;
[buttonMul setTitle:@"X" forState:UIControlStateNormal];
[buttonMul addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonMul];
CGRect frameDiv = CGRectMake(200, 250, 50, 50);
UIButton *buttonDiv = [UIButton buttonWithType:UIButtonTypeSystem];
buttonDiv.frame = frameDiv;
buttonDiv.layer.borderWidth = 1;
[buttonDiv setTitle:@"/" forState:UIControlStateNormal];
[buttonDiv addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonDiv];
// 等于键(全是为他服务的,老大啊)
CGRect frameEql = CGRectMake(200, 300, 50, 50);
UIButton *buttonEql = [UIButton buttonWithType:UIButtonTypeSystem];
buttonEql.frame = frameEql;
buttonEql.layer.borderWidth = 1;
[buttonEql setTitle:@"=" forState:UIControlStateNormal];
[buttonEql addTarget:self action:@selector(clickEql:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonEql];
self.label.text = @"0";
// 初值
self.a = 0;
self.b = 0;
self.isOn = 0;
self.p = nil;
}
- (void)clickRemainder:(UIButton *)button{
self.a = self.label.text.doubleValue;
if (![self.label.text isEqualToString:@"0"]) {
self.label.text = [NSString stringWithFormat:@"%.14lg", self.a / 100];
}
}
#pragma mark 清空
- (void)clickAc:(UIButton *)button{
self.a = 0;
self.b = 0;
self.p = nil;
self.isOn = 0;
self.label.text = @"0";
[button setTitle:@"Ac" forState:UIControlStateNormal];
}
#pragma mark 运算(取函数,取a值)
- (void)click:(UIButton *)button{
if (self.p && !self.a) {
self.a = self.p(self.a, self.label.text.doubleValue);
self.label.text = [NSString stringWithFormat:@"%.14g", self.a];
}else{
self.a = self.label.text.doubleValue;
}
// self.label.text = @"0";
self.isOn = 1;
if ([button.currentTitle isEqualToString:@"+"]) {
self.p = ^(double a, double b){
return a + b;
};
}
if ([button.currentTitle isEqualToString:@"-"]) {
self.p = ^(double a, double b){
return a - b;
};
}
if ([button.currentTitle isEqualToString:@"X"]) {
self.p = ^(double a, double b){
return a * b;
};
}
if ([button.currentTitle isEqualToString:@"/"]) {
self.p = ^(double a, double b){
return a / b;
};
}
self.b = 0;
}
#pragma mark 小数点
- (void)clickPoint:(UIButton *)button{
UIButton *buttonAc = (UIButton *)[self.view viewWithTag:1000];
[buttonAc setTitle:@"C" forState:UIControlStateNormal];
if (![self.label.text rangeOfString:@"."].length) {
self.a = self.label.text.doubleValue;
self.label.text = [NSString stringWithFormat:@"%.14lg.",self.a];
}
}
#pragma mark 正负改变
- (void)clickNegative:(UIButton *)button{
self.a = self.label.text.doubleValue;
if (![self.label.text isEqualToString:@"0"]) {
self.label.text = [NSString stringWithFormat:@"%.14lg", - self.a];
}
}
#pragma mark 数字输入
- (void)clickOne:(UIButton *)button{
if (self.isOn) {
self.label.text = @"0";
self.isOn = 0;
}
UIButton *buttonAc = (UIButton *)[self.view viewWithTag:1000];
[buttonAc setTitle:@"C" forState:UIControlStateNormal];
if ([self.label.text rangeOfString:@"e"].length) {
self.label.text = @"0";
}
if ([self.label.text isEqualToString:@"0"]) {
self.label.text = button.currentTitle;
} else {
NSString *text = [NSString stringWithFormat:@"%@%@", self.label.text, button.currentTitle];
self.label.text = [NSString stringWithFormat:@"%.14lg", text.doubleValue];
}
}
#pragma mark 结果(取b的值,函数调用)
- (void)clickEql:(UIButton *)button{
if (!self.b) {
self.b = self.label.text.doubleValue;
}
if (self.p != nil) {
self.label.text = [NSString stringWithFormat:@"%.14g", self.p(self.a , self.b)];
self.a = self.label.text.doubleValue;
}
}
- (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