//
// ViewController.m
// IOS150630_UI_Button计算器
//
// Created by PengJunlong on 15/6/30.
// Copyright (c) 2015年 Peng Junlong. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
{
UILabel *_display;
NSString *_operator1;
NSString *_operator;
NSString *_operator2;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//创建UI界面
_display = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, self.view.frame.size.width-20, 100)];
_display.backgroundColor = [UIColor grayColor];
_display.alpha = 0.8;
_display.textAlignment = NSTextAlignmentRight;
_display.numberOfLines = 2;
_display.text = @"";
_operator = @"";
_operator1 = @"";
_operator2 = @"";
[self.view addSubview:_display ];
NSArray *titleArray1 = @[@"MC",@"M+",@"M-",@"MR",@"清除"];
NSArray *titleArray2 = @[@[@"7",@"8",@"9",@"+"],@[@"4",@"5",@"6",@"-"],@[@"1",@"2",@"3",@"*"],@[@"0",@".",@"=",@"/"]];
CGFloat size1 = (self.view.frame.size.width-60)/5;
CGFloat size2 = (self.view.frame.size.width-50)/4;
for (int i=0; i<5; i++)
{
CGRect frame = CGRectMake(10+i*(size1+10), 180, size1, 50);
[self createButtonByFrame:frame withTitle:titleArray1[i]];
}
for (int i=0; i<4; i++)
{
for (int j=0; j<4; j++)
{
[self createButtonByFrame:CGRectMake(10+j*(size2+10), 240+i*60, size2, 50) withTitle:titleArray2[i][j]];
}
}
}
- (void)createButtonByFrame:(CGRect)frame withTitle:(NSString *)title
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = frame;
btn.backgroundColor = [UIColor grayColor];
btn.alpha = 0.8;
btn.layer.cornerRadius = 10;
[btn setTitle:title forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont systemFontOfSize:25];
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)btnClicked:(UIButton *)btn
{
NSString *title = btn.currentTitle;
if ([title hasPrefix:@"M"])
{
}
if ([title isEqualToString:@"清除"])
{
_display.text = @"";
_operator = @"";
_operator1 = @"";
_operator2 = @"";
return;
}
if ([self isOperator:title])
{
if (_operator.length)//判断操作运算符是否已经存在
{
float result = 0.0;
float value1 = [_operator1 floatValue];
float value2 = [_operator2 floatValue];
unichar ch = [_operator characterAtIndex:0];
switch (ch)
{
case '+':
result = value1+value2;
break;
case '-':
result = value1-value2;
break;
case '*':
result = value1*value2;
break;
case '/':
result = value1/value2;
break;
default:
break;
}
_operator1 = [NSString stringWithFormat:@"%g",result];
_display.text = [NSString stringWithFormat:@"%@\n%@",_operator1,title];
_operator2 = @"";
if ([btn.currentTitle isEqualToString:@"="])
{
_operator = @"";
}
else
{
_operator = title;
}
}
else
{
_operator = title;
_display.text = [NSString stringWithFormat:@"%@\n%@",_operator1,_operator];
}
}
else//是操作数
{
if (_operator.length)//判断是哪个操作数
{
if ([_operator2 containsString:@"."] && [title isEqualToString:@"."])
{
}
else
{
_operator2 = [_operator2 stringByAppendingString:title];
_display.text = [NSString stringWithFormat:@"%@\n%@%@",_operator1,_operator,_operator2];
}
}
else
{
if ([_operator1 containsString:@"."] && [title isEqualToString:@"."])
{
}
else
{
_operator1 = [_operator1 stringByAppendingString:title];
_display.text = _operator1;
}
}
}
}
//判断标题是否为运算符+ - * / =
- (BOOL)isOperator:(NSString *)title
{
if ([[NSCharacterSet characterSetWithCharactersInString:@"+-*/="] characterIsMember:[title characterAtIndex:0]])
{
return YES;
}
return NO;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
20150630_UI之计算器练习
最新推荐文章于 2020-09-15 23:40:00 发布