在xcode中用oc實現計算器


在建立工程后建立關於計算器的類。

在類的.h 中進行類的方法和成員的聲明,在.m是實現:

值得注意的是,在其他文件中用到這個類需要 加上這個類的頭文件。

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

我做的是將數字按鈕拖入到一個函數中,將所有運算符拖入到到另一個函數中,其他的按鈕都有獨立的函數。

然后在viewcontroll.h中申明一個全局的類的對象

在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

 

運行編譯后的結果為:

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM