[课堂实践与项目]IOS只能进行简单的加减乘除的没有优先级的计算器
//
// 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
[课堂实践与项目]IOS只能进行简单的加减乘除的没有优先级的计算器的更多相关文章
- [课堂实践与项目]IOS优先级的计算器
这个计算器主要是使用数组进行实现的.虽然没有使用前缀后缀表达式,但是是一种方法o. .h文件 // // LCViewController.h // 具有优先级的calculator // // Cr ...
- [课堂实践与项目]手机QQ客户端--4期(SQLite的加入,注册,找回,登录界面的修改):建立关于QQ注册类,使用SQLite进行存储,
经过昨天下午和今天上午的不懈努力,终于通过了SQLite的学习. 我们现在这里定义一个有关SQLIte的封装类,便于我在后面的用户注册,用户密码找回,和登录界面的使用 1.首先我们看看我们建立的use ...
- [课堂实践与项目]NavigationController与TabBarController的综合使用及易错点分析(包含消息提醒,app更新)
陈述:我们在使用tabbarController的时候,我们总会和NavagationController联合起来.但是不联合的时候又是什么样的一种pool的情况呢?我们就单单的 TabBarCont ...
- 2016-2017-2 《Java 程序设计》课堂实践项目
目录 基本工具 基础内容 Hello World 和 模块分解 数组的使用 命令行参数 递归 分支语句 String类的使用 类的定义与测试 多态 IO与异常 数据库 网络与安全 数据结构应用 And ...
- 《Java 程序设计》课堂实践项目 课后学习总结
<Java 程序设计>课堂实践项目 课后学习总结 String类的使用(sort) 目录 Linux命令(sort) 课堂实践 课后思考 学习老师的代码之后的思考:int与Integer ...
- 《Java 程序设计》课堂实践项目-类定义
<Java 程序设计>课堂实践项目类定义 课后学习总结 目录 改变 类定义实验要求 课堂实践成果 课后思考 改变 修改了博客整体布局,过去就贴个代码贴个图很草率,这次布局和内容都有修改. ...
- 20155308 2016-2017-2《Java程序设计》课堂实践项目
20155308 2016-2017-2<Java程序设计>课堂实践项目 在java.lang包中有String.split()方法,返回是一个数组 我在应用中用到一些,给大家总结一下,仅 ...
- 《Java 程序设计》课堂实践项目-命令行参数
<Java 程序设计>课堂实践项目 课后学习总结 目录 改变 命令行参数实验要求 课堂实践成果 课后思考 改变 修改了博客整体布局,过去就贴个代码贴个图很草率,这次布局和内容都有修改.加了 ...
- 《Java 程序设计》课堂实践项目-mini dc
<Java 程序设计>课堂实践项目-后缀表达式 课后学习总结 目录 改变 mini dc实验要求 后缀表达式介绍 课堂实践成果 课后思考 改变 修改了博客整体布局,改变了之前贴个截图粘个代 ...
随机推荐
- javascript 学习随笔2
<html> <head> <script type="text/javascript"> function writeText(txt) { ...
- AWVS介绍(转)
使用AWVS对域名进行全局分析,深入探索: 首先,介绍一下AWVS这个工具. Acunetix Web Vulnerability Scanner(简称AWVS)是一款知名的网络漏洞扫描工具,它通过网 ...
- Nginx 因 Selinux 服务导致无法远程訪问
文章来源:http://blog.csdn.net/johnnycode/article/details/41947581 2014-12-16日 昨天晚上处理好的网络訪问连接.早晨又訪问不到了. 现 ...
- BNU 26579 Andrew the Ant 【蚂蚁】
链接: http://www.bnuoj.com/bnuoj/problem_show.php?pid=26579 http://www.bnuoj.com/bnuoj/contest_show.ph ...
- block 解析 - 形参变量
block形参 之前漏了一篇block形参的介绍,这里给补上. block形参就是定义block带的参数,和函数的参数使用一样,我们可以在block随意使用修改block形参. 我们来看个例子: 我们 ...
- Android Bluetooth开发
原文地址:http://developer.android.com/guide/topics/wireless/bluetooth.html 翻译:jykenan 更新:2012.06.19 Andr ...
- Security:蠕虫的行为特征描述和工作原理分析
________________________ 参考: 百度文库---蠕虫的行为特征描述和工作原理分析 http://wenku.baidu.com/link?url=ygP1SaVE4t4-5fi ...
- 使用java对执行命令行 或 执行bat文件
public class Hellotianhao { public static void main(String[] args) throws Exception{ System.out.prin ...
- CentOS 漏洞修补
以前没注意 今后得实时更新系统漏洞和补丁了! 1.Bash软件安全漏洞检测及解决方案 http://netsecurity.51cto.com/art/201409/452322.htm
- iOS UITextField垂直居中
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;