iOS实现简单计算器功能

本文实例为大家分享了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

效果图:

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

(0)

相关推荐

  • iOS开发实现计算器功能

    本文实例为大家分享了iOS实现计算器功能的具体代码,供大家参考,具体内容如下 效果图 Masonry 使用数组来自动约束 NSArray *buttonArrayOne = @[_buttonAC, _buttonLeftBracket, _buttonRightBracket, _buttonDivide]; //withFixedSpacing: 每个view中间的间距 //leadSpacing: 左最开始的间距 //tailSpacing:; 右边最后的的间距 [buttonArrayO

  • 基于AngularJS实现iOS8自带的计算器

    前言 首先创建angularjs的基本项目就不说了,最好是利用yeoman这个脚手架工具直接生成,如果没有该环境的,当然也可以通过自行下载angularjs的文件引入项目. 实例详解 main.js是项目的主要js文件,所有的js都写在这个文件中,初始化之后,该文件的js代码如下 angular .module('calculatorApp', [ 'ngAnimate', 'ngCookies', 'ngResource', 'ngRoute', 'ngSanitize', 'ngTouch'

  • iOS实现简单计算器功能

    本文实例为大家分享了iOS实现简单计算器功能的具体代码,供大家参考,具体内容如下 //  ZYAppDelegate.m //  Calculator // //  Created by mac on 15-7-30. //  Copyright (c) 2015年 zhiyou. All rights reserved. //   #import "ZYAppDelegate.h"   @implementation ZYAppDelegate   - (BOOL)applicati

  • iOS实现简单计算器小功能

    本文实例为大家分享了iOS实现简单计算器小功能的具体代码,供大家参考,具体内容如下 SimpleCaculatorViewController.h // //  SimpleCaculatorViewController.h //  SimpleCaculator // //  Created by LI Junui on 14-2-12. //  Copyright (c) 2014年 LEE JUNHUI. All rights reserved. //   #import <UIKit/U

  • 基于javascript实现简单计算器功能

    本文实例为大家介绍javascript实现简单计算器功能的详细代码,分享给大家供大家参考,具体内容如下 效果图: 实现代码: <html> <head> <script> function calc(event){ // test //window.alert(event.value); var val = new String(event.value); // clear space val = val.trim(); var res = document.getEl

  • Android开发实现的简单计算器功能【附完整demo源码下载】

    本文实例讲述了Android开发实现的简单计算器功能.分享给大家供大家参考,具体如下: 这个Android计算器虽然还有点小bug,不过简单的计算功能还是没问题的哦: 先上图看效果 比较简单,所以我就没怎么写注释,应该一看就能明白的 有不明白的可以发信问我 先贴MainActivity.java代码 package com.example.calculator; import android.app.Activity; import android.os.Bundle; import andro

  • JavaScript实现的超简单计算器功能示例

    本文实例讲述了JavaScript实现的超简单计算器功能.分享给大家供大家参考,具体如下: 先来看看运行效果: 具体代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>www.jb51.net JS计算器</title> <script type="text/javascript&qu

  • Python实现的简单计算器功能详解

    本文实例讲述了Python实现的简单计算器功能.分享给大家供大家参考,具体如下: 使用python编写一款简易的计算器 计算器效果图 首先搭建计算器的面板: 计算器面板结构 建造一个继承于wx.Frame的frame,在init属性函数中搭建面板 class CalcFrame(wx.Frame):#建造一个继承于wx.Frame的frame def __init__(self,title): wx.Frame.__init__ (self,None,-1,title, pos=(100,300

  • C语言实现简单计算器功能(1)

    本文为大家分享了C语言实现简单计算器功能的具体方法,供大家参考,具体内容如下 这几天一直上控制语句,在学完if.switch和循环语句后,为了巩固所学知识点,想给学生出一道简单的计算器程序.题目如下: 通过所学的知识(选择语句.循环语句)完成一个简单的计算器编程,要求不许抄袭,只能用自己所学的知识点,不可引用网上自己的不懂的程序. 要求的格式如下图: 要求以这样的一种格式进行输出,并能通过选择完成不同的功能. 如:选择1,完成整数相加功能,算完后又能回到该页面. 经过分析,我简单的写了一段代码,

  • C语言实现简单计算器功能(2)

    本文接着上一篇C语言实现简单计算器功能(1)继续学习. 之前用switch语句完成了一个简单计算器的功能,下面我给大家用if语句和while循环语句再实现一个简单计算器的编程. 要求: 实现简单的计算器,具体代码如下: #include <stdio.h> // 头文件 void main() { char a = 0; // 变量声明并初始化 int x, y; // 变量声明 int result = 0; char b = 37; while(1) // whiel循环实现反复在计算器系

  • Android Studio实现简单计算器功能

    本文实例为大家分享了Android Studio实现简单计算器功能的具体代码,供大家参考,具体内容如下 程序步骤: (1)在布局文件定义一些计算器界面的文本框,按钮等组件. (2)在Activity中获取组件实例. (3)通过swtich函数,判断输入的内容,并进行相应操作,通过getText()获取文本内容,setText()显示. 程序代码设计: 一.界面布局 参考书本例2-9,应用网格布局设计计算器界面,在设计区域设置一个6行4列的网格布局,第一行为显示数据的文本标签,第二行为清除数据的按

  • 用Java实现简单计算器功能

    本项目为大家分享了Java实现简单计算器功能的具体代码,供大家参考,具体内容如下 一 项目说明 实训目的:掌握 Java GUI 开发中的布局管理和事件处理机制. 实训要求: (1)要使用 java 的 GUI 设计出计算器界面. (2)通过界面按钮,可以实现整数或浮点数的四则运算,并能将结果显示在界面中. (3)计算可以有小数点,和正负整数的计算. (4)要有清零功能. 二.类设计 中缀表达式的计算solution(String str) 用来中算后缀表达式的值,并将结果返回.准备一个数字栈,

随机推荐