初学者如何做一个简单的计算器,代码分享

先新建一个类 startCalculator

声明如下

#import <Foundation/Foundation.h>

 

@interface StartCalculator : NSObject

//声明两个要计算的变量

@property float opValue1;

@property float opValue2;

//声明一个运算符

@property char op;

 

//普通方法

//- (float) gzyWorkAdd;

//

//- (float) gzyWorkSubtraction;

//

//- (float) gzyWorkMultiplication;

//

//- (float) gzyWorkDivision;

 

//用switch语句生成的方法

- (float) yes : (char) n;

 

@end

---------------------------------------------------------------------------------

实现部分如下

 

#import "StartCalculator.h"

 

@implementation StartCalculator

//用普通方法

//- (float) gzyWorkAdd

//{

//    return self.opValue1 + self.opValue2;

//}

//

//- (float) gzyWorkSubtraction

//{

//    return self.opValue1 - self.opValue2;

//}

//

//- (float) gzyWorkMultiplication

//{

//    return self.opValue1 * self.opValue2;

//}

//

//- (float) gzyWorkDivision

//{

//    return self.opValue1 / self.opValue2;

//}

 

//用switch进行判断

- (float) yes : (char) n

{

    switch (n) {

        case '+':

            return self.opValue1 + self.opValue2;

            

        case '-':

            return self.opValue1 - self.opValue2;

            

        case '*':

            return self.opValue1 * self.opValue2;

            

        case '/':

            return self.opValue1 / self.opValue2;

            

        default:

            return  0;

    }

}

 

@end

 

————————————————————————————————————————————————

viewcontroller部分的声明

 

#import <UIKit/UIKit.h>

#import "StartCalculator.h"

 

@interface ViewController : UIViewController

//用于给类定义一个scl的实例 

{

    StartCalculator *scl;

}

 

@end

 ——————————————————————————————————————

实现部分如下

 

#import "ViewController.h"

 

@interface ViewController ()

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

 

            

 

@end

 

@implementation ViewController

 

