计算器的功能实现的并不是很完善,仅供参考
#import "MainViewController.h"
<pre name="code" class="plain">#import "MainViewController.h"
@interface MainViewController ()
@property (nonatomic, retain) UILabel *label;
@property (nonatomic, assign) int a;
@property (nonatomic, assign) int b;
@property (nonatomic, assign) int total;
@property (nonatomic, retain) NSString *total1;
@property (nonatomic, retain) NSString *total2;
@property (nonatomic, retain) NSString *total3;
@property (nonatomic, retain) NSString *total4;
@property (nonatomic, retain) NSMutableString *string;
@end
@implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.string = [NSMutableString stringWithString:@""];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// 创建底层label
_label = [[UILabel alloc]initWithFrame:CGRectMake(50, 40, 200, 40)];
_label.userInteractionEnabled = NO;
[self.view addSubview:_label];
[_label release];
NSArray * array = [NSArray arrayWithObjects:@"1",@"2",@"3",@"+",@"4",@"5",@"6",@"-",@"7",@"8",@"9",@"*",@"0",@".",@"=",@"/", nil];
// 创建button
int a = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(50 + j * 50, 150 + i * 50, 40, 40);
button.backgroundColor = [UIColor yellowColor];
button.tag = 10000 + i * 4 + j;
// 将数组中的数据传到button上显示
[button setTitle:[array objectAtIndex:a] forState:UIControlStateNormal];
a++;
[self.view addSubview:button];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
//NSLog(@"%d",button.tag);
}
}
// 创建清除button
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(60, 380, 170, 50);
[button setTitle:@"清空" forState:UIControlStateNormal];
button.backgroundColor = [UIColor yellowColor];
[button addTarget:self action:@selector(clearButton:) forControlEvents:UIControlEventTouchUpInside];
button.tag = 1000;
[self.view addSubview:button];
}
// 相应点击button事件
- (void)buttonClick:(UIButton *)button
{
self.string = [NSMutableString stringWithString:[self.string stringByAppendingString:button.titleLabel.text]];
self.label.text = self.string;
if ( [button.titleLabel.text isEqual: @"="]) {
NSRange x = [self.string rangeOfString:@"+"];
if (x.location != NSNotFound) {
// 加号后面的
_a = [[self.string substringFromIndex:x.location + 1] intValue];
// 加号之前的
_b = [[self.string substringToIndex:x.location] intValue];
_total = _a + _b;
_total1 = [NSString stringWithFormat:@"%d",_total];
self.string = [NSMutableString stringWithString:[self.string stringByAppendingString:_total1 ]];
self.label.text = self.string;
}
NSRange jian = [self.string rangeOfString:@"-"];
// 减法
if (jian.location != NSNotFound){
// 减号之前的
_a = [[self.string substringFromIndex:jian.location + 1] intValue];
_b = [[self.string substringToIndex:jian.location] intValue];
_total = _b - _a;
_total2 = [NSString stringWithFormat:@"%d",_total];
self.string = [NSMutableString stringWithString:[self.string stringByAppendingString:_total2]];
self.label.text = self.string;
}
// 乘法
NSRange cheng = [self.string rangeOfString:@"*"];
if (cheng.location != NSNotFound) {
// 乘号后面的
_a = [[self.string substringFromIndex:cheng.location + 1] intValue];
// 乘号之前的
_b = [[self.string substringToIndex:cheng.location] intValue];
_total = _b * _a;
_total3 = [NSString stringWithFormat:@"%d",_total];
self.string = [NSMutableString stringWithString:[self.string stringByAppendingString:_total3]];
self.label.text = self.string;
}
// 除法
NSRange chu = [self.string rangeOfString:@"/"];
if (chu.location != NSNotFound) {
// 除号后面的
_a = [[self.string substringFromIndex:chu.location + 1] intValue];
// 除号之前的
_b = [[self.string substringToIndex:chu.location] intValue];
_total = _b / _a;
_total4 = [NSString stringWithFormat:@"%d",_total];
self.string = [NSMutableString stringWithString:[self.string stringByAppendingString:_total4]];
self.label.text = self.string;
}
}
}
// 相应清除button事件
- (void)clearButton:(UIButton *)button
{
if (button.tag == 1000) {
self.string = @" ";
self.label.text = @" ";
} else {
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
}
}