zoukankan      html  css  js  c++  java
  • 在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

    运行编译后的结果为:

  • 相关阅读:
    编译不通过:提示XXXX不是类或命名空间名 的解决办法
    nginx应用总结(2)--突破高并发的性能优化
    nginx应用总结(1)--基础认识和应用配置
    springboot内置tomcat验证授权回调页面域名
    MySQL实现类似Oracle中的nextval和currval
    Notepad++中删除连续的任意n行
    Spring Boot系列二 Spring @Async异步线程池用法总结
    Spring线程池配置
    Spring异步方法注解 @Async
    异步任务spring @Async注解源码解析
  • 原文地址:https://www.cnblogs.com/lucan727/p/3836933.html
Copyright © 2011-2022 走看看