[课堂实践与项目]IOS优先级的计算器

[课堂实践与项目]IOS优先级的计算器

这个计算器主要是使用数组进行实现的.虽然没有使用前缀后缀表达式,但是是一种方法o.

.h文件

 

//

//  LCViewController.h

//  具有优先级的calculator

//

//  Created by lichan on 13-12-4.

//  Copyright (c) 2013年 com.lichan. All rights reserved.

//



#import <UIKit/UIKit.h>



@interface LCViewController : UIViewController

@property (retain, nonatomic) IBOutlet UITextField *textField;

@property (retain, nonatomic) IBOutlet UITextField *longTextField;



@property (strong,nonatomic)NSMutableArray *numbersArray;



@property (strong,nonatomic)NSMutableArray *operateArray;



@property (copy,nonatomic)NSString *numberTempString;



- (IBAction)longTextField:(id)sender;



- (IBAction)numberPressed:(id)sender;

- (IBAction)clearScreen:(id)sender;



- (IBAction)operatePressed:(UIButton *)sender;

- (IBAction)equalOperate:(id)sender;

@end


.m文件

 

 

//

//  LCViewController.m

//  具有优先级的calculator

//

//  Created by lichan on 13-12-4.

//  Copyright (c) 2013年 com.lichan. All rights reserved.

//



#import "LCViewController.h"



@interface LCViewController ()



@end



@implementation LCViewController



- (void)viewDidLoad

{

    [super viewDidLoad];

    _numberTempString = [[NSMutableString alloc]init];

    _numbersArray = [[NSMutableArray alloc]init];

    _operateArray = [[NSMutableArray alloc]init];

	// Do any additional setup after loading the view, typically from a nib.

}



-(void)dealloc

{

    [_numberTempString release];

    [_textField release];

    [_numbersArray release];

    [_operateArray release];

    [_longTextField release];

    [super dealloc];



}



- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}





- (IBAction)numberPressed:(id)sender {

    

    UIButton *tempButton = (UIButton *)sender;

    

    NSString *tempNumber = [tempButton titleLabel].text;//得到button的title值,以便于在textField显示

    

    [_textField setText:[NSString stringWithFormat:@"%@%@",_textField.text,tempNumber]];  //textfield 上字符串的连接,以便于形成字符串

    

     [_longTextField setText:[NSString stringWithFormat:@"%@%@",_longTextField.text,tempNumber]];

    

    _numberTempString = _textField.text;

    

    

      

}



- (IBAction)clearScreen:(id)sender {

    

    [_numbersArray removeAllObjects];

    [_operateArray removeAllObjects];

    _numberTempString = nil;

    [_textField setText:@""];

    [_longTextField setText:@""];

}



- (IBAction)operatePressed:(UIButton *)sender {

    

    UIButton *tempButton = (UIButton *)sender;

    

    NSString *temp = [tempButton titleLabel].text;//得到button的title值,以便于在textField显示



    [_longTextField setText:[NSString stringWithFormat:@"%@%@",_longTextField.text,temp]];

    

    

    

    if (_textField.text != nil) {

        

        [_numbersArray addObject:_numberTempString];

        

        NSLog(@"---_numbersArray%@",_numbersArray);



        [_textField setText:@""];

        

        [_operateArray addObject:sender.currentTitle];

        NSLog(@"_operateArray %@",_operateArray);

    }

    else if (_textField.text == nil)

    {

        [_operateArray removeLastObject];

    [_operateArray addObject:sender.currentTitle];

    NSLog(@"_operateArray %@",_operateArray);

    }

    

}



