[Stanford] RPN calculator (overall design)

Source: Internet
Author: User
Tags rpn calculator

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)

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.