iOS实现简单计算器功能

 更新时间:2022年1月27日 16:59  点击:422 作者:祝小言

本文实例为大家分享了iOS实现简单计算器功能的具体代码,供大家参考,具体内容如下

//  ZYAppDelegate.m
//  Calculator
//
//  Created by mac on 15-7-30.
//  Copyright (c) 2015年 zhiyou. All rights reserved.
//
 
#import "ZYAppDelegate.h"
 
@implementation ZYAppDelegate
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    show=[[UITextField alloc] initWithFrame:CGRectMake(20, 30, 260, 30)];
    show.borderStyle=UITextBorderStyleRoundedRect;
    show.text=@"0";
    
    show.backgroundColor=[UIColor redColor];
    [self.window addSubview:show];
    
    
    UIButton *btn1=[UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame=CGRectMake(20, 80, 40, 40);
    [btn1 setTitle:@"1" forState:UIControlStateNormal];
    btn1.backgroundColor=[UIColor blueColor];
    btn1.tag=1;
    [btn1 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn1];
    
    UIButton *btn2=[UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame=CGRectMake(80, 80, 40, 40);
    [btn2 setTitle:@"2" forState:UIControlStateNormal];
    btn2.backgroundColor=[UIColor blueColor];
    btn2.tag=2;
    [btn2 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn2];
    
    UIButton *btn3=[UIButton buttonWithType:UIButtonTypeCustom];
    btn3.frame=CGRectMake(140, 80, 40, 40);
    [btn3 setTitle:@"3" forState:UIControlStateNormal];
    btn3.backgroundColor=[UIColor blueColor];
    btn3.tag=3;
    [btn3 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn3];
    
  
 
    
    UIButton *btn4=[UIButton buttonWithType:UIButtonTypeCustom];
    btn4.frame=CGRectMake(20, 140, 40, 40);
    [btn4 setTitle:@"4" forState:UIControlStateNormal];
    btn4.backgroundColor=[UIColor blueColor];
    btn4.tag=4;
    [btn4 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn4];
    
    UIButton *btn5=[UIButton buttonWithType:UIButtonTypeCustom];
    btn5.frame=CGRectMake(80, 140, 40, 40);
    [btn5 setTitle:@"5" forState:UIControlStateNormal];
    btn5.backgroundColor=[UIColor blueColor];
    btn5.tag=5;
    [btn5 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn5];
    
    UIButton *btn6=[UIButton buttonWithType:UIButtonTypeCustom];
    btn6.frame=CGRectMake(140, 140, 40, 40);
    [btn6 setTitle:@"6" forState:UIControlStateNormal];
    btn6.backgroundColor=[UIColor blueColor];
    btn6.tag=6;
    [btn6 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn6];
    
 
    
    
    UIButton *btn7=[UIButton buttonWithType:UIButtonTypeCustom];
    btn7.frame=CGRectMake(20, 200, 40, 40);
    [btn7 setTitle:@"7" forState:UIControlStateNormal];
    btn7.backgroundColor=[UIColor blueColor];
    btn7.tag=7;
    [btn7 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn7];
    
    UIButton *btn8=[UIButton buttonWithType:UIButtonTypeCustom];
    btn8.frame=CGRectMake(80, 200, 40, 40);
    [btn8 setTitle:@"8" forState:UIControlStateNormal];
    btn8.backgroundColor=[UIColor blueColor];
    btn8.tag=8;
    [btn8 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn8];
    
    UIButton *btn9=[UIButton buttonWithType:UIButtonTypeCustom];
    btn9.frame=CGRectMake(140, 200, 40, 40);
    [btn9 setTitle:@"9" forState:UIControlStateNormal];
    btn9.backgroundColor=[UIColor blueColor];
    
    btn9.tag=9;
    [btn9 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn9];
    
    
 
    
 
    UIButton *btn0=[UIButton buttonWithType:UIButtonTypeCustom];
    btn0.frame=CGRectMake(20, 260, 100, 40);
    [btn0 setTitle:@"0" forState:UIControlStateNormal];
    btn0.backgroundColor=[UIColor blueColor];
    btn0.tag=10;
    [btn0 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn0];
    
    
    
    
    
    UIButton *jia=[UIButton buttonWithType:UIButtonTypeCustom];
    jia.frame=CGRectMake(200, 140, 40, 40);
    [jia setTitle:@"+" forState:UIControlStateNormal];
    jia.backgroundColor=[UIColor blueColor];
    jia.tag=100101;
    [jia addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:jia];
    
    
    UIButton *jian=[UIButton buttonWithType:UIButtonTypeCustom];
    jian.frame=CGRectMake(200, 200, 40, 40);
    [jian setTitle:@"-" forState:UIControlStateNormal];
    jian.backgroundColor=[UIColor blueColor];
    jian.tag=100102;
    [jian addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:jian];
    
    
    
    UIButton *cheng=[UIButton buttonWithType:UIButtonTypeCustom];
    cheng.frame=CGRectMake(200, 80, 40, 40);
    [cheng setTitle:@"*" forState:UIControlStateNormal];
    cheng.backgroundColor=[UIColor blueColor];
    cheng.tag=100103;
    [cheng addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:cheng];
    
    
    UIButton *chu=[UIButton buttonWithType:UIButtonTypeCustom];
    chu.frame=CGRectMake(200, 260, 40, 40);
    [chu setTitle:@"/" forState:UIControlStateNormal];
    chu.backgroundColor=[UIColor blueColor];
    chu.tag=100104;
    [chu addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:chu];
  
 
    UIButton *deng=[UIButton buttonWithType:UIButtonTypeCustom];
    deng.frame=CGRectMake(140, 260, 40, 40);
    [deng setTitle:@"=" forState:UIControlStateNormal];
    deng.backgroundColor=[UIColor blueColor];
    deng.tag=100105;
    [deng addTarget:self action:@selector(equal) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:deng];
 
    return YES;
}
 
-(void)onClick:(UIButton *)btn{
 
    switch (btn.tag) {
        case 1:
            num1=@"1";
            show.text=num1;
            a=[num1 intValue];
            
            break;
        case 2:
            num1=@"2";
            show.text=num1;
            a=[num1 intValue];
            break;
        
        case 3:
            num1=@"3";
            show.text=num1;
            a=[num1 intValue];
            break;
        case 4:
            num1=@"4";
            show.text=num1;
            a=[num1 intValue];
            break;
        case 5:
            num1=@"5";
            show.text=num1;
            a=[num1 intValue];
 
            break;
        case 6:
            num1=@"6";
            show.text=num1;
            a=[num1 intValue];
            break;
        case 7:
            num1=@"7";
            show.text=num1;
            a=[num1 intValue];
            break;
        case 8:
            num1=@"8";
            show.text=num1;
            a=[num1 intValue];
            break;
        case 9:
            num1=@"9";
            show.text=num1;
            a=[num1 intValue];
            break;
        case 10:
            num1=@"0";
            show.text=num1;
            a=[num1 intValue];
 
            break;
        default:
            break;
    }
 
}
-(void)click:(UIButton *)btn{
    switch (btn.tag) {
        case 100101:
            b=1;
            c=a;
            break;
        case 100102:
            b=2;
            c=a;
            break;
        case 100103:
            b=3;
            c=a;
            break;
        case 100104:
            b=4;
            c=a;
            break;
       
    }
    
}
-(void)equal{
 
    if (b==1) {
        show.text=[NSString stringWithFormat:@"%d",(c+a)];
    }
    if (b==2) {
        show.text=[NSString stringWithFormat:@"%d",(c-a)];
    }
    if (b==3) {
        show.text=[NSString stringWithFormat:@"%d",(c*a)];
    }
    if (b==4) {
        show.text=[NSString stringWithFormat:@"%d",(c/a)];
    }
 
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.window endEditing:YES];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
 
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
 
- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
 
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
 
- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
 
@end

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。

原文出处:https://blog.csdn.net/swanzhu/article/details/47155939

[!--infotagslink--]

相关文章

  • MySQL性能监控软件Nagios的安装及配置教程

    这篇文章主要介绍了MySQL性能监控软件Nagios的安装及配置教程,这里以CentOS操作系统为环境进行演示,需要的朋友可以参考下...2015-12-14
  • iOS APP h5快捷程序 .mobileconfig的生成

    1.从APP Store 下载Apple Configurator 2从一个管理点管理所有iOS设备应用程序,文档和配置文件。想要确保您的所有家庭成员在其每台iOS设备上都有类似的应用和文档,管理日益增...2021-12-23
  • iOS设置UIButton文字显示位置和字体大小、颜色的方法

    这篇文章给大家分享了iOS如何设置UIButton的文字显示位置和字体的大小、颜色,文中给出了示例代码,相信对大家的学习和理解很有帮助,有需要的朋友们下面来一起看看吧。...2020-06-30
  • iOS如何将图片裁剪成圆形

    这篇文章主要为大家详细介绍了iOS如何将图片裁剪成圆形,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-30
  • iOS给border设置渐变色的方法实例

    这篇文章主要给大家介绍了关于iOS给border设置渐变色的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-09
  • C#实现简易计算器功能(附源码)

    这篇文章主要为大家详细介绍了C#实现简易计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-07-21
  • iOS新版微信底部返回横条问题的解决

    这篇文章主要介绍了iOS新版微信底部返回横条问题的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-30
  • vue+axios全局添加请求头和参数操作

    这篇文章主要介绍了vue+axios全局添加请求头和参数操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-24
  • iOS蓝牙设备名称缓存问题的解决方法

    这篇文章主要给大家介绍了关于iOS蓝牙设备名称缓存问题的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-08
  • iOS新版微信底部工具栏遮挡问题完美解决

    这篇文章主要介绍了iOS新版微信底部工具栏遮挡问题完美解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-30
  • iOS新增绘制圆的方法实例代码

    这篇文章主要给大家介绍了关于iOS新增绘制圆的方法,文中通过示例代码介绍的非常详细,对各位iOS开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧...2020-06-30
  • iOS UIBezierPath实现饼状图

    这篇文章主要为大家详细介绍了iOS UIBezierPath实现饼状图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-03-20
  • 封装 axios+promise通用请求函数操作

    这篇文章主要介绍了封装 axios+promise通用请求函数操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-12
  • IOS获取各种文件目录路径的方法

    ios获取文件路径的方法,iphone沙箱模型的四个文件夹,通过documents,tmp,app,Library得到模拟器路径的简单方式,下面小编整理相关资料,把IOS获取各种文件目录路径的方式总结如下,需要的朋友可以参考下...2020-06-30
  • Vue中 axios delete请求参数操作

    这篇文章主要介绍了Vue中 axios delete请求参数操作,具有很好的参考价值,希望对大家有所 帮助。一起跟随小编过来看看吧...2020-08-26
  • iOS UICollectionView实现卡片效果

    这篇文章主要为大家详细介绍了iOS UICollectionView实现卡片效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-30
  • iOS实现电子签名

    这篇文章主要为大家详细介绍了iOS实现电子签名,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-12-08
  • 小程序实现计算器功能

    这篇文章主要为大家详细介绍了小程序实现计算器功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-07-19
  • C# WinForm程序设计简单计算器

    这篇文章主要为大家详细介绍了C# WinForm程序设计简单计算器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
  • Vue使用axios引起的后台session不同操作

    这篇文章主要介绍了Vue使用axios引起的后台session不同操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-14