[课堂实践与项目]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实验要求 后缀表达式介绍 课堂实践成果 课后思考 改变 修改了博客整体布局,改变了之前贴个截图粘个代 ...
随机推荐
- dhtmlx使用学习
Var tabbar=new dhtmlXTabBar("tab","top"); tabbar.setImagePath("./tabbar/cod ...
- Pyton——int内部功能介绍
int内部功能详解: class int(object): """ int(x=0) -> integer int(x, base=10) -> intege ...
- ZOJ 3326 An Awful Problem 模拟
只有在 Month 和 Day 都为素数的时候才能得到糖 那就模拟一遍时间即可. //#pragma comment(linker, "/STACK:16777216") //fo ...
- 开发快很重要——如果只看法语或者产品结果C++似乎很强大,但是参与这个C++的开发过程,就会感觉到这种痛苦(Google也是这个看法)
开发快很重要——如果只看语法或者产品结果C++似乎很强大,但是参与这个C++的开发过程,就会感觉到这种痛苦,太慢了,太麻烦了,虽然在反复调试和优化之后,最后产品的结果可能还不错. Delphi的最大特 ...
- HDU Good Numbers (热身赛2)
转载请注明出处:http://blog.csdn.net/a1dark 分析:一道水题.找下规律就OK了.不过要注意特判一下0.因为0也是good number.这个把我坑惨了= =||| #incl ...
- 基于visual Studio2013解决C语言竞赛题之0415特殊对数
题目 解决代码及点评 这道题也是锻炼for循环,在for循环中遍历所有可能的数,然后再判断该数是不是有这样的性质 /********************************* ...
- zzuli求最大值
1786: 求最大值 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 134 Solved: 28SubmitStatusWeb Board Desc ...
- MySQL优化必须调整的10项配置
当我们被人雇来监测MySQL性能时,人们希望我们能够检视一下MySQL配置然后给出一些提高建议.许多人在事后都非常惊讶,因为我们建议他们仅仅改动几个设置,即使是这里有好几百个配置项.这篇文章的目的在于 ...
- 高级UIKit-10(UDPSocket)
[day1201_UDPSocket]:utpsocket的使用 使用UDP网络传输,是一种无连接的传输协议,不安全,一般使用在监控视频中或QQ聊天中,该网络传输就向广播传播模式,一对多. 在ios中 ...
- js中exec、test、match、search、replace、split、indesOf()用法
exec:对string进行正则处理,并返回匹配结果.array[0]为原字符串,array[i]为匹配在整个被搜索字符串中的位置. test:测试string是否包含有匹配结果,包含返回true,不 ...