//
// 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

最新文章

  1. java并发控制:lock
  2. access查询优化
  3. 北大OJ 1001题
  4. codeforces 493A. Vasya and Football 解题报告
  5. iOS10 UI教程视图的边界与视图的框架
  6. COJ970 WZJ的数据结构(负三十)
  7. MapObject shape数据操作
  8. MySQL游标操作指南
  9. WCF技术剖析之五:利用ASP.NET兼容模式创建支持会话(Session)的WCF服务
  10. SE 2014 年4月21日(二)
  11. linux学习笔记----权限与命令之间的关系(极重要)
  12. hosts管理工具1.0发布了。。。。
  13. nodeppt的使用教程
  14. Django by example -----1总结
  15. 分布式版本控制系统Git的安装和使用
  16. MySQL--Ansible推送密钥实现免密码登录
  17. 【BZOJ2054】疯狂的馒头(并查集,线段树)
  18. MongoDB副本集配置系列一:安装MongoDB
  19. js过滤HTML标签以及&amp;nbsp;
  20. TensorFlow函数(七)tf.argmax()

热门文章

  1. ssh &quot;openssh-daemon is stopped&quot;操作之伤+sftp访问“-bash: /dev/null: Permission denied”
  2. Flask web开发 处理Session
  3. CCIE路由实验(2) -- BGP选路原则
  4. 基于visual Studio2013解决C语言竞赛题之0414特殊平方数
  5. GridView控件-01-[简单的数据显示]
  6. Linux命令: chown
  7. log4cpp的初步使用
  8. Eclipse 和 MyEclipse控制台console不停的自动跳动,跳出来解决方案
  9. JVM调优总结(九)-新一代的垃圾回收算法
  10. C语言的system函数