iOS简易计算器的实现
2014-08-21 19:12
148 查看
// // CCLAppDelegate.m // TestCalculator // // Created by lanouhn on 14-8-20. // Copyright (c) 2014年 vaercly@163.com 陈聪雷. All rights reserved. // #import "CCLAppDelegate.h" #import "CCLCalculatorView.h" @interface CCLAppDelegate () { UIView *_backgroundView; UILabel *_showResultLable; } @end @implementation CCLAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. [self setupbgView]; [self setupShowView]; [self setupCalView]; [self showCal]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; } - (void)setupbgView { _backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)]; _backgroundView.backgroundColor = [UIColor whiteColor]; [self.window addSubview:_backgroundView]; [_backgroundView release]; } - (void)setupShowView { _showResultLable = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, 280, 80)]; _showResultLable.backgroundColor = [UIColor whiteColor]; _showResultLable.font = [UIFont systemFontOfSize:45]; _showResultLable.layer.cornerRadius = 6; _showResultLable.layer.borderColor = [UIColor grayColor].CGColor; _showResultLable.layer.borderWidth = 2; _showResultLable.textAlignment = NSTextAlignmentRight; [_backgroundView addSubview:_showResultLable]; [_showResultLable release]; } - (void)setupCalView { NSArray *arr = @[@[@"MC", @"M+", @"M-", @"C"], @[@7, @8, @9, @"+"], @[@4, @5, @6, @"-"], @[@1, @2, @3, @"*"], @[@0, @".", @"/", @"="]]; for (int i = 0; i < 5; i++) { CCLCalculatorView *calKeyView = [[CCLCalculatorView alloc] initWithFrame:CGRectMake(20, 150 + 80 * i, 280, 40) buttonTitle:arr[i]]; [_backgroundView addSubview:calKeyView]; [calKeyView release]; } } - (void)showCal { // CCLCalculatorView *calKeyView = [[CCLCalculatorView alloc] init]; // NSString *str = [calKeyView text]; // NSLog(@"%@", str); } - (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:. } - (void)dealloc { [_window release]; [super dealloc]; } @end // // CCLCalculatorView.m // TestCalculator // // Created by lanouhn on 14-8-20. // Copyright (c) 2014年 vaercly@163.com 陈聪雷. All rights reserved. // #import "CCLCalculatorView.h" @interface CCLCalculatorView () { UIButton *_btn; } @end @implementation CCLCalculatorView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code for (int i = 0; i < 4; i++) { _btn = [UIButton buttonWithType:UIButtonTypeSystem]; _btn.backgroundColor = [UIColor grayColor]; _btn.frame = CGRectMake(0 + 75 * i, 0, 55, 50); [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; _btn.layer.cornerRadius = 6; _btn.layer.masksToBounds = YES; _btn.titleLabel.font = [UIFont systemFontOfSize:30]; [_btn addTarget:self action:@selector(show:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:_btn]; } } return self; } - (id)initWithFrame:(CGRect)frame buttonTitle:(NSArray *)title { self = [self initWithFrame:frame]; if (self) { for (int i = 0; i < 4; i++) { UIButton *tempButton = (UIButton *)self.subviews[i]; NSString *tempTitle = [NSString stringWithFormat:@"%@", title[i]]; [tempButton setTitle:tempTitle forState:UIControlStateNormal]; } } return self; } - (NSString *)text:(NSString *)str { return [NSString stringWithFormat:@"%@", _btn.titleLabel]; } - (NSString *)show:(UIButton *)button { NSString *str = [NSString stringWithFormat:@"%@", button.titleLabel.text]; UIView *tempView = self.window.subviews[0]; UILabel *lable = tempView.subviews[0]; // [UIView beginAnimations:nil context:nil]; // [UIView setAnimationDuration:1]; // lable.layer.borderColor = [UIColor redColor].CGColor; // lable.layer.borderWidth = 2; // [UIView commitAnimations]; static CGFloat a;//存储第一个操作数 static char b;//存储符号 static char c[10];//依次存储输入的每个数字 static int i;//控制存储数字的位置 if ([str isEqualToString:@"+"] || [str isEqualToString:@"-"] || [str isEqualToString:@"*"] || [str isEqualToString:@"/"]) { a = [lable.text floatValue]; b = [str characterAtIndex:0]; NSLog(@"test1 a = %g str = %@ b = %c", a, str, b); for (int i = 0; i < 10; i++) { c[i] = '\0'; } i = -1; lable.text = nil; } else if ([str isEqualToString:@"="]) { NSLog(@"test2 a = %g str = %@ b = %c", a, str, b); if (b == '+') { lable.text = [NSString stringWithFormat:@"%g", (a + [lable.text floatValue])]; } else if (b == '-') { lable.text = [NSString stringWithFormat:@"%g", (a - [lable.text floatValue])]; } else if (b == '*') { lable.text = [NSString stringWithFormat:@"%g", (a * [lable.text floatValue])]; } else { lable.text = [NSString stringWithFormat:@"%g", (a / [lable.text floatValue])]; } } else if ([str isEqualToString:@"C"]) { NSLog(@"C"); i--; c[i] = '\0'; i--; lable.text = [NSString stringWithFormat:@"%s", c]; } else if ([str isEqualToString:@"MC"]) { NSLog(@"MC"); for (int i = 0; i < 10; i++) { c[i] = '\0'; } lable.text = [NSString stringWithFormat:@"%s", c]; i = -1; } else if ([str isEqualToString:@"M+"]) { NSLog(@"M+"); } else if ([str isEqualToString:@"M-"]) { NSLog(@"M-"); } else { c[i] = [str characterAtIndex:0]; lable.text = [NSString stringWithFormat:@"%s", c]; // lable.text = str; } i++; return str; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end
相关文章推荐
- iOS简易计算器的实现
- Antlr实现的简易计算器(C#版)
- 在Cisco设备上实现IOS DHCP Server的功能
- js实现简易计算器
- ios实现基于socket tcp/ip的通讯
- 应用内实现 iOS 通讯录查询与删除功能的代码
- 实现 iOS 应用自动编译
- iOS-实现简单的动画效果
- iOS实现本地通知
- ios实现基于socket tcp/ip 的通讯
- 用C#实现的简易计算器
- 栈的应用:进制转换和简易计算器的实现
- iOS实现本地通知(local notification)的方法
- 在Windows电脑中实现iPhone4 iOS 4.2.1 AirPrint(无线打印)功能
- Swing实现的简易计算器
- 用C#实现的简易计算器
- javascript实现简易计算器
- 用js实现简易计算器(只能实现加减乘除)
- ios实现通讯录的查询与删除