- (IBAction)equalOperate:(id)sender {

    

    UIButton *tempButton = (UIButton *)sender;

    

    NSString *temp = [tempButton titleLabel].text;//得到button的title值,以便于在textField显示

    

    [_longTextField setText:[NSString stringWithFormat:@"%@%@",_longTextField.text,temp]];

    

    if (_textField.text != nil) {

        

        [_numbersArray addObject:_numberTempString];

        

        NSLog(@"---_numbersArray%@",_numbersArray);

        

    }

    

    //----------------

NSString *result = [[NSString alloc]init];

    



    

while (_numbersArray.count >= 3 )

{

   

    NSString *firstOperate = [_operateArray objectAtIndex:0];

    NSString *sencondOperate = [_operateArray objectAtIndex:1];

    

    NSString *firstNumber = [_numbersArray objectAtIndex:0];

    NSString *sencondNumber = [_numbersArray objectAtIndex:1];

    NSString *thirdNumber = [_numbersArray objectAtIndex:2];

    

    if ([firstOperate isEqualToString:@"+"] ||[firstOperate isEqualToString:@"-"])

    {

       if ([sencondOperate isEqualToString:@"*"] ||[sencondOperate isEqualToString:@"/"])

       {

           

           if ([sencondOperate isEqualToString:@"*"]) {

               result = [self multiOperatorSymbolByOne:sencondNumber Two:thirdNumber];

  



           }

           if ([sencondOperate isEqualToString:@"/"]) {

               result = [self divOperatorSymbolByOne:sencondNumber Two:thirdNumber];

           

               

           }

           [_operateArray removeObjectAtIndex:1];

           

           [_numbersArray removeObjectAtIndex:2];

           [_numbersArray replaceObjectAtIndex:1 withObject:result];

           NSLog(@"num:%@",_numbersArray);



       }

        else

        {

            NSString *result = [[NSString alloc]init];

            if ([firstOperate isEqualToString:@"+"]) {

            result = [self plusOperatorSymbolByOne:firstNumber Two:sencondNumber];

                

                

            }

            if ([firstOperate isEqualToString:@"-"]) {

            result = [self subOperatorSymbolByOne:firstNumber Two:sencondNumber];

                    

            }

            

            [_operateArray removeObjectAtIndex:0];

            

             [_numbersArray replaceObjectAtIndex:0 withObject:result];

            [_numbersArray removeObjectAtIndex:1];

           

            NSLog(@"%@",_numbersArray);

        

        

        }

          

    }

    else

    {

        NSString *firstOperate = [_operateArray objectAtIndex:0];

        

        NSString *firstNumber = [_numbersArray objectAtIndex:0];

        NSString *sencondNumber = [_numbersArray objectAtIndex:1];

        

        

        if ([firstOperate isEqualToString:@"*"]) {

            result = [self multiOperatorSymbolByOne:firstNumber Two:sencondNumber];

 

        }

      

        if ([firstOperate isEqualToString:@"/"]) {

            result = [self divOperatorSymbolByOne:firstNumber Two:sencondNumber];

            

        }

        

        [_operateArray removeObjectAtIndex:0];

        

        [_numbersArray removeObjectAtIndex:1];

        [_numbersArray replaceObjectAtIndex:0 withObject:result];

           NSLog(@"%@",_numbersArray);

        



    }

        

    

    

}



    NSString *firstOperate = [_operateArray objectAtIndex:0];

   

    NSString *firstNumber = [_numbersArray objectAtIndex:0];

    NSString *sencondNumber = [_numbersArray objectAtIndex:1];

       NSLog(@"%@",_numbersArray);

    

 

        if ([firstOperate isEqualToString:@"*"]) {

            result = [self multiOperatorSymbolByOne:firstNumber Two:sencondNumber];

            

            

        }

        if ([firstOperate isEqualToString:@"/"]) {

            result = [self divOperatorSymbolByOne:firstNumber Two:sencondNumber];

            

        }

  

        if ([firstOperate isEqualToString:@"+"]) {

            result = [self plusOperatorSymbolByOne:firstNumber Two:sencondNumber];

            

            

        }

        if ([firstOperate isEqualToString:@"-"]) {

            result = [self subOperatorSymbolByOne:firstNumber Two:sencondNumber];

            

        }

    

     

   NSLog(@"result : %@",result);



[_longTextField setText:[NSString stringWithFormat:@"%@%@",_longTextField.text,result]];



}



    





#pragma mark 操作符号 method



- (NSString *)plusOperatorSymbolByOne:(NSString *)str1 Two:(NSString *)str2

{

    float num1 = [str1 floatValue];

    float num2 = [str2 floatValue];

    

    

    float resultNum = num1 + num2;

        

    return [NSString stringWithFormat:@"%f",resultNum];

        

}



- (NSString *)subOperatorSymbolByOne:(NSString *)str1 Two:(NSString *)str2

{

    float num1 = [str1 floatValue];

    float num2 = [str2 floatValue];

    

    

    float resultNum = num1 - num2;

    

    return [NSString stringWithFormat:@"%f",resultNum];

    

}





- (NSString *)multiOperatorSymbolByOne:(NSString *)str1 Two:(NSString *)str2

{

    float num1 = [str1 floatValue];

    float num2 = [str2 floatValue];

    

    

    float resultNum = num1 * num2;

    

    return [NSString stringWithFormat:@"%f",resultNum];

    

}



- (NSString *)divOperatorSymbolByOne:(NSString *)str1 Two:(NSString *)str2

{

    float num1 = [str1 floatValue];

    float num2 = [str2 floatValue];

    

    

    float resultNum = num1 / num2;

    

    return [NSString stringWithFormat:@"%f",resultNum];

    

}









@end


 

 

你可能感兴趣的:(ios)