RPN (reverse Polish Notation), a reverse polish expression.
RPN calculator is a calculator that uses the reverse Polish notation method of user interaction, which is typically popular with people who deal with financial, engineering or other technical problems.
1) The function of the calculator:
If no parentheses are used, input numbers are pushed into the stack. Enter the addition, subtraction, multiplication, division operator to calculate the operation result of the top element of the stack and press the result into the stack. Prepare for the next operation.
For example, input 5, input 4, input +, then the result in the stack is 9 (of course there can be more complex input)
2) Overall Design
1) Model Design (calculatorbrain)
The model file is designed as follows: Use a private stack (nsmutablearray) to store the operands and intermediate results, use the pushoperand method to push the operands to the stack, use the performoperation method to calculate and return double results.
Public part:
@interface CalculatorBrain : NSObject -(void)pushOperand:(double)operand; -(double)performOperation:(NSString *)operation;@end
PRIVATE:
@ Interface calculatorbrain () // Private @ property (nonatomic, strong) nsmutablearray * operandstack; // It must be strong, because only the pointer is used, strong will tell the compiler how to manage the memory. When the value of property creation is 0 or nil, no message is sent to nil @ end.
2) controller design (calculatorviewcontroller)
The Controller file is designed as follows: Set the label in the view as outlet and use the outlet as the controller attribute to control the display on The View; the digitpressed method is used to control the display of the label after the digit key is pressed. The operationpressed method controls the operation process of pressing the "+", "-", "*", and "/" keys; the enterpressed method adds the operands to the stack of the stack model.
Public part:
@ Interface calculatorviewcontroller: uiviewcontroller @ property (weak, nonatomic) iboutlet uilabel * display; // display ------ label, View to the Controller outlet @ end
PRIVATE:
@ Interface calculatorviewcontroller () @ property (nonatomic) bool userisinthemiddleofenteringanumber; // The initial value is 0 and belongs to the private @ property (nonatomic, strong) calculatorbrain * brain; @ end
Most of the private implementation methods are the target action methods implemented by the view and controller:
@implementation CalculatorViewController- (IBAction)digitPressed:(UIButton *)sender;- (IBAction)OperationPressed:(UIButton *)sender;- (IBAction)enterPressed;@end
3) view
Enter "56 enter 3 +", and the result "59" appears"
[Stanford] RPN calculator (overall design)