[课堂实践与项目]IOS只能进行简单的加减乘除的没有优先级的计算器

//
//  LCViewController.m
//  calculator
//
//  Created by lichan on 13-12-3.
//  Copyright (c) 2013年 com.lichan. All rights reserved.
//

#import "LCViewController.h"

static int lastKey = -1;

@interface LCViewController ()

@end

@implementation LCViewController


#pragma mark numberButtonPressed method

- (IBAction)buttonPressed:(id)sender  //数字显示连接按钮
{
    
    UIButton *tempButton = (UIButton *)sender;
    
    NSString *tempNumber = [tempButton titleLabel].text;//得到button的title值,以便于在textField显示
     
    [_textField setText:[NSString stringWithFormat:@"%@%@",_textField.text,tempNumber]];  //textfield 上字符串的连接,以便于形成字符串

    self.temp = _textField.text;
    
   // NSLog(@"浮点型:%f",[self.temp floatValue]);
                       
}

- (IBAction)backButtonPressed:(id)sender {
    
    if (self.temp) {
        self.temp = [self.temp substringToIndex:self.temp.length-1];
        
        [_textField setText:self.temp];
        NSLog(@"new Temp:%@",self.temp);
    }
   
}



- (IBAction)opreatePressed:(id)sender  //操作符按钮
{
   [_textField setText:@""];
    if (!self.result)
    {
        self.result = self.temp;
        self.temp = nil;
    }
    
    self.num1 = [self.result floatValue];
    self.num2 = [self.temp floatValue];
    NSInteger opreateTag = [sender tag];
      switch (opreateTag) {
        case 1:
        {
            lastKey = 1;
            [self plusOperatorSymbol];
            break;
        }
        case 2:
        {
            lastKey = 2;
            [self subOperatorSymbol];
            break;
        }
        case 3:
        {
           
            lastKey = 3;
            [self multiOperatorSymbol];
            break;
        }
        case 4:
        {
            lastKey = 4;
            [self divOperatorSymbol];
            break;
        }
        case 5:
        {
            
            if (lastKey == 1)
            {
                [self plusOperatorSymbol];
             
            }else if(lastKey == 2)
            {
                [self subOperatorSymbol];
            
            }else if(lastKey == 3)
            {
                [self multiOperatorSymbol];

            }else if(lastKey == 4)
            {
                lastKey = 4;
                
                [self divOperatorSymbol];
            }

            
       [_textField setText:self.result];
              break;
        }
 
        case 6:
        {
            self.result = nil;
            self.temp = nil;
            
               break;

        }
            
        default:
            break;
     }
    
}


#pragma mark 操作符号 method

- (void)plusOperatorSymbol
{

    
    if (self.temp != nil)
    {
        float resultNum = _num1 + _num2;
        
        self.result  = [NSString stringWithFormat:@"%f",resultNum];
        
        self.temp = nil;
    }
    

}

- (void)subOperatorSymbol
{

    if (self.temp != nil)
    {
        float resultNum = _num1 - _num2;
        
        self.result  = [NSString stringWithFormat:@"%f",resultNum];
        
        self.temp = nil;
    }

}

- (void)multiOperatorSymbol
{

    if (self.temp != nil)
    {
        float resultNum = _num1 * _num2;
        
        self.result  = [NSString stringWithFormat:@"%f",resultNum];
        
        self.temp = nil;
    }
    
}

- (void)divOperatorSymbol
{

    if (self.temp != nil)
    {
        float resultNum = _num1 / _num2;
        
        self.result  = [NSString stringWithFormat:@"%f",resultNum];
        
        self.temp = nil;
    }
    
}

#pragma mark 系统method

