• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Objective C - 3 - 实现一个计算器

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

代码

//
//  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

界面

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
objective-cdelegateprotocol发布时间:2022-07-12
下一篇:
Objective-C语法之第一个iPhone应用程序的那些事儿(十)发布时间:2022-07-12
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap