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

最新文章

  1. mybatis中#{}与${}的差别(如何防止sql注入)
  2. ReactJS入门(一)—— 初步认识React
  3. SQL Server 2008 R2——创建函数 筛选出字符串中的数字 筛选出字符串中的非数字
  4. 配置RAC到单节点standby的data guard
  5. php短路与 短路或
  6. IT男的”幸福”生活&quot;续2
  7. shell编程基础(2)---&amp;&amp;与||
  8. NOI十连测 第四测 T1
  9. Javascript 面向对象编程—封装
  10. Stall Reservations
  11. 6.QT信号和槽
  12. 12 Django Rest Swagger生成api文档
  13. shell基础:预定义变量
  14. 深入理解Linux内核-Ext2和Ext3文件系统
  15. EF6 CodeFirst代码迁移笔记
  16. C#中Windows Media Player控件使用实例|方法
  17. 零基础学习openstack【完整中级篇】及openstack资源汇总
  18. 转 delete 和 delete []的真正区别
  19. Python学习 :正则表达式
  20. [技巧篇]00.TrimFilter去掉jsp页面空白,提升访问速度

热门文章

  1. js的定位实现和ip查询
  2. Linux命令--用户用户组管理
  3. Swift - 32 - 函数类型
  4. 初涉JavaScript模式系列 阶段总结及规划
  5. HTTP协议学习-03
  6. 扩展《C程序设计语言》练习2-3程序通用性
  7. hdu1405 第六周J题(质因数分解)
  8. HDU1276(士兵队列训练模拟与链表)
  9. Egret 事件机制
  10. Android 判断当前设备是手机还是平板