-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculatorBrain.m
218 lines (170 loc) · 6.71 KB
/
CalculatorBrain.m
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//
// CalculatorBrain.m
// Calculator2
//
// Created by ching loo on 12-3-27.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "CalculatorBrain.h"
@interface CalculatorBrain()
@property (nonatomic, strong) NSMutableArray *operandStack;
@property (nonatomic, strong) NSMutableArray *operatorStack;
@property (nonatomic, strong) NSMutableArray *formulaStack;
@end
@implementation CalculatorBrain
@synthesize operandStack = _operandStack;
@synthesize operatorStack = _operatorStack;
@synthesize formulaStack = _formulaStack;
- (NSMutableArray *)operandStack
{
if (_operandStack == nil) _operandStack = [[NSMutableArray alloc] init ];
return _operandStack;
}
- (NSMutableArray *)operatorStack
{
if (_operatorStack == nil) _operatorStack = [[NSMutableArray alloc] init ];
return _operatorStack;
}
- (NSMutableArray *)formulaStack
{
if (_formulaStack == nil) _formulaStack = [[NSMutableArray alloc] init];
return _formulaStack;
}
/* - (NSMutableArray *) getDisplayContent
{
NSMutableArray *stack, *stack_temp;
stack_temp = [self.formulaStack mutableCopy];
int elementCount = [stack_temp count];
while (elementCount >= 0) {
[stack addObject:[stack_temp objectAtIndex:elementCount - 1]];
elementCount -= 1;
}
return stack;
} */ //getting an array for displaying the formula
- (void)pushFormula
{
// NSLog(@"the top of operand stack = %g", [self.operandStack lastObject]);
/* while (self.operandStack) {
[self.formulaStack addObject:[self.operandStack lastObject]];
[self.operandStack removeLastObject];
[self.formulaStack addObject:[self.operatorStack lastObject]];
[self.operatorStack removeLastObject];
[self.formulaStack addObject:[self.operandStack lastObject]];
[self.operandStack removeLastObject];
}*/
int operatorCount = [self.operatorStack count];
NSLog(@"operator count = %g", operatorCount);
while (operatorCount) {
NSLog(@"top of operand stack = %g", [[self.operandStack lastObject] doubleValue]);
[self.formulaStack addObject:[NSNumber numberWithDouble:self.popOperand]];
[self.formulaStack addObject:self.popOperator];
operatorCount -= 1;
}
[self.formulaStack addObject:[NSNumber numberWithDouble:self.popOperand]];
NSLog(@"formula stack elements count = %d", [self.formulaStack count]);
} //combining the operand stack and the operator stack
- (double)calculateTheFormula
{
double result = 0;
/* NSMutableArray *stack;
if (stack == nil) stack = [[NSMutableArray alloc] init];
int formulaCount = [self.formulaStack count];
while (formulaCount != 1) {
double topOfStack_1 = [[self.formulaStack lastObject] doubleValue];
[stack addObject:[NSNumber numberWithDouble:topOfStack_1]];
[self.formulaStack removeLastObject];
NSString * topOfStack_2 = [self.formulaStack lastObject];
[stack addObject:topOfStack_2];
[self.formulaStack removeLastObject];
formulaCount -= 2;
} //turnint the formula stack upside down
double topOfStack = [[self.formulaStack lastObject] doubleValue];
[stack addObject:[NSNumber numberWithDouble:topOfStack]];
[self.formulaStack removeLastObject]; */
//id topOfStack = [stack lastObject];
int numberOfStackElement = [self.formulaStack count];
while (numberOfStackElement != 1) {
double operand_1 = [[self.formulaStack lastObject] doubleValue];
[self.formulaStack removeLastObject];
NSString *operator = [self.formulaStack lastObject];
[self.formulaStack removeLastObject];
double operand_2 = [[self.formulaStack lastObject] doubleValue];
[self.formulaStack removeLastObject];
numberOfStackElement -= 3;
if ([operator isEqualToString:@"+"]) {
result = operand_1 + operand_2;
[self.formulaStack addObject:[NSNumber numberWithDouble:result]];
numberOfStackElement += 1;
} else if ([operator isEqualToString:@"-"]) {
result = operand_1 - operand_2;
[self.formulaStack addObject:[NSNumber numberWithDouble:result]];
numberOfStackElement += 1;
} else if ([operator isEqualToString:@"×"]) {
result = operand_1 * operand_2;
[self.formulaStack addObject:[NSNumber numberWithDouble:result]];
numberOfStackElement += 1;
} else if ([operator isEqualToString:@"/"]) {
if (operand_2) {
result = operand_1 / operand_2;
[self.formulaStack addObject:[NSNumber numberWithDouble:result]];
numberOfStackElement += 1;
}
}
}
[self.formulaStack removeLastObject];
return result;
//do the calculation things
}
- (void)pushOperand:(double)operand
{
[self.operandStack addObject:[NSNumber numberWithDouble:operand]];
}
- (double)popOperand
{
NSNumber *operandObject = [self.operandStack lastObject];
if (self.operandStack) [self.operandStack removeLastObject];
return [operandObject doubleValue];
}
- (void)pushOperator:(NSString *)operator
{
[self.operatorStack addObject:operator];
}
- (NSString *)popOperator
{
NSString *operator = [self.operatorStack lastObject];
if (self.operatorStack) [self.operatorStack removeLastObject];
return operator;
}
- (double)performOperation
{
double result = 0;
NSString *operator = self.popOperator;
while (operator) {
double operand_2 = self.popOperand;
double operand_1 = self.popOperand;
//NSLog(@"operand_1 = %g", operand_1);
//NSLog(@"operand_2 = %g", operand_2);
//NSLog(@"operator = %@", operator);
if ([operator isEqualToString:@"+"]) {
result = operand_1 + operand_2;
operator = self.popOperator;
[self.operandStack addObject:[NSNumber numberWithDouble:result]];
} else if ([operator isEqualToString:@"-"]) {
result = operand_1 - operand_2;
operator = self.popOperator;
[self.operandStack addObject:[NSNumber numberWithDouble:result]];
} else if ([operator isEqualToString:@"×"]) {
result = operand_1 * operand_2;
operator = self.popOperator;
[self.operandStack addObject:[NSNumber numberWithDouble:result]];
} else if ([operator isEqualToString:@"/"]) {
if (operand_2) {
result = operand_1 / operand_2;
operator = self.popOperator;
[self.operandStack addObject:[NSNumber numberWithDouble:result]];
}
}
}
return result;
}
@end