把  计算器里的方法 封装一个类

#import <Foundation/Foundation.h>

@interface CaculatorBrain : NSObject

- (void)pushOperation:(NSString *)operation;

- (double)result:(BOOL)secondEq;

- (void)zero;

- (void)pushNumberInStack:(double)aDouble  andBool:(BOOL)aBool;

@end

#import "CaculatorBrain.h"

@class MxlJisuanqiViewController;

@interface CaculatorBrain()

@property (strong,nonatomic)NSMutableArray *operandStack;

@property (assign,nonatomic)unichar symbol;

@property (weak,nonatomic)MxlJisuanqiViewController *controller;

@property (assign,nonatomic)double secondNumber;

@end

@implementation CaculatorBrain

- (NSMutableArray *)operandStack

{

if (!_operandStack) {

_operandStack = [NSMutableArray arrayWithCapacity:3];

}

return _operandStack;

}

- (double)outANumber

{

double number = [[self.operandStack lastObject]doubleValue];

if ([self.operandStack lastObject]){

[self.operandStack removeLastObject];

}

return number;

}

- (double)result:(BOOL)secondEq

{

NSString *opera = [NSString stringWithFormat:@"%c",self.symbol];

if (!secondEq){

if ([self.operandStack count] >1 ){

double number2 = [self outANumber];

double number1 = [self outANumber];

double result = 0;

if ([opera isEqualToString:@"+"]) result = number1 + number2;

else if ([@"-" isEqualToString:opera]) result = number1 - number2;

else if ([opera isEqualToString:@"×"]) result = number1 * number2;

else if ([opera isEqualToString:@"÷"])

{

if (number2 == 0) {

}else

{

result = number1 / number2;

}

}else if ([opera isEqualToString:@""])

{

return result = number1;

}

return result;

}

else

{

[self zero];

return  0;

}

}else {

double number = [self outANumber];

double result  = 0;

if ([opera isEqualToString:@"+"]) result = number + self.secondNumber;

else if ([@"-" isEqualToString:opera]) result = number - self.secondNumber;

else if ([opera isEqualToString:@"×"]) result = number * self.secondNumber;

else if ([opera isEqualToString:@"÷"])

{

if (self.secondNumber == 0) {

}else

{

result = number / self.secondNumber;

}

}

return  result;

}

}

- (void)zero {

[self.operandStack removeAllObjects];

}

- (void)pushNumberInStack:(double)aDouble  andBool:(BOOL)aBool {

double number = aDouble;

NSNumber *operand = [NSNumber numberWithDouble:number];

[self.operandStack addObject:operand];

if(!aBool) {

self.secondNumber = [[self.operandStack lastObject]doubleValue];}

}

- (void)pushOperation:(NSString *)operati

{

self.symbol = [operati characterAtIndex:0];

}

@end

在计算器界面里

在.h  文件里

#import <UIKit/UIKit.h>

@interface MxlJisuanqiViewController : UIViewController

@property (nonatomic,retain)NSString *disStr;

@end

在.m  文件里

#import "MxlJisuanqiViewController.h"

#import "CaculatorBrain.h"

@interface MxlJisuanqiViewController ()

@property (nonatomic, retain)  UILabel *display;

@property BOOL isUseInEnteringANumber;

@property (nonatomic)CaculatorBrain *brain;

@property BOOL isContinue;

@property BOOL secondEqual;

@property BOOL firstEqual;

@property (nonatomic,retain)NSUserDefaults*userDefaults;

@end

@implementation MxlJisuanqiViewController

@synthesize userDefaults;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {

userDefaults = [NSUserDefaults standardUserDefaults];

}

return self;

}

- (CaculatorBrain *)brain

{

if (!_brain) {

_brain = [CaculatorBrain new];

}

return _brain;

}

- (void)viewDidLoad

{

[super viewDidLoad];

NSArray *familyNames =[[NSArray alloc]initWithArray:[UIFont familyNames]];

NSArray *fontNames;

NSInteger indFamily, indFont;

NSLog(@"[familyNames count]===%d",[familyNames count]);

for(indFamily=0;indFamily<[familyNames count];++indFamily)

{

NSLog(@"Family name: %@", [familyNames objectAtIndex:indFamily]);

fontNames =[[NSArray alloc]initWithArray:[UIFont fontNamesForFamilyName:[familyNames objectAtIndex:indFamily]]];

for(indFont=0; indFont<[fontNames count]; ++indFont)

{

NSLog(@"Font name: %@",[fontNames objectAtIndex:indFont]);

}

}

//    titleView的label

UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];

titleLab.text = @"计算器";

titleLab.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];

titleLab.textColor = [UIColor whiteColor];

