把 计算器里的方法 封装一个类
#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;