//
// LCViewController.m
// calculator
//
// Created by lichan on 13-12-3.
// Copyright (c) 2013年 com.lichan. All rights reserved.
// #import "LCViewController.h" static int lastKey = -1; @interface LCViewController () @end @implementation LCViewController #pragma mark numberButtonPressed method - (IBAction)buttonPressed:(id)sender //数字显示连接按钮
{ UIButton *tempButton = (UIButton *)sender; NSString *tempNumber = [tempButton titleLabel].text;//得到button的title值,以便于在textField显示 [_textField setText:[NSString stringWithFormat:@"%@%@",_textField.text,tempNumber]]; //textfield 上字符串的连接,以便于形成字符串 self.temp = _textField.text; // NSLog(@"浮点型:%f",[self.temp floatValue]); } - (IBAction)backButtonPressed:(id)sender { if (self.temp) {
self.temp = [self.temp substringToIndex:self.temp.length-1]; [_textField setText:self.temp];
NSLog(@"new Temp:%@",self.temp);
} } - (IBAction)opreatePressed:(id)sender //操作符按钮
{
[_textField setText:@""];
if (!self.result)
{
self.result = self.temp;
self.temp = nil;
} self.num1 = [self.result floatValue];
self.num2 = [self.temp floatValue];
NSInteger opreateTag = [sender tag];
switch (opreateTag) {
case 1:
{
lastKey = 1;
[self plusOperatorSymbol];
break;
}
case 2:
{
lastKey = 2;
[self subOperatorSymbol];
break;
}
case 3:
{ lastKey = 3;
[self multiOperatorSymbol];
break;
}
case 4:
{
lastKey = 4;
[self divOperatorSymbol];
break;
}
case 5:
{ if (lastKey == 1)
{
[self plusOperatorSymbol]; }else if(lastKey == 2)
{
[self subOperatorSymbol]; }else if(lastKey == 3)
{
[self multiOperatorSymbol]; }else if(lastKey == 4)
{
lastKey = 4; [self divOperatorSymbol];
} [_textField setText:self.result];
break;
} case 6:
{
self.result = nil;
self.temp = nil; break; } default:
break;
} } #pragma mark 操作符号 method - (void)plusOperatorSymbol
{ if (self.temp != nil)
{
float resultNum = _num1 + _num2; self.result = [NSString stringWithFormat:@"%f",resultNum]; self.temp = nil;
} } - (void)subOperatorSymbol
{ if (self.temp != nil)
{
float resultNum = _num1 - _num2; self.result = [NSString stringWithFormat:@"%f",resultNum]; self.temp = nil;
} } - (void)multiOperatorSymbol
{ if (self.temp != nil)
{
float resultNum = _num1 * _num2; self.result = [NSString stringWithFormat:@"%f",resultNum]; self.temp = nil;
} } - (void)divOperatorSymbol
{ if (self.temp != nil)
{
float resultNum = _num1 / _num2; self.result = [NSString stringWithFormat:@"%f",resultNum]; self.temp = nil;
} } #pragma mark 系统method - (void)dealloc
{
[_textField release]; [_result release];
[_numberString release];
[_temp release]; [super dealloc];
} - (void)viewDidLoad
{
[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
最简单的优先级
.h
//
// LCViewController.h
// calculator
//
// Created by lichan on 13-12-3.
// Copyright (c) 2013年 com.lichan. All rights reserved.
// #import <UIKit/UIKit.h> @interface LCViewController : UIViewController
@property (retain, nonatomic) IBOutlet UITextField *textField; @property (copy,nonatomic)NSString *numberString; @property (copy,nonatomic)NSString *result; @property (copy,nonatomic)NSString *temp; @property (nonatomic) float num1;
@property (nonatomic) float num2; - (IBAction)buttonPressed:(id)sender; - (IBAction)backButtonPressed:(id)sender; -(IBAction)opreatePressed:(id)sender; @end
.m文件
//
// LCViewController.m
// calculator
//
// Created by lichan on 13-12-3.
// Copyright (c) 2013年 com.lichan. All rights reserved.
// #import "LCViewController.h" static int lastKey = -1; @interface LCViewController () @end @implementation LCViewController #pragma mark numberButtonPressed method - (IBAction)buttonPressed:(id)sender //数字显示连接按钮
{ UIButton *tempButton = (UIButton *)sender; NSString *tempNumber = [tempButton titleLabel].text;//得到button的title值,以便于在textField显示 [_textField setText:[NSString stringWithFormat:@"%@%@",_textField.text,tempNumber]]; //textfield 上字符串的连接,以便于形成字符串 self.temp = _textField.text; // NSLog(@"浮点型:%f",[self.temp floatValue]); } - (IBAction)backButtonPressed:(id)sender { if (self.temp) {
self.temp = [self.temp substringToIndex:self.temp.length-1]; [_textField setText:self.temp];
NSLog(@"new Temp:%@",self.temp);
} } - (IBAction)opreatePressed:(id)sender //操作符按钮
{
[_textField setText:@""];
if (!self.result)
{
self.result = self.temp;
self.temp = nil;
} self.num1 = [self.result floatValue];
self.num2 = [self.temp floatValue];
NSInteger opreateTag = [sender tag];
switch (opreateTag) {
case 1:
{
lastKey = 1;
[self plusOperatorSymbol];
break;
}
case 2:
{
lastKey = 2;
[self subOperatorSymbol];
break;
}
case 3:
{ lastKey = 3;
[self multiOperatorSymbol];
break;
}
case 4:
{
lastKey = 4;
[self divOperatorSymbol];
break;
}
case 5:
{ if (lastKey == 1)
{
[self plusOperatorSymbol]; }else if(lastKey == 2)
{
[self subOperatorSymbol]; }else if(lastKey == 3)
{
[self multiOperatorSymbol]; }else if(lastKey == 4)
{
lastKey = 4; [self divOperatorSymbol];
} [_textField setText:self.result];
break;
} case 6:
{
self.result = nil;
self.temp = nil; break; } default:
break;
} } #pragma mark 操作符号 method - (void)plusOperatorSymbol
{ if (self.temp != nil)
{
float resultNum = _num1 + _num2; self.result = [NSString stringWithFormat:@"%f",resultNum]; self.temp = nil;
} } - (void)subOperatorSymbol
{ if (self.temp != nil)
{
float resultNum = _num1 - _num2; self.result = [NSString stringWithFormat:@"%f",resultNum]; self.temp = nil;
} } - (void)multiOperatorSymbol
{ if (self.temp != nil)
{
float resultNum = _num1 * _num2; self.result = [NSString stringWithFormat:@"%f",resultNum]; self.temp = nil;
} } - (void)divOperatorSymbol
{ if (self.temp != nil)
{
float resultNum = _num1 / _num2; self.result = [NSString stringWithFormat:@"%f",resultNum]; self.temp = nil;
} } #pragma mark 系统method - (void)dealloc
{
[_textField release]; [_result release];
[_numberString release];
[_temp release]; [super dealloc];
} - (void)viewDidLoad
{
[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end