titleLab.textAlignment = NSTextAlignmentCenter;

self.navigationItem.titleView = titleLab;

self.display = [[UILabel alloc]initWithFrame:CGRectMake(0, 64, 320, 92)];

self.display.font = [UIFont systemFontOfSize:40.0f];

self.display.textAlignment = NSTextAlignmentRight;

self.display.text = self.disStr;

//    self.display.text = [userDefaults objectForKey:@"tf2"];

NSLog(@"%@",self.display.text);

self.display.backgroundColor = [UIColor colorWithRed:236/255. green:236/255. blue:236/255. alpha:1];

[self.view addSubview:self.display];

NSArray *shuziArr = @[@"AC",@"-",@"+",@"7",@"8",@"9",@"4",@"5",@"6",@"1",@"2",@"3",@"0",@".",@"00"];

for(int i=0; i<5; i++) {

for(int j=0; j<3; j++) {

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

if (isPhone5) {

btn.frame = CGRectMake(80*j, 156+83*i, 80, 83);

}else

{

btn.frame = CGRectMake(80*j, 156+65*i, 80, 65);

}

[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

if (i ==0 ) {

if (j ==0) {

[btn addTarget:self action:@selector(zeroPressed:) forControlEvents:UIControlEventTouchUpInside];

}else

{

[btn addTarget:self action:@selector(operationPressed:) forControlEvents:UIControlEventTouchUpInside];

}

btn.backgroundColor = [UIColor colorWithRed:120/255. green:193/255. blue:42/255. alpha:0.76];

//                btn.backgroundColor = [UIColor  lightGrayColor];

}else

{

btn.backgroundColor = [UIColor colorWithRed:236/255. green:236/255. blue:236/255. alpha:1];

[btn addTarget:self action:@selector(digitPressed:) forControlEvents:UIControlEventTouchUpInside];

}

btn.titleLabel.font= [UIFont fontWithName:@"HelveticaNeue-Thin" size:25.0];

//            btn.titleLabel.font = [UIFont systemFontOfSize:25.0f];

[btn setTitle:[shuziArr objectAtIndex:3*i+j] forState:UIControlStateNormal];

//            [btn setTitleShadowColor:[UIColor redColor] forState:UIControlStateSelected];

btn.tag = 3*i+j;

[self.view addSubview:btn];

}

}

NSArray *yunsuanArr  =@[@"÷",@"×",@"=",@"OK"];

for (int i = 0; i<4; i++) {

UIButton *buttons = [UIButton buttonWithType:UIButtonTypeRoundedRect];

if (i ==3) {

if (isPhone5) {

buttons.frame = CGRectMake(240, 156+83*i, 80, 166);

}else

{

buttons.frame = CGRectMake(240, 156+65*i, 80, 130);

}

[buttons addTarget:self action:@selector(okBtn:) forControlEvents:UIControlEventTouchUpInside];

buttons.backgroundColor = [UIColor colorWithRed:120/255. green:193/255. blue:42/255. alpha:0.76];

//            buttons.backgroundColor = [UIColor  lightGrayColor];

}else if(i ==2)

{

if (isPhone5) {

buttons.frame = CGRectMake(240, 156+83*i, 80, 83);

}else

{

buttons.frame = CGRectMake(240, 156+65*i, 80, 65);

}

[buttons addTarget:self action:@selector(equalR) forControlEvents:UIControlEventTouchUpInside];

buttons.backgroundColor =[UIColor colorWithRed:120/255. green:193/255. blue:42/255. alpha:0.76];

//            buttons.backgroundColor = [UIColor  lightGrayColor];

//            [buttons setTitleColor:[UIColor  blackColor] forState:UIControlStateNormal];

//            buttons.titleLabel.font = [UIFont fontWithName:@"Copperplate" size:25];

//            [buttons setTitle:[yunsuanArr objectAtIndex:i] forState:UIControlStateNormal];

}

else

{

if (isPhone5) {

buttons.frame = CGRectMake(240, 156+83*i, 80, 83);

}else

{

buttons.frame = CGRectMake(240, 156+65*i, 80, 65);

}

buttons.backgroundColor = [UIColor colorWithRed:120/255. green:193/255. blue:42/255. alpha:0.76];

//            buttons.backgroundColor = [UIColor  lightGrayColor];

[buttons addTarget:self action:@selector(operationPressed:) forControlEvents:UIControlEventTouchUpInside];

}

[buttons setTitleColor:[UIColor  blackColor] forState:UIControlStateNormal];

buttons.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:25.0];

//        buttons.titleLabel.font = [UIFont systemFontOfSize:25.0f];

[buttons setTitle:[yunsuanArr objectAtIndex:i] forState:UIControlStateNormal];

[self.view addSubview:buttons];

}

