[iOS]

Source: Internet
Author: User
Tags rpn calculator

Description

The previous RPN calculator was done according to the course, which is the calculation of the suffix expression. Now this calculator is the calculation of infix expression, and the calculation process is also displayed on the screen, is a general calculator.

Design method:

In the model with two stacks, one is the digital stack, one is the operation Fu Yi, if the number is pressed into the Fu Yi, check the top element of the operation is not multiplication sign and division sign, if it is multiplication sign and division sign to calculate the calculation results, and finally, when the "Equals key" calculation results, operation Ching only the plus and minus sign.

Final "View" as follows:

The codes of the "Model":

//OrdinaryCalculatorBrain.h//Ordinarycalculator////Created by Lvxy on 10/4/14.//Copyright (c) lvxy. All rights reserved.//#import<UIKit/UIKit.h>@interfaceOrdinarycalculatorbrain:nsobject-(void) Pushoperation: (NSString *) operation;// + - * /-(void) Pushoperand: (Double) operand;-(Double) equal;-(void) ClearAll;@end//ordinarycalculatorbrain.m//Ordinarycalculator////Created by Lvxy on 10/4/14.//Copyright (c) lvxy. All rights reserved.//#import "OrdinaryCalculatorBrain.h"@interfaceOrdinarycalculatorbrain () @property (nonatomic,strong) Nsmutablearray*Numberstack, @property (nonatomic,strong) Nsmutablearray*Operationstack;@end@implementationOrdinarycalculatorbrain@synthesizeNumberstack =_numberstack;@synthesizeOperationstack =_operationstack;-(nsmutablearray*) numberstack{if(_numberstack==0) {_numberstack=[[Nsmutablearray alloc]init]; }    return_numberstack;}-(nsmutablearray*) operationstack{if(_operationstack==0) {_operationstack=[[Nsmutablearray alloc]init]; }    return_operationstack;}-(void) Pushoperation: (NSString *) operation{// + - * /[Self.operationstack addobject:operation];}-(void) Pushoperand: (Double) operand{NSString*lastoperation =[Self.operationstack Lastobject]; if(Lastoperation==nil | | [Lastoperation isequaltostring:@"+"]|| [Lastoperation isequaltostring:@"-"]) {[Self.numberstack addobject:[nsnumber Numberwithdouble:operand]]; }    Else if([Lastoperation isequaltostring:@"*"]|| [Lastoperation isequaltostring:@"/"]) {[Self.operationstack removelastobject]; Doublenum2 =operand; NSNumber*lastnumber =[Self.numberstack Lastobject]; if(Lastnumber) [Self.numberstack Removelastobject]; DoubleNUM1 =[Lastnumber Doublevalue]; if([Lastoperation isequaltostring:@"*"]) {[Self.numberstack addobject:[nsnumber numberwithdouble:num1*Num2]]; }Else if([Lastoperation isequaltostring:@"/"]) {[Self.numberstack addobject:[nsnumber numberwithdouble:num1/Num2]]; }    }}-(Double) equal{Doubleresult =0;  while([Self.operationstack lastobject]!=Nil) {NSString*lastoperation =[Self.operationstack Lastobject]; if(lastoperation) [Self.operationstack Removelastobject]; DoubleNUM1 =result; NSNumber*lastnumber =[Self.numberstack Lastobject]; if(Lastnumber) [Self.numberstack Removelastobject]; Doublenum2 =[Lastnumber Doublevalue]; if([Lastoperation isequaltostring:@"+"]) {result= num1+num2; }Else if([Lastoperation isequaltostring:@"-"]) {result= num1-num2; }} NSNumber*lastnumber =[Self.numberstack Lastobject]; if(Lastnumber) [Self.numberstack Removelastobject]; Doublenum2 =[Lastnumber Doublevalue]; Result+=num2; returnresult;}-(void) clearall{if(self.numberstack) {[Self.numberstack removeallobjects]; }    if(self.operationstack) {[Self.operationstack removeallobjects]; }}@end

The codes of the "Controller":

//CalculatorViewController.h//Ordinarycalculator////Created by Lvxy on 10/4/14.//Copyright (c) lvxy. All rights reserved.//#import<UIKit/UIKit.h>@interfaceCalculatorviewcontroller:uiviewcontroller@property (Weak, nonatomic) Iboutlet UILabel*display;@end//CALCULATORVIEWCONTROLLER.M//Ordinarycalculator////Created by Lvxy on 10/4/14.//Copyright (c) lvxy. All rights reserved.//#import "CalculatorViewController.h"#import "OrdinaryCalculatorBrain.h"@interfaceCalculatorviewcontroller () @property (nonatomic,strong) Ordinarycalculatorbrain*Brain; @property (nonatomic) BOOL userisinthemiddleofenteringanumber; @property (nonatomic) NSString*currentdigit; @property (nonatomic) BOOL isnotfirstpressed;@end@implementationCalculatorviewcontroller@synthesizedisplay =_display;@synthesizeBrain =_brain;@synthesizeUserisinthemiddleofenteringanumber=_userisinthemiddleofenteringanumber;@synthesizeIsnotfirstpressed=_isnotfirstpressed;-(ordinarycalculatorbrain*) brain{if(_brain==nil) _brain=[[Ordinarycalculatorbrain alloc] init]; return_brain;}-(Ibaction) digitpressed: (UIButton *) Sender {if(self.) isnotfirstpressed==0) {Self.display.text=@""; Self. Isnotfirstpressed=1; } nsstring*digit =Sender.currenttitle; Self.display.text=[Self.display.text Stringbyappendingstring:digit]; if(self.userisinthemiddleofenteringanumber) {self.currentdigit=[Self.currentdigit Stringbyappendingstring:digit]; }Else{self.currentdigit=Digit; Self.userisinthemiddleofenteringanumber=1; }    }-(Ibaction) operationpressed: (UIButton *Sender {[Self.brain pushoperand:[self.currentdigit doublevalue]]; Self.currentdigit=@""; Self.display.text=[Self.display.text StringByAppendingString:sender.currentTitle];    [Self.brain PushOperation:sender.currentTitle]; Self.userisinthemiddleofenteringanumber=0; }-(ibaction) clearpressed {[Self.brain clearAll]; Self.userisinthemiddleofenteringanumber=0; Self. Isnotfirstpressed=0; Self.display.text=@"0";}-(Ibaction) equalpressed: (UIButton *Sender {[Self.brain pushoperand:[self.currentdigit doublevalue]]; Self.currentdigit=@""; Doubleresult =[Self.brain equal]; Self.display.text= [[Self.display.text stringbyappendingstring:@"="]stringbyappendingstring:[nsstring stringWithFormat:@"%g", result]]; Self.userisinthemiddleofenteringanumber=0;}@end

[iOS]

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.