ios 学习之 Simple Calculator Application

Posted ybleeho

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ios 学习之 Simple Calculator Application相关的知识,希望对你有一定的参考价值。

//
//  ViewController.h
//  ocTest
//
//  Created by Hu Li on 2018/12/30.
//  Copyright ? 2018 Hu Li. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{
    
    bool operatorPressed;
    bool add;
    NSString *firstEntry;
    NSString *secondEntry;
    
}

@property (weak, nonatomic) IBOutlet UILabel *labelOutput;

- (IBAction)clearPressed:(id)sender;
- (IBAction)addPressed:(id)sender;
- (IBAction)minusPressed:(id)sender;
- (IBAction)equalsPressed:(id)sender;

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

@end

  

//
//  ViewController.m
//  ocTest
//
//  Created by Hu Li on 2018/12/30.
//  Copyright ? 2018 Hu Li. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    operatorPressed = FALSE;
    firstEntry = NULL;
    secondEntry = NULL;
}


- (IBAction)clearPressed:(id)sender {
    operatorPressed = FALSE;
     _labelOutput.text = nil;
    firstEntry = nil;
    secondEntry = nil;
    
}

- (IBAction)addPressed:(id)sender {
    add=TRUE;
    operatorPressed =TRUE;
}

- (IBAction)minusPressed:(id)sender {
    add=FALSE;
    operatorPressed =TRUE;
}

- (IBAction)equalsPressed:(id)sender {
    if(add == FALSE){
        int outPut = [firstEntry intValue] - [secondEntry intValue];
        _labelOutput.text = [NSString stringWithFormat: @"%i", outPut];
    }else{
        int outPut = [firstEntry intValue] + [secondEntry intValue];
        _labelOutput.text = [NSString stringWithFormat: @"%i", outPut];
        
    }
    operatorPressed = FALSE;
    firstEntry = nil;
    secondEntry = nil;
}
- (IBAction)numberPressed:(UIButton*)sender{
    NSInteger tag = sender.tag;
    if(operatorPressed ==FALSE){
        if(firstEntry == NULL){
            firstEntry = [NSString stringWithFormat:@"%i",(long)tag];
            _labelOutput.text = firstEntry;
        }else{
            firstEntry = [NSString stringWithFormat:@"%@%i",firstEntry,tag];
            _labelOutput.text = firstEntry;
        }
    }else{
        if(secondEntry == NULL){
            secondEntry = [NSString stringWithFormat:@"%i",tag];
            _labelOutput.text = secondEntry;
        }else{
            secondEntry = [NSString stringWithFormat:@"%@%i",secondEntry,tag];
            _labelOutput.text = secondEntry;
        }
    }
}
@end  

学习总结:

1. nil 和null?

2.Values of type ‘NSInteger‘ should not be used as format arguments; add an explicit cast to ‘long‘ instead。

 

 

 

以上是关于ios 学习之 Simple Calculator Application的主要内容,如果未能解决你的问题,请参考以下文章

任意精度计算器 bc (arbitrary precision calculator)

AGC 008 A - Simple Calculator

iOS学习之iOS沙盒(sandbox)机制和文件操作

iOS学习之iOS沙盒(sandbox)机制和文件操作1

。。。IO流学习之二。。。

IOS学习之UITableView滚动到指定位置