//
// AppDelegate.m
// TheCalculator
//
// Created by lanouhn on 14-8-20.
// Copyright (c) 2014年 airuru. All rights reserved.
//
#import "AppDelegate.h"
#define button_color [UIColor grayColor]
#define calculate_color [UIColor clearColor]
#define title_color [UIColor whiteColor]
@interface AppDelegate ()
{
UIView *_containerView;
BOOL _isHas;
BOOL _isOK;
UITextField *_field;
}
@property(retain ,nonatomic) NSString *_str;
@end
@implementation AppDelegate
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
_containerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
_containerView.backgroundColor = [UIColor darkGrayColor];
[self.window addSubview:_containerView];
[_containerView release];
[self calculator];
_isHas = NO;
_isOK = NO;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
-(void)dealloc{
[_window release];
[__str release];
[super dealloc];
}
-(void)calculator{
_field = [[UITextField alloc]initWithFrame:CGRectMake(0, 30, 320, 120)];
_field.backgroundColor = [UIColor blackColor];
_field.delegate = self;
_field.tag = 100;
_field.autocorrectionType = UITextAutocorrectionTypeNo;
_field.text = @"0";
_field.font = [UIFont systemFontOfSize:30];
_field.textColor =[ UIColor whiteColor];
UIView *view = [[UIView alloc] init];
_field.inputView = view;
[view release];
_field.textAlignment = NSTextAlignmentRight;
_field.layer.cornerRadius = 10;
_field.layer.masksToBounds = YES;
[_containerView addSubview:_field];
[_field release];
UIButton *butt = [UIButton buttonWithType:UIButtonTypeSystem];
butt.frame = CGRectMake(260, 150, 60, 35);
butt.backgroundColor = [UIColor clearColor];
[butt setTitle:@"←" forState:UIControlStateNormal];
butt.titleLabel.font = [UIFont systemFontOfSize:30];
[butt addTarget:self action:@selector(deleteNow:) forControlEvents:UIControlEventTouchUpInside];
[_containerView addSubview:butt];
NSArray *array = @[@"mc",@"m+",@"m-",@"mr",@"AC"];
for (int i = 0 ; i < 5 ; i++) {
UIButton *butn = [UIButton buttonWithType:UIButtonTypeSystem];
[butn setTitle:array[i] forState:UIControlStateNormal];
butn.backgroundColor = button_color;
[butn.layer setBorderColor:[[UIColor brownColor] CGColor]];
[butn.layer setBorderWidth:0.5];
[butn setTitleColor:title_color forState:UIControlStateNormal];
butn.layer.cornerRadius = 32;
butn.titleLabel.font = [UIFont systemFontOfSize:30];
butn.frame = CGRectMake(0 + (i * 64), 185, 64, 64);
butn.backgroundColor = [UIColor lightGrayColor];
if (i == 4) {
[butn addTarget:self action:@selector(clean:) forControlEvents:UIControlEventTouchUpInside];
butn.backgroundColor = [UIColor redColor];
}
[_containerView addSubview:butn];
}
NSArray *arr = @[@"1",@"2",@"3",@"+",@"4",@"5",@"6",@"-",@"7",@"8",@"9",@"*",@".",@"0",@"=",@"/"];
NSInteger n = 0;
NSInteger i = 0, j = 0;
for (i = 0 ; i < 4; i++) {
for (j = 0; j < 4; j++) {
NSLog(@"hehehe");
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(0 +(j * 80), 248 + (i * 80), 80, 80);
[btn setTitle:arr[n] forState:UIControlStateNormal];
btn.backgroundColor = button_color;
[btn setTitleColor:title_color forState:UIControlStateNormal];
[btn.layer setBorderColor:[[UIColor brownColor] CGColor]];
[btn.layer setBorderWidth:0.5];
btn.titleLabel.font = [UIFont systemFontOfSize:30];
btn.layer.cornerRadius = 40;
if (n == 3 || n == 7 || n == 11 || n == 15 ) {
[btn addTarget:self action:@selector(calculation:) forControlEvents:UIControlEventTouchUpInside];
btn.backgroundColor = [UIColor orangeColor];
} else if(n == 14){
[btn addTarget:self action:@selector(result:) forControlEvents:UIControlEventTouchUpInside];
btn.backgroundColor = [UIColor redColor];
}else if (n == 0 || n == 1 || n == 2 || n == 4 ||n == 5 || n == 6 || n == 8 || n == 9|| n == 10 ||n == 12|| n== 13) {
[btn addTarget:self action:@selector(number:) forControlEvents:UIControlEventTouchUpInside];
}
[_containerView addSubview:btn];
n++;
}
j = 0;
}
}
-(void)deleteNow:(UIButton *)butten{
NSString *str = _field.text;
NSUInteger i = [str length];
if ([str isEqualToString:@"0"]) {
}else if(i == 1){
_field.text =@"0";
}else{
_field.text = [_field.text substringToIndex:[str length] - 1];
}
}
-(void)clean:(UIButton *)button{
_field.text = @"0";
self._str = @"";
}
-(void)number:(UIButton *)button{
if ( _isHas || [_field.text isEqualToString:@"0"] || _isOK ) {
_field.text = @"";
_field.text = [_field.text stringByAppendingString:button.currentTitle];
}else{
_field.text = [_field.text stringByAppendingString:button.currentTitle];
}
_isHas = NO;
_isOK = NO;
}
-(void)calculation:(UIButton *)betten{
if ([betten.currentTitle isEqualToString:@"+"]) {
self._str = [_field.text stringByAppendingString:@"+"];
_isHas = YES;
} else if ([betten.currentTitle isEqualToString:@"-"]) {
self._str = [_field.text stringByAppendingString:@"-"];
_isHas = YES;
} else if ([betten.currentTitle isEqualToString:@"*"]) {
self._str = [_field.text stringByAppendingString:@"*"];
_isHas = YES;
}else if ([betten.currentTitle isEqualToString:@"/"]) {
self._str = [_field.text stringByAppendingString:@"/"];
_isHas = YES;
}
}
-(void)result:(UIButton *)butten{
NSLog(@"等于");
if ([self._str hasSuffix:@"+"]) {
CGFloat value1 = [self._str floatValue];
CGFloat value2 = [_field.text floatValue];
_field.text = [NSString stringWithFormat:@"%g",(value1 + value2)];
self._str = @"";
_isOK = YES;
}else if ([self._str hasSuffix:@"-"]){
CGFloat value1 = [self._str floatValue];
CGFloat value2 = [_field.text floatValue];
_field.text = [NSString stringWithFormat:@"%g",(value1 - value2)];
self._str = @"";
_isOK = YES;
}else if([self._str hasSuffix:@"*"]){
CGFloat value1 = [self._str floatValue];
CGFloat value2 = [_field.text floatValue];
_field.text = [NSString stringWithFormat:@"%g",(value1 * value2)];
self._str = @"";
_isOK = YES;
} else if([self._str hasSuffix:@"/"] && (![_field.text isEqualToString:@"0"])){
CGFloat value1 = [self._str floatValue];
CGFloat value2 = [_field.text floatValue];
_field.text = [NSString stringWithFormat:@"%g",(value1 / value2)];
self._str = @"";
_isOK = YES;
}else{
self._str = @"";
_isOK = 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:.
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
return YES;
}// called when 'return' key pressed. return NO to ignore.
@end