- (IBAction)didClick00:(id)sender {

    //判断是否用了加减乘除

    if (scl.op == '+') {

        scl.opValue2 = scl.opValue2 * 10;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else if (scl.op == '-'){

        scl.opValue2 = scl.opValue2 * 10;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

                }

    else if (scl.op == '*'){

        scl.opValue2 = scl.opValue2 * 10;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

 

        

    }

    else if (scl.op == '/'){

        scl.opValue2 = scl.opValue2 * 10;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else {

        scl.opValue1 = scl.opValue1 * 10 ;

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue1];

 

        

    }

}

 

- (IBAction)didClick01:(id)sender {

    if (scl.op == '+') {

        scl.opValue2 = scl.opValue2 * 10 + 1;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else if (scl.op == '-'){

        scl.opValue2 = scl.opValue2 * 10 + 1;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else if (scl.op == '*'){

        scl.opValue2 = scl.opValue2 * 10 + 1;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

        

    }

    else if (scl.op == '/'){

        scl.opValue2 = scl.opValue2 * 10 + 1;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else {

        scl.opValue1 = scl.opValue1 * 10 + 1;

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue1];

        

        

    }

 

 

 

}

 

- (IBAction)didClick02:(id)sender {

    if (scl.op == '+') {

        scl.opValue2 = scl.opValue2 * 10 + 2;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else if (scl.op == '-'){

        scl.opValue2 = scl.opValue2 * 10 + 2;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else if (scl.op == '*'){

        scl.opValue2 = scl.opValue2 * 10 + 2;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

        

    }

    else if (scl.op == '/'){

        scl.opValue2 = scl.opValue2 * 10 + 2;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else {

        scl.opValue1 = scl.opValue1 * 10 + 2;

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue1];

        

        

    }

 

 

}

 

- (IBAction)didClick03:(id)sender {

    if (scl.op == '+') {

        scl.opValue2 = scl.opValue2 * 10 + 3;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else if (scl.op == '-'){

        scl.opValue2 = scl.opValue2 * 10 + 3;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else if (scl.op == '*'){

        scl.opValue2 = scl.opValue2 * 10 + 3;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

        

    }

    else if (scl.op == '/'){

        scl.opValue2 = scl.opValue2 * 10 + 3;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else {

        scl.opValue1 = scl.opValue1 * 10 + 3;

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue1];

        

        

    }

 

 

}

 

- (IBAction)didClick04:(id)sender {

    if (scl.op == '+') {

        scl.opValue2 = scl.opValue2 * 10 + 4;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else if (scl.op == '-'){

        scl.opValue2 = scl.opValue2 * 10 + 4;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else if (scl.op == '*'){

        scl.opValue2 = scl.opValue2 * 10 + 4;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

        

    }

    else if (scl.op == '/'){

        scl.opValue2 = scl.opValue2 * 10 + 4;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else {

        scl.opValue1 = scl.opValue1 * 10 + 4;

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue1];

        

        

    }

 

}

 

- (IBAction)didClick05:(id)sender {

    if (scl.op == '+') {

        scl.opValue2 = scl.opValue2 * 10 + 5;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else if (scl.op == '-'){

        scl.opValue2 = scl.opValue2 * 10 + 5;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else if (scl.op == '*'){

        scl.opValue2 = scl.opValue2 * 10 + 5;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

        

    }

    else if (scl.op == '/'){

        scl.opValue2 = scl.opValue2 * 10 + 5;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else {

        scl.opValue1 = scl.opValue1 * 10 + 5;

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue1];

        

        

    }

 

 

}

 

- (IBAction)didClick06:(id)sender {

    if (scl.op == '+') {

        scl.opValue2 = scl.opValue2 * 10 + 6;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else if (scl.op == '-'){

        scl.opValue2 = scl.opValue2 * 10 + 6;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else if (scl.op == '*'){

        scl.opValue2 = scl.opValue2 * 10 + 6;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

        

    }

    else if (scl.op == '/'){

        scl.opValue2 = scl.opValue2 * 10 + 6;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else {

        scl.opValue1 = scl.opValue1 * 10 + 6;

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue1];

        

        

    }

 

}

 

- (IBAction)didClick07:(id)sender {

    if (scl.op == '+') {

        scl.opValue2 = scl.opValue2 * 10 + 7;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else if (scl.op == '-'){

        scl.opValue2 = scl.opValue2 * 10 + 7;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else if (scl.op == '*'){

        scl.opValue2 = scl.opValue2 * 10 + 7;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

        

    }

    else if (scl.op == '/'){

        scl.opValue2 = scl.opValue2 * 10 + 7;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else {

        scl.opValue1 = scl.opValue1 * 10 + 7;

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue1];

        

        

    }

 

 

}

 

- (IBAction)didClick08:(id)sender {

    if (scl.op == '+') {

        scl.opValue2 = scl.opValue2 * 10 + 8;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else if (scl.op == '-'){

        scl.opValue2 = scl.opValue2 * 10 + 8;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else if (scl.op == '*'){

        scl.opValue2 = scl.opValue2 * 10 + 8;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

        

    }

    else if (scl.op == '/'){

        scl.opValue2 = scl.opValue2 * 10 + 8;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else {

        scl.opValue1 = scl.opValue1 * 10 + 8;

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue1];

        

        

    }

 

}

 

- (IBAction)didClick09:(id)sender {

    if (scl.op == '+') {

        scl.opValue2 = scl.opValue2 * 10 + 9;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else if (scl.op == '-'){

        scl.opValue2 = scl.opValue2 * 10 + 9;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else if (scl.op == '*'){

        scl.opValue2 = scl.opValue2 * 10 + 9;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

        

    }

    else if (scl.op == '/'){

        scl.opValue2 = scl.opValue2 * 10 + 9;

        //显示在label里

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

        

    }

    else {

        scl.opValue1 = scl.opValue1 * 10 + 9;

        self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue1];

        

        

    }

 

 

}

- (IBAction)add:(id)sender {

    scl.op = '+';

}

 

- (IBAction)sub:(id)sender {

    scl.op = '-';

}

 

- (IBAction)multi:(id)sender {

    scl.op = '*';

}

 

- (IBAction)division:(id)sender {

    scl.op = '/';

}

 

- (IBAction)didClickEquarl:(id)sender {

    

    float result = [scl yes:scl.op];

    self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",result];

    

    scl.op = 0;

    scl.opValue1 = 0;

    scl.opValue2 = 0;

    

}

 

- (IBAction)AC:(id)sender {

    scl.opValue1 = 0;

    self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue1];

    scl.opValue2 = 0;

    self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];

}

 

 

- (void)viewDidLoad {

    [super viewDidLoad];

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

    scl = [[StartCalculator alloc] init];

    

    

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end

 

结合storyboard链接这些代码即可

 

 

 

 

转载于:https://www.cnblogs.com/gzy0724/p/3836681.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值