for (int i = 0; i<5; i++) {

UIImageView *imageview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"divider1.png"]];

if (i ==4) {

if (isPhone5) {

imageview.frame = CGRectMake(0, 155.6+83*i, 240, 1.1);

}else

{

imageview.frame = CGRectMake(0, 155.6+65*i, 240, 1.1);

}

}else

{

if (isPhone5) {

imageview.frame = CGRectMake(0, 155.6+83*i, 320, 1.1);

}else

{

imageview.frame = CGRectMake(0, 155.6+65*i, 320, 1.1);

}

}

[self.view addSubview:imageview];

}

for (int i = 0; i<3; i++) {

UIView *lineView = [[UIView alloc] init ];

if (i ==1) {

if (isPhone5) {

lineView.frame =CGRectMake(80*i+80, 156.0f, 0.60f, 415.0f);

}else

{

lineView.frame =CGRectMake(80*i+80, 156.0f, 0.60f, 330.0f);

}

}else

if (isPhone5) {

lineView.frame =CGRectMake(80*i+80, 156.0f, 0.60f, 415.0f);

}else

{

lineView.frame =CGRectMake(80*i+80, 156.0f, 0.60f, 330.0f);

}        [lineView setBackgroundColor:[UIColor lightGrayColor]];

[self.view addSubview:lineView];

}

}

-(void)viewWillAppear:(BOOL)animated

{

[super viewWillAppear:YES];

self.navigationItem.hidesBackButton = YES;

}

-(void)viewWillDisappear:(BOOL)animated

{

self.display.text = @"";

}

// 点击操作符号

-(void)operationPressed:(UIButton*)sender

{

self.firstEqual = YES;

_secondEqual = NO;

if(_isContinue) [self equalR];

[self.brain pushOperation:sender.currentTitle];

[self numberInBrain];

_isContinue = YES;

}

- (void)numberInBrain

{

_isUseInEnteringANumber = NO;

[self.brain pushNumberInStack:[self.display.text doubleValue] andBool:self.secondEqual];

}

// 清空

-(void)zeroPressed:(id)sender

{

self.display.text = @"0";

[self.brain zero];

_isContinue = NO;

_isUseInEnteringANumber = NO;

[self.brain pushNumberInStack:0.0 andBool:NO];

self.secondEqual = NO;

self.firstEqual = NO;

}

//  点击数字按钮

-(void)digitPressed:(UIButton*)sender

{

_secondEqual = NO;

NSString *digit = sender.currentTitle;

if (_isUseInEnteringANumber == YES)

{

self.display.text = [self.display.text stringByAppendingString:digit];

}

else {

if ([digit isEqualToString:@"0"]) {

self.display.text = @"0";

_isUseInEnteringANumber = NO;

}else if ([digit isEqualToString:@"."])

{

self.display.text = @"0.";

_isUseInEnteringANumber = YES;

}

else

{

self.display.text = digit;

_isUseInEnteringANumber = YES;

}

}

NSLog(@"%@",digit);

}

// 点击等号

-(void)equalR

{

if (self.firstEqual) {

_isContinue = NO;

[self numberInBrain];

double resultNumber = [self.brain result:_secondEqual];

NSLog(@"%e",resultNumber);

NSMutableString *resultStr = [NSMutableString stringWithFormat:@"%lg",resultNumber];

NSLog(@"%@",resultStr);

//           NSMutableString *resultStr = [NSMutableString stringWithFormat:@"%lf",resultNumber];

if(resultNumber>1000000000){

int count = 0;

while (resultNumber >=10) {

resultNumber/= 10;

count++;

}

[resultStr deleteCharactersInRange:NSMakeRange(0, [resultStr length])];

[resultStr appendFormat:@"%lg",resultNumber];

[resultStr appendFormat:@" E%d",count];

}

self.display.text = resultStr;

_secondEqual = YES;

}else {

NSLog(@"right!");

double resultNumber = [self.display.text doubleValue];

NSMutableString *resultStr = [NSMutableString stringWithFormat:@"%lg",resultNumber];

if(resultNumber>1000000000){

double count = 0.0;

while (resultNumber >=10) {

resultNumber /= 10.0;

count += 1.0;

}

[resultStr deleteCharactersInRange:NSMakeRange(0, [resultStr length])];

[resultStr appendFormat:@"%lg",resultNumber];

[resultStr appendFormat:@"e%g",count];

}

self.display.text = resultStr;

//       [resultStr deleteCharactersInRange:NSMakeRange(0, [resultStr length])];

}

self.firstEqual = YES;

}

-(void)okBtn:(id)sender

