首页 > 代码库 > Objective C - 3 - 实现一个计算器

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

代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//
//  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

界面