惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
Jina AI
Jina AI
博客园_首页
J
Java Code Geeks
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
S
SegmentFault 最新的问题
MyScale Blog
MyScale Blog
P
Proofpoint News Feed
L
Lohrmann on Cybersecurity
Forbes - Security
Forbes - Security
大猫的无限游戏
大猫的无限游戏
Vercel News
Vercel News
Y
Y Combinator Blog
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
N
News | PayPal Newsroom
S
Security Archives - TechRepublic
量子位
Cisco Talos Blog
Cisco Talos Blog
V
V2EX
C
Cisco Blogs
The Cloudflare Blog
Stack Overflow Blog
Stack Overflow Blog
L
LangChain Blog
Scott Helme
Scott Helme
S
Securelist
Security Latest
Security Latest
爱范儿
爱范儿
TaoSecurity Blog
TaoSecurity Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
I
Intezer
L
LINUX DO - 最新话题
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Check Point Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
美团技术团队
Know Your Adversary
Know Your Adversary
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
PCI Perspectives
PCI Perspectives
月光博客
月光博客
T
Tailwind CSS Blog
Cloudbric
Cloudbric
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
K
Kaspersky official blog
D
DataBreaches.Net
博客园 - 【当耐特】
有赞技术团队
有赞技术团队

博客园 - K3

什么是URL,URI或URN? 软件设计思想的一些文章 (持续补充中) Mac OS X 上Lua的安装方法 Objective C - 4 - 下载图片并且加载到View Objective C - 2 - 随机数,可变字符串,字符串,SubString Objective C - 1 - 实现一个MessageBox.Show MySQL 存储过程,游标,临时表创建 List connected users–similar to task manager How to compose namespaces? Hide/Show running Console Calculate drive total/free/available space C# list installed softwares How to: Modify a Project System So That Projects Load in Multiple Versions of Visual Studio PS:WINRAR制作32位安装程序和64位安装程序选项 使用WINRAR来制作安装程序 impersonate a user VBScript - CUD registry key and value mysql script for dynamic running sql script XmlElement可以避免由XmlSerializer多余生成的代码
Objective C - 3 - 实现一个计算器
K3 · 2014-05-29 · via 博客园 - K3

代码

//
//  CalculatorViewController.m
//  Calculator
//
//  Created by sskset on 5/28/14.
//  Copyright (c) 2014 shanke. All rights reserved.
//

#import "CalculatorViewController.h"

@interface CalculatorViewController ()
@property (weak, nonatomic) IBOutlet UITextField *resultText;
@property (nonatomic) BOOL isProcessing;
@property (nonatomic) NSMutableArray *numberStack;
@property (nonatomic) NSMutableArray *operandStack;
@end

@implementation CalculatorViewController

-(NSMutableArray *)operandStack
{
    if(!_operandStack)
        _operandStack = [[NSMutableArray alloc] init];
    return _operandStack;
}

-(NSMutableArray *)numberStack
{
    if(!_numberStack)
        _numberStack = [[NSMutableArray alloc] init];
    
    return _numberStack;
}

- (IBAction)operandPressed:(UIButton *)sender {
    
    if([self isAvailableToCalculate])
    {
        [self doCalculate];
    }
    else
    {
        [self.numberStack addObject:self.resultText.text];
    }
    
    [self.operandStack addObject:sender.titleLabel.text];
    self.isProcessing = NO;
}

- (IBAction)numberPressed:(UIButton *)sender {
    
    if (self.isProcessing) {
        self.resultText.text =
        [self.resultText.text stringByAppendingString:sender.titleLabel.text];
    }
    else{
        self.resultText.text = sender.titleLabel.text;
        self.isProcessing = YES;
    }
}

- (IBAction)resetPressed:(id)sender {
    
    self.isProcessing = NO;
    
    [self.numberStack removeAllObjects];
    [self.operandStack removeAllObjects];
    
    self.resultText.text = @"0";
}

-(void)doCalculate
{
    NSString *operand = [self.operandStack lastObject];
    
    if(operand)
    {
        NSString *outObject = [self.numberStack lastObject];
        int outputInteger = outObject ? [outObject intValue] : 0;
        [self.numberStack removeLastObject];
        
        int resultInteger = 0;
        
        if ([operand isEqualToString:@"+"])
        {
            resultInteger =  [self.resultText.text intValue] + outputInteger;
        }
        else if ([operand isEqualToString:@"-"])
        {
            resultInteger = outputInteger - [self.resultText.text intValue];
        }
        else if ([operand isEqualToString:@"*"])
        {
            resultInteger = outputInteger * [self.resultText.text intValue];
        }
        else
        {
            resultInteger = [self.resultText.text intValue] == 0 ? 0 : outputInteger / [self.resultText.text intValue];
        }
        
        [self.operandStack removeLastObject];
        
        NSString *resultString = [NSString stringWithFormat:@"%d", resultInteger];
        self.resultText.text = resultString;
        [self.numberStack addObject:resultString];
    }
}

-(BOOL)isAvailableToCalculate
{
    return [self.operandStack lastObject] != nil;
}

- (IBAction)enterPressed:(id)sender
{
    [self doCalculate];
}

@end

界面

posted on 2014-05-29 05:12  K3  阅读(1761)  评论()    收藏  举报