{

[userDefaults setObject:self.display.text forKey:@"value"];

[self.navigationController popViewControllerAnimated:YES];

}

- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

这个 计算器  没有 实现 优先级的 比较 。  有做出来的  求教 。。。 谢谢

iOS 简易计算器 纯代码相关推荐

  1. IOS之使用纯代码push ViewController

    IOS之使用纯代码push ViewController LJSettingController *set = [[LJSettingController alloc]init];[self.navi ...

  2. html居右显示语言设置,iOS开发:纯代码设置UIButton文字居左或者居右显示

    UIButton这个控件使用,作为资深的iOS开发人员来说是小儿科,但是有些时候还是需要记录一下UIButton的一些其他用法,这样方便快速解决实际问题.比如UIButton的纯代码编程的时候,设置文 ...

  3. iOS storyBoard 和 纯代码 实现 九宫格 切图

    一,在StoryBoard中实现九宫格切图: 1.把图片添加到Assets.xcassets文件夹里 2.选择这个图片 3.点击"Show Slicing" 二,用纯代码实现九宫格 ...

  4. 基于Python中tkinter做的可视化简易计算器(代码有注释,利于理解)

    Python可视化简易计算器 基于GUI库:tkinter做的一个简单计算器 开发软件 Pycharm2019.3 代码如下:(有对代码的简单分析,易于理解) import re import tki ...

  5. java实现简易计算器完整代码

    早晨起来没事,打开mac突然想写个小程序,就写来个简易的计算器,可以实现基本的运算,测试目前无bug,下面分享下自己的代码. 首先写了个简单的界面如下: 整个面板用了BorderLayout布局,分为 ...

  6. 两款简易计算器纯js版附源码下载

    效果如图: 还有一款更简易的 下载地址: csdn下载需要c币,没有c币可从微信公众号[无心有云] 发送[js版计算器],可获取百度云地址下载

  7. 关于c基础实现简易计算器的代码(加减乘除)

    输入为数字和算术符号组成的字符串,程序会根据运算级处理后输出结果,这是前段时间写的了,没有添加输入字母报错功能,当时我认为计算器上一般也没有字母按键,所以就没有写,但是现在想想计算器和这个代码实现的原 ...

  8. Qt简易计算器的代码实现

    大二用qt写的简易计算机,已经修改完bug,请放心食用 输入的时候一定用按钮,不要直接在文本框中输入 在qt中新建这几个文件,运行就OK了  main.cpp #include "calcu ...

  9. matlab做计算器纯代码,**matlab GUI-纯编程实现简单计算器**

    作者:ERIC_崔浩 clear,clc,close all chang=65; str={'1' '2' '3' '+'; '4' '5' '6' '-'; '7' '8' '9' '*'; '0' ...

最新文章

  1. 干货丨入门机器学习,从搞懂这8大经典算法开始
  2. python编程基础语法-Python编程入门基础语法详解
  3. Dijkstra和动态规划
  4. 为什么判断 n 是否为质数只需除到开平方根就行了?(直接证明)
  5. 这篇 CPU Cache,估计要消化一下
  6. 标示符和关键字的总结--希望别再犯错
  7. linux 部署php svn,Linux服务器搭建svn环境方法详解
  8. Go Concurrency Patterns: Timing out, moving on
  9. 区块链学习笔记:DAY05 如何使用公有云区块链服务
  10. 【面试题14】调整数组顺序使奇数位于偶数前面
  11. zedgraph显示最小刻度_关于ZedGraph几个难点
  12. Windows 下 FTP的搭建
  13. 奇迹单机版服务器修改,大天使之剑奇迹网页游戏 一键服务端单机版/架设教程/修改方法...
  14. 怎么写化学反应方程式?
  15. 云服务器ECS能做什么用途?
  16. 学习MySQL这一篇就够了
  17. 【小李木耳】2013年1月31日:北京!北京!空气污染,我倒是赚钱了,自己都无奈。
  18. 《“ 追梦人” 的逐梦路:探寻大学生创客群体的发展之道》
  19. 科技交流英语(2022秋)
  20. Python 抓取淘宝联盟优惠券

热门文章

  1. Zigbee基础知识
  2. SimpleQQ – WebQQ 桌面端 基于----WebQQ 2.00内核
  3. 大数据统计之卡方检验
  4. 浏览器兼容性测试应该如何做?
  5. 2018自媒体运营吸粉3大途径
  6. 计算机主机开机滴滴叫,电脑开机滴滴滴重复响怎么解决
  7. python math.log
  8. 数字化能力成关键优势,《2022中国人力资源科技发展十大趋势发布》
  9. codeforce 333B chips
  10. 自动驾驶传感器融合:激光雷达+摄像头