iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >iOS实现小型计算器
  • 790
分享到

iOS实现小型计算器

2024-04-02 19:04:59 790人浏览 泡泡鱼
摘要

作为一名初学者,编辑一款能够在iOS操作系统上运行的计算器是一件很值得自豪的事情,网络上虽然后很多相关的文章和代码,功能也很强大但是我感觉相关的计算器比加复杂,晦涩难懂,所以我想通过

作为一名初学者,编辑一款能够在iOS操作系统上运行的计算器是一件很值得自豪的事情,网络上虽然后很多相关的文章和代码,功能也很强大但是我感觉相关的计算器比加复杂,晦涩难懂,所以我想通过这个小小的计算器,能够帮到大家,如果有不完美的地方,还请大家多多批评指教。

首先呢,编辑这个计算器用到了两种控件,Label和Button控件,Label控件用于显示结果,而Button则是相应的键。我把计算器的键分为三种numButton,caculatorButton和clearButton。numButton主要有数字0到9还有小数点,caculatorButton有加号,减号,乘号,除号,等号。clearButton有清除键。所以总共有三种方法。首先先对控件进行连接,Label进行属性连接,Button进行方法连接。

计算器的图形如下:

具体的代码如下;

HHLDelegate.h

#import <UIKit/UIKit.h>
 
@class HHLViewController;
@interface HHLAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) HHLViewController *viewController;
 
@end

HHLDelegate.m

#import "HHLAppDelegate.h"
 
#import "HHLViewController.h"
 
@implementation HHLAppDelegate
 
- (void)dealloc
{
    [_window release];
    [_viewController release];
    [super dealloc];
}
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[HHLViewController alloc] initWithNibName:@"HHLViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
   self.viewController.view.backgroundColor=[[UIColor alloc]initWithRed:0.76 green:0.82 blue:0.94 alpha:0.8];
    [self.window makeKeyAndVisible];
    return 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

HHLViewController.h

#import <UIKit/UIKit.h>
 
@interface HHLViewController : UIViewController
 
@property(retain,nonatomic)IBOutlet UILabel *label;
@property(copy,nonatomic)NSString *title;
@property(retain,nonatomic)NSMutableString *num1,*num2,*num3;
 
- (IBAction)calculatorButton:(id)sender;
- (IBAction)numButton:(id)sender;
- (IBAction)clearButton:(id)sender;
@end

HHLViewController.m

#import "HHLViewController.h"
 
 
@interface HHLViewController ()
 
@end
 
@implementation HHLViewController
@synthesize label;
@synthesize title;
@synthesize num1,num2,num3;
 
int m=0;
int n=0;
 
float y=0;
float count=0;
NSString *collect=@"";
 
- (void)viewDidLoad
{
    [super viewDidLoad];
    
}
 
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
- (IBAction)calculatorButton:(id)sender
{
   
    n=0;
    m++;
    num3=num2;
 
    title=[sender titleForState:UIControlStateNormal];
        
        if (m==1) {
 
            count=[num3 floatValue];
 
             collect=title;
        }
        else{
            
            if ([collect isEqualToString:@"+"]==1) {
                y=[num3 floatValue];
                count=count+y;
            }
            
            if ([collect isEqualToString:@"-"]==1) {
                y=[num3 floatValue];
                count=count-y;
            }
            
            if ([collect isEqualToString:@"*"]==1) {
                y=[num3 floatValue];
                count=count*y;
            }
            
            if ([collect isEqualToString:@"/"]==1) {
                y=[num3 floatValue];
                count=count/y;
            }
            label.text=[NSString stringWithFormat:@"%f",count];
            collect=title;
            
        
        }
        
    }
    
 
 
- (IBAction)numButton:(id)sender{
    n++;
    title=[sender titleForState:UIControlStateNormal];
    num1=[[NSMutableString alloc]initWithString:title];
    if(n==1)
    {
        num2=num1;
    }
    else{
       num2=[[NSMutableString alloc]initWithString:[num2 stringByAppendingString:num1]];
    }
    label.text=num2;
    
}
- (IBAction)clearButton:(id)sender{
label.text=@"";
num1=num3=num2=[[NSMutableString alloc]initWithString:@""];
collect=@"";
count=0;
m=0;
n=0;
  
}
- (void)dealloc
{
    [num1 release];
    [num2 release];
    [num3 release];
    [title release];
    [label release];
    [super dealloc];
}
 
 
@end

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

--结束END--

本文标题: iOS实现小型计算器

