iOS实现小型计算器

 更新时间:2022年1月27日 16:59  点击:428 作者:JackLee18

作为一名初学者,编辑一款能够在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

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

原文出处:https://blog.csdn.net/hanhailong18/article/details/17249055

[!--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
  • iOS新版微信底部返回横条问题的解决

    这篇文章主要介绍了iOS新版微信底部返回横条问题的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-30
  • C#实现简易计算器功能(附源码)

    这篇文章主要为大家详细介绍了C#实现简易计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-07-21
  • 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
  • iOS UICollectionView实现卡片效果

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

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

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

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

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

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