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
IOS实现小型计算器的更多相关文章
- iOS小型计算器
// // ViewController.m // 计算器 //屏幕的宽和高 #define SCREEN_W self.view.frame.size.width #define SCREEN_ ...
- [课堂实践与项目]IOS优先级的计算器
这个计算器主要是使用数组进行实现的.虽然没有使用前缀后缀表达式,但是是一种方法o. .h文件 // // LCViewController.h // 具有优先级的calculator // // Cr ...
- iOS开发之计算器
本项目基于swift3.0的语法. // // ViewController.swift // 加法计算器 // // Created by 葛杨杨 on 2017/7/25. // Copyrigh ...
- 寒城攻略:Listo 教你用 Swift 写IOS UI 项目计算器
之前总结过 Swift 的语言攻略,这里就不做赘述了,如今做一个实例计算器项目来介绍一下 Swift 的应用.(凝视已经全然.直接上代码) 先看一下效果图: 以下是详细的代码和解释: 分享快乐.开源中 ...
- html5 javascript 小型计算器
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- ios开发之-计算器的改进
#import <Foundation/Foundation.h> extern double add(double x,double y); extern double subtract ...
- 我的iOS开发之路
我终于开始写我的第一个cocos2d-iphone程序了.纪念一下 额,这是一个悲伤的故事.其实我从开始准备开发iOS已经好久了,从我装上Xcode开始到现在,应该已经有差不多一年的时间了把. 还记得 ...
- IOS自适应库---- Masonry的使用
Masonry是一个轻量级的布局框架,拥有自己的描述语法,采用更优雅的链式语法封装自动布局,简洁明了并具有高可读性,而且同时支持 iOS 和 Max OS X.Masonry是一个用代码写iOS或OS ...
- iOS参考工具和资源
图片: Glyphish(图标资源) 资源: SwiftGuide:这份指南汇集了Swift语言主流学习资源,并以开发者的视角整理编排. 27款iOS开源库,让你的开发溜到飞起 创业者的新春礼包—优秀 ...
随机推荐
- Content-Disposition的使用和注意事项
转载:http://www.cnblogs.com/jzaileen/articles/1281025.html 最近不少Web技术圈内的朋友在讨论协议方面的事情,有的说web开发者应该熟悉web相关 ...
- 利用后缀数组(suffix array)求最长公共子串(longest common substring)
摘要:本文讨论了最长公共子串的的相关算法的时间复杂度,然后在后缀数组的基础上提出了一个时间复杂度为o(n^2*logn),空间复杂度为o(n)的算法.该算法虽然不及动态规划和后缀树算法的复杂度低,但其 ...
- C++简单工厂模式
核心思想:用一个工厂类根据不同的输入条件new出不同的派生类指针,然后由多态调用不同派生类中virtual函数,得到不同的结果 所有的判断处理在工厂类中完成,通过CreateFactory函数,根据不 ...
- rhel-server-6.2-i386安装gcc、g++步骤
安装的版本:rhel-server-6.2-i386 RHEL 6.2默认是没有gcc和gcc-c++环境的,而且我也没有$购买正版服务.只能本地安装了,总结方法如下: 上传安装镜像rhel-serv ...
- JQuery相关的网络资源
jquery插件列表 国外网站:http://plugins.jquery.com/ 国内网站:http://www.oschina.net/project/tag/273/jquery
- HTML5 QQ登录背景动态图片
预览效果如图所示: 代码如下: <!DOCTYPE html> <head> <meta http-equiv="Content-Type" cont ...
- 《asp.net mvc3 高级编程》第四章 模型
一,建立简单的Model 在Models文件夹上右击鼠标,选择“添加”,“类”,如下图所示: 建立三类相关联的类代码如下: public class Album { public virtual in ...
- javascript mvc 简单例子
<!DOCTYPE html> <html> <head> </head> <body> <input type="text ...
- JS判断鼠标从哪个方向进入DIV容器
写的不够高大上 , 不要介意哦... Js: //进去 $(".flash").bind("mouseenter",function(e){ /** the w ...
- TCP客户/服务器程序概述
一个回射服务器: 1)客户从标准输入读入一行文本,并写给服务器 2)服务器从网络输入读入这行文本,并回射给客户 3)客户从网络输入读入这行回射文本,并显示在标准输出上 回射输入行这样一个客户/服务器程 ...