[Stanford] RPN calculator (model improved version)

Source: Internet
Author: User
Tags rpn calculator

In the previous model, the operands were pushed into the stack. When the addition, subtraction, multiplication, division, and division numbers were directly calculated, the operations and addition, multiplication, multiplication, division, were pushed into the stack, and the interfaces in the Controller remained unchanged:

Calculatorbrain. h

#import <UIKit/UIKit.h>@interface CalculatorBrain : NSObject-(void)pushOperand:(double)operand;-(double)performOperation:(NSString *)operation;@property(readonly)id program;+(double)runProgram:(id)program;@end

 Calculatorbrain. m

# Import "calculatorbrain. H "@ interface calculatorbrain () @ property (nonatomic, strong) nsmutablearray * programstack; // The original operandstack can only store the number of operations, // This programstack can save both the number of operations and operator numbers. @ End @ implementation calculatorbrain @ synthesize programstack = _ programstack;-(nsmutablearray *) programstack // The getter method is required for delayed initialization {If (_ programstack = nil) _ programstack = [[nsmutablearray alloc] init]; return _ programstack;}-(void) pushoperand :( double) operand // the operand is added to the stack {[self. programstack addobject: [nsnumber numberwithdouble: operand];}-(double) operation moperation :( nsstring *) operation // you only need to import the operator into the stack and then run the computing program. Return a value {[self. programstack addobject: Operation] // Add the operator to the stack, so that the stack has the operand and operator. Return [calculatorbrain runprogram: Self. program] // the return value here is the calculation result, so you need to run the computing program runprogram}-(ID) program // getter method {return [self. programstack copy]; // copy does two things. First, it generates a copy, so it no longer submits the original internal data. // second, will the copy of a mutable array return an immutable array? It cannot be modified. Now you get the program after calling it. Even if you use introspection, you can't do anything to it .} + (Double) popoperandoffstack :( nsmutablearray *) stack // recursive call. The parameter here is not ID, but nsmutablearray. The Compiler checks {double result = 0; id topofstack = [stack lastobject]; // If the stack is nil, nil if (topofstack) [stack removelastobject] is returned; // If topofstack is nil, then this sentence will not be executed if ([topofstack iskindofclass: [nsnumber class]) {result = [topofstack doublevalue];} else if ([topofstack iskindofclass: [nsstring class]) {nsstring * operation = T Opofstack; If ([operation isequaltostring: @ "+"]) {result = [self popoperandoffstack: Stack] + [self poperandoffstack: Stack];} else if ([@ "*" isequaltostring: Operation]) {result = [self poperandoffstack: Stack] * [self popoperandoffstack: Stack];} else if ([@ "-" isequaltostring: Operation]) {result = [self poperandoffstack: Stack]-[self popoperandoffstack: Stack];} else if ([@ "/" isequaltostring: Operation ]) {Result = [self poperandoffstack: Stack]/[self popoperandoffstack: Stack] ;}} return result ;}+ (double) runprogram :( ID) program // modify, so that not only the number of operators and operators can be imported into the stack, but also the stack variables. for example, {nsmutablearray * stack such as X and Y; // create a local variable if ([Program iskindofclass: [nsarray class]) // use introspection to determine whether the program is an array, then assign the value to the stack {stack = [Program mutablecopy]; // The local variable stack does two things for it using Introspection: first, make sure it is an array, second, you have made a variable copy, so you can eat it} // because the runprogram implementation method is to use recursion to digest everything on the stack, and it must be variable. Digest. (Recursion means loop until the final condition is true. The final condition is an empty array or a result is obtained .) Stack is static, while mutablecopy returns ID. ID and static types can be assigned values. The compiler will not warn you. Return [self popoperandoffstack: Stack]; // because runprogram is a class method, popoperandoffstack is also a class method, and self is a class. // Stack the computing result. If the top of the stack is a number, a number is returned. If it is an operator, the value is evaluated .} // If it is not a number or string "+" "-" * "/", it will not crash. Because the IF and else if in popoperandoffstack are not satisfied, return result is executed, that is, 0. Therefore, we are not afraid to pass in the garbage array. This is very important.

 

[Stanford] RPN calculator (model improved version)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.