本文链接: https://www.lsjlt.com/news/164008.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • iOS实现小型计算器
    作为一名初学者,编辑一款能够在IOS操作系统上运行的计算器是一件很值得自豪的事情,网络上虽然后很多相关的文章和代码,功能也很强大但是我感觉相关的计算器比加复杂,晦涩难懂,所以我想通过...
    99+
    2024-04-02
  • iOS怎么实现小型计算器
    iOS怎么实现小型计算器,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。首先呢,编辑这个计算器用到了两种控件,Label和Button控件,Label控件用于显示结果,而Bu...
    99+
    2023-06-29
  • iOS实现计算器小功能
    本文实例为大家分享了iOS实现计算器小功能,供大家参考,具体内容如下 本文利用ios实现计算器app,后期将用mvc结构重构 import UIKit class CalculVi...
    99+
    2024-04-02
  • iOS实现简单计算器小功能
    本文实例为大家分享了iOS实现简单计算器小功能的具体代码,供大家参考,具体内容如下 SimpleCaculatorViewController.h // //  SimpleCac...
    99+
    2024-04-02
  • C++实现小型复数计算器
    小型复数计算器项目设计,供大家参考,具体内容如下 一、问题描述及功能要求 1、实现复数的设置和显示。 2、声明一个复数类Complex,重载运算符 “+”、 “-”、 “*”、 “/...
    99+
    2024-04-02
  • iOS是怎样实现简单计算器小功能
    本篇文章为大家展示了iOS是怎样实现简单计算器小功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。SimpleCaculatorViewController.h////  Sim...
    99+
    2023-06-29
  • 微信小程序实现小型计算器
    本文实例为大家分享了微信小程序实现小型计算器的具体代码,供大家参考,具体内容如下 app.js // app.js App({   onLaunch() {   },   REGEX...
    99+
    2024-04-02
  • iOS实现简易的计算器
    本文实例为大家分享了iOS实现简易的计算器的具体代码,供大家参考,具体内容如下 初步接触视图,制作了一个简易的计算器,基本上简单的计算是没有问题的,不是很完美,可能还有一些bug,再...
    99+
    2024-04-02
  • iOS实现计算器小功能的代码怎么写
    本篇内容介绍了“iOS实现计算器小功能的代码怎么写”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!本文利用ios实现计算器app,后期将用mv...
    99+
    2023-06-29
  • iOS实现简单计算器功能
    本文实例为大家分享了iOS实现简单计算器功能的具体代码,供大家参考,具体内容如下 //  ZYAppDelegate.m //  Calculator // //  Created ...
    99+
    2024-04-02
  • iOS开发实现简单计算器功能
    用Object-C写的一个简单的计算机程序,主要学习按钮的action动作。 下面是主界面: 下面代码时界面按钮和ViewController.h连接的地方: - (IBActio...
    99+
    2024-04-02
  • iOS怎么实现简单计算器功能
    本篇文章给大家分享的是有关iOS怎么实现简单计算器功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。//  ZYAppDelegate.m// &...
    99+
    2023-06-29
  • python实现计算器小功能
    本文实例为大家分享了python实现计算器功能的具体代码,供大家参考,具体内容如下 1. 案例介绍 本例利用 Python 开发一个可以进行简单的四则运算的图形化计算器,会用到 Tk...
    99+
    2024-04-02
  • jquery实现计算器小功能
    本文实例为大家分享了jquery实现计算器功能的具体代码,供大家参考,具体内容如下 用jquery实现计算器对于我来说有三个难点 1.单纯的html页面,怎么实现计算2.显示屏用什么...
    99+
    2024-04-02
  • iOS实现简易计算器的代码怎么写
    本篇内容主要讲解“iOS实现简易计算器的代码怎么写”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“iOS实现简易计算器的代码怎么写”吧!初步接触视图,制作了一个简易的计算器,基本上简单的计算是没有...
    99+
    2023-06-29
  • java gui实现计算器小程序
    本文实例为大家分享了java gui实现计算器小程序的具体代码,供大家参考,具体内容如下 废话不多说 , 直接贴代码 , 有详细的注释 , 我也是刚学GUI没多久 这个是效果图 : ...
    99+
    2024-04-02
  • 小程序实现计算器功能
    本文实例为大家分享了小程序实现计算器功能的具体代码,供大家参考,具体内容如下 实现模拟手机上的计算器,输入即可运算 本页面是做一个计算收款的页面,如果不需要下面的内容可以删除掉 w...
    99+
    2024-04-02
  • 微信小程序实现计算器小功能
    微信小程序现在越来越火爆了,我也看到很多在校大学生都在自学了,那些专门从事APP开发,网页开发的工作者更是看到了小程序的前景,在小程序领域也掺上一脚,本人也是自学小程序的,初期跟很多...
    99+
    2024-04-02
  • C#实现简易计算器小功能
    本文实例为大家分享了C#实现简易计算器小功能的具体代码,供大家参考,具体内容如下 简易的登陆界面。 具有幻灯片效果。(picturebox time控件)计算器支持多位数,小数,括...
    99+
    2024-04-02
  • JavaScript实现简单计算器小程序
    本文实例为大家分享了JavaScript实现简单计算器的具体代码,供大家参考,具体内容如下 代码: <!DOCTYPE html> <html> <h...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作