码迷,mamicode.com
首页 > 其他好文 > 详细

在xcode中用oc实现计算器

时间:2014-07-12 13:42:20      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   文件   os   width   

在建立工程后建立关于计算器的类。

bubuko.com,布布扣

在类的.h 中进行类的方法和成员的声明,在.m是实现:

bubuko.com,布布扣bubuko.com,布布扣

值得注意的是,在其他文件中用到这个类需要 加上这个类的头文件。

然后在main.storybroad中添加你所需要的控件,并加上函数:

bubuko.com,布布扣

我做的是将数字按钮拖入到一个函数中,将所有运算符拖入到到另一个函数中,其他的按钮都有独立的函数。

然后在viewcontroll.h中申明一个全局的类的对象

bubuko.com,布布扣

在viewcontroll.m中的代码为:

//

//  ViewController.m

//  jisuanqi

//

//  Created by apple on 14-7-10.

//  Copyright (c) 2014年 无名. All rights reserved.

//

 

#import "ViewController.h"

#import "jisuanqi.h"

 

@interface ViewController ()

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

            

 

@end

int i=10;

 

@implementation ViewController

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

    float value=[sender.currentTitle floatValue];

    if(jsq.j==‘+‘|| jsq.j==‘-‘|| jsq.j==‘/‘ || jsq.j==‘*‘)

    {

        

        if(jsq.dian==‘.‘)

       {

           jsq.b=jsq.b+value/i;

           i=i*10;

       }

        else

        jsq.b=jsq.b*10+value;

        

        self.xianshi.text=[NSString stringWithFormat:@"%f",jsq.b];

    }

    else

    {

        if(jsq.dian==‘.‘)

        {

            jsq.a=jsq.a+value/i;

            i=i*10;

        }

        else

        jsq.a=jsq.a*10+value;

        self.xianshi.text=[NSString stringWithFormat:@"%f",jsq.a];

    }

}

- (IBAction)diandian:(id)sender {

     jsq.dian=‘.‘;

}

 

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

     jsq.dian=‘0‘;

    i=10;

   

    if( sender.tag==1)

    {

        jsq.j=‘+‘;

    }

    else if(sender.tag==2)

    {

        jsq.j=‘-‘;

    }

    else if(sender.tag==3)

    {

        jsq.j=‘*‘;

    }

    else if (sender.tag==4)

    {

        jsq.j=‘/‘;

    }

    

    

}

- (IBAction)work:(id)sender {

    float gg=[jsq work];

    self.xianshi.text=[NSString stringWithFormat:@"%f",gg];

    jsq.b=0;

    jsq.a=0;

    jsq.j=0;

    jsq.dian=0;

    i=10;

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

 jsq=[[jisuanqi alloc] init];

    // 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

 

运行编译后的结果为:

bubuko.com,布布扣

 

 

 

 

 

 

 

在xcode中用oc实现计算器,布布扣,bubuko.com

在xcode中用oc实现计算器

标签:style   blog   http   文件   os   width   

原文地址:http://www.cnblogs.com/lucan727/p/3836933.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!