- (void)dealloc
{
    [_textField release];
    
   [_result release];
   [_numberString release];
   [_temp release];
    
    
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    

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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end



最简单的优先级

.h

//
//  LCViewController.h
//  calculator
//
//  Created by lichan on 13-12-3.
//  Copyright (c) 2013年 com.lichan. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface LCViewController : UIViewController
@property (retain, nonatomic) IBOutlet UITextField *textField;

@property (copy,nonatomic)NSString *numberString;

@property (copy,nonatomic)NSString *result;

@property (copy,nonatomic)NSString *temp;

@property (nonatomic) float num1;
@property (nonatomic) float num2;

- (IBAction)buttonPressed:(id)sender;


- (IBAction)backButtonPressed:(id)sender;


-(IBAction)opreatePressed:(id)sender;

@end

.m文件

//
//  LCViewController.m
//  calculator
//
//  Created by lichan on 13-12-3.
//  Copyright (c) 2013年 com.lichan. All rights reserved.
//

#import "LCViewController.h"

static int lastKey = -1;

@interface LCViewController ()

@end

@implementation LCViewController


#pragma mark numberButtonPressed method

- (IBAction)buttonPressed:(id)sender  //数字显示连接按钮
{
    
    UIButton *tempButton = (UIButton *)sender;
    
    NSString *tempNumber = [tempButton titleLabel].text;//得到button的title值,以便于在textField显示
     
    [_textField setText:[NSString stringWithFormat:@"%@%@",_textField.text,tempNumber]];  //textfield 上字符串的连接,以便于形成字符串

    self.temp = _textField.text;
    
   // NSLog(@"浮点型:%f",[self.temp floatValue]);
                       
}

- (IBAction)backButtonPressed:(id)sender {
    
    if (self.temp) {
        self.temp = [self.temp substringToIndex:self.temp.length-1];
        
        [_textField setText:self.temp];
        NSLog(@"new Temp:%@",self.temp);
    }
   
}



- (IBAction)opreatePressed:(id)sender  //操作符按钮
{
   [_textField setText:@""];
    if (!self.result)
    {
        self.result = self.temp;
        self.temp = nil;
    }
    
    self.num1 = [self.result floatValue];
    self.num2 = [self.temp floatValue];
    NSInteger opreateTag = [sender tag];
      switch (opreateTag) {
        case 1:
        {
            lastKey = 1;
            [self plusOperatorSymbol];
            break;
        }
        case 2:
        {
            lastKey = 2;
            [self subOperatorSymbol];
            break;
        }
        case 3:
        {
           
            lastKey = 3;
            [self multiOperatorSymbol];
            break;
        }
        case 4:
        {
            lastKey = 4;
            [self divOperatorSymbol];
            break;
        }
        case 5:
        {
            
            if (lastKey == 1)
            {
                [self plusOperatorSymbol];
             
            }else if(lastKey == 2)
            {
                [self subOperatorSymbol];
            
            }else if(lastKey == 3)
            {
                [self multiOperatorSymbol];

            }else if(lastKey == 4)
            {
                lastKey = 4;
                
                [self divOperatorSymbol];
            }

            
       [_textField setText:self.result];
              break;
        }
 
        case 6:
        {
            self.result = nil;
            self.temp = nil;
            
               break;

        }
            
        default:
            break;
     }
    
}


#pragma mark 操作符号 method

- (void)plusOperatorSymbol
{

    
    if (self.temp != nil)
    {
        float resultNum = _num1 + _num2;
        
        self.result  = [NSString stringWithFormat:@"%f",resultNum];
        
        self.temp = nil;
    }
    

}

- (void)subOperatorSymbol
{

    if (self.temp != nil)
    {
        float resultNum = _num1 - _num2;
        
        self.result  = [NSString stringWithFormat:@"%f",resultNum];
        
        self.temp = nil;
    }

}

- (void)multiOperatorSymbol
{

    if (self.temp != nil)
    {
        float resultNum = _num1 * _num2;
        
        self.result  = [NSString stringWithFormat:@"%f",resultNum];
        
        self.temp = nil;
    }
    
}

- (void)divOperatorSymbol
{

    if (self.temp != nil)
    {
        float resultNum = _num1 / _num2;
        
        self.result  = [NSString stringWithFormat:@"%f",resultNum];
        
        self.temp = nil;
    }
    
}

#pragma mark 系统method

- (void)dealloc
{
    [_textField release];
    
   [_result release];
   [_numberString release];
   [_temp release];
    
    
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    

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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值