objective-c第二十一章课后练习题
来源:互联网 发布:昆明java语言培训 编辑:程序博客网 时间:2024/05/16 04:48
// ViewController.m// Fraction_Calculator//// Created by new on 15/6/7.// Copyright (c) 2015年 All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController{ char op; int numberOps; int currentNumber;//输入中的数字 BOOL firstOperand,isNumerator;//跟踪输入的是第一个分数和第二个分数,以及输入的是分子还是分母; BOOL isNegative,writingDigit;//跟踪输入的是否是负数,以及书写位数; Calculator *myCalculator; NSMutableString *displayString;}@synthesize display;-(void)viewDidLoad{ //覆盖应用程序载入的自定义方法 firstOperand=YES; isNumerator=YES; isNegative=NO; writingDigit=NO; numberOps=0; displayString=[NSMutableString stringWithCapacity:40]; myCalculator=[[Calculator alloc]init];}-(void)errorOutDisplay{ [myCalculator clear]; [displayString setString:@"Error!"]; [display setText:displayString];}-(void)processDight:(int)digit{ if (isNegative) currentNumber=currentNumber*10-digit; else currentNumber=currentNumber*10+digit; if(!writingDigit) writingDigit=YES; [displayString appendString:[NSString stringWithFormat:@"%i",digit]]; display.text=displayString;}-(IBAction)clickDigit:(UIButton *)sender{ int digit; digit=(int)sender.tag; [self processDight:digit];}-(void)processOp:(char)theOp{ NSString *opStr; [self storeFracPart]; numberOps++; if (numberOps>1) { NSLog(@"------%i",numberOps); [myCalculator performOperation:op]; myCalculator.operand1.numerator=myCalculator.accumulator.numerator; myCalculator.operand1.denominator=myCalculator.accumulator.denominator; } if (writingDigit==NO && theOp=='-') { isNegative=YES; [displayString setString:[displayString stringByAppendingString:@"-"]]; [display setText:displayString]; return ; } op=theOp; switch (theOp) { case '+': opStr=@"+"; break; case '-': opStr=@"-"; break; case '*': opStr=@"∞"; break; case '/': opStr=@"÷"; break; } //[self storeFracPart]; firstOperand=NO; isNumerator=YES; writingDigit=NO; isNegative=NO; [displayString appendString:opStr]; display.text=displayString;}-(void)storeFracPart{ if (firstOperand) { if (isNumerator) { myCalculator.operand1.numerator=currentNumber; myCalculator.operand1.denominator=1; } else { if (currentNumber==0) [self errorOutDisplay]; myCalculator.operand1.denominator=currentNumber; } } else if (isNumerator) { myCalculator.operand2.numerator=currentNumber; myCalculator.operand2.denominator=1; } else { if (currentNumber==0) [self errorOutDisplay]; myCalculator.operand2.denominator=currentNumber; firstOperand=YES; } currentNumber=0;}-(IBAction)clickOver{ [self storeFracPart]; isNumerator=NO; isNegative=NO; [displayString appendString:@"/"]; display.text=displayString;}//算术操作键-(IBAction)clickPlus{ [self processOp:'+'];}-(IBAction)clickMinus{ [self processOp:'-'];}-(IBAction)clickMultiply{ [self processOp:'*'];}-(IBAction)clickDivide{ [self processOp:'/'];}//Misc键-(IBAction)clickEquals{ if (firstOperand==NO) { [self storeFracPart]; [myCalculator performOperation:op]; [displayString appendString:@"="]; [displayString appendString:[myCalculator.accumulator convertToString]]; display.text=displayString; currentNumber=0; isNumerator=YES; firstOperand=YES; numberOps=0; [displayString setString:@""]; }}-(IBAction)clickClear{ numberOps=0; isNumerator=YES; firstOperand=YES; currentNumber=0; [myCalculator clear]; [displayString setString:@""]; display.text=displayString;}-(IBAction)Convert{ if(firstOperand && isNumerator &&!currentNumber) { [displayString setString:[NSString stringWithFormat:@"%g",[myCalculator.accumulator convertToNum]]]; [display setText:displayString]; }}- (void)viewDidUnLoad { [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
// ViewController.h// Fraction_Calculator//// Created by new on 15/6/7.// Copyright (c) 2015年 All rights reserved.//#import <UIKit/UIKit.h>#import "Calculator.h"@interface ViewController : UIViewController@property(strong,nonatomic)IBOutlet UILabel *display;-(void)processDight:(int)digit;-(void)processOp:(char)theOp;-(void)storeFracPart;-(void)errorOutDisplay;//数字键-(IBAction)clickDigit:(UIButton*)sender;//算术操作键-(IBAction)clickPlus;-(IBAction)clickMinus;-(IBAction)clickMultiply;-(IBAction)clickDivide;//Misc键-(IBAction)clickOver;-(IBAction)clickEquals;-(IBAction)clickClear;-(IBAction)Convert;@end
0 0
- objective-c第二十一章课后练习题
- [学习笔记—Objective-C]《Objective-C 程序设计 第6版》第四章 数据类型和表达式 课后练习题 4-10
- objective-c第十六章练习题
- objective-c第四章课后练习答案
- objective-C第十五章课后练习答案
- objective-c第十三章练习题答案
- 课后练习题(第二章)
- C和指针课后练习题总结
- objective_c 第九章课后练习题答案
- 第四章课后练习题第17题
- Objective-C实验练习题汇总(一)
- Objective-c语言_数组练习题1
- Objective-c语言_数组练习题2:
- Objective-C语言_数组练习题
- Objective-c语言_练习题3
- Objective-c语言_练习题3
- [学习笔记—Objective-C]《Objective-C 程序设计 第6版》第五章 循环结构 部分例题&课后题讲解
- [学习笔记—Objective-C]《Objective-C 程序设计 第6版》第七章 类 课后题 7-6 解析
- Core Java 笔记(二)
- MFC中的MainFrame Dlg,App,Doc,View的关系
- 使用Hive的正则解析器RegexSerDe分析nginx日志
- POJ 1149 PIGS 最大流建图
- Android触摸屏事件派发机制详解与源码分析
- objective-c第二十一章课后练习题
- Java 字符串计算频率出现最高的字符
- web.xml详解之Servlet和URL路径
- Division - HDU 3480 斜率优化,四边形不等式优化
- 大话数据结构—二叉排序树
- unity3d中,数字转换成字符串的方法
- 流媒体分发方式比较
- POJ 2823 Sliding Window滑窗
- js捕获鼠标滚动事件