Created
August 7, 2012 19:56
-
-
Save chyld/3288816 to your computer and use it in GitHub Desktop.
iOS calc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CMViewController.m | |
// Xcalc | |
// | |
// Created by Chyld Medford on 8/7/12. | |
// Copyright (c) 2012 Chyld Medford. All rights reserved. | |
// | |
#import "CMViewController.h" | |
@interface CMViewController () | |
@end | |
@implementation CMViewController | |
@synthesize answer; | |
@synthesize math1; | |
@synthesize math2; | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view, typically from a nib. | |
[[self view] setBackgroundColor:[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"x1.jpg"]]]; | |
} | |
- (void)viewDidUnload | |
{ | |
[self setAnswer:nil]; | |
[self setMath1:nil]; | |
[self setMath2:nil]; | |
[super viewDidUnload]; | |
// Release any retained subviews of the main view. | |
} | |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation | |
{ | |
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); | |
} | |
- (IBAction)add:(id)sender { | |
int a = [[math1 text] intValue]; | |
int b = [[math2 text] intValue]; | |
int sum = a + b; | |
NSString *result = [NSString stringWithFormat:@"%d", sum]; | |
[answer setText:result]; | |
[self setAnswerColor:sum]; | |
} | |
- (IBAction)sub:(id)sender { | |
int a = [[math1 text] intValue]; | |
int b = [[math2 text] intValue]; | |
int diff = a - b; | |
NSString *result = [NSString stringWithFormat:@"%d", diff]; | |
[answer setText:result]; | |
[self setAnswerColor:diff]; | |
} | |
- (IBAction)mul:(id)sender { | |
int a = [[math1 text] intValue]; | |
int b = [[math2 text] intValue]; | |
int sum = a * b; | |
NSString *result = [NSString stringWithFormat:@"%d", sum]; | |
[answer setText:result]; | |
} | |
- (IBAction)div:(id)sender { | |
int a = [[math1 text] intValue]; | |
int b = [[math2 text] intValue]; | |
int sum = a / b; | |
NSString *result = [NSString stringWithFormat:@"%d", sum]; | |
[answer setText:result]; | |
} | |
- (IBAction)pow:(id)sender { | |
int a = [[math1 text] floatValue]; | |
int b = [[math2 text] floatValue]; | |
float power = powf(a, b); | |
NSString *result = [NSString stringWithFormat:@"%f", power]; | |
[answer setText:result]; | |
[self setAnswerColor:power]; | |
} | |
- (IBAction)sqr:(id)sender { | |
int a = [[math1 text] floatValue]; | |
//int b = [[math2 text] floatValue]; | |
float root = sqrtf(a); | |
NSString *result = [NSString stringWithFormat:@"%f", root]; | |
[answer setText:result]; | |
[self setAnswerColor:root]; | |
} | |
- (void)setAnswerColor:(int)value | |
{ | |
if(value < 0) | |
{ | |
[answer setTextColor:[UIColor redColor]]; | |
NSLog(@"%d", value); | |
} | |
else | |
{ | |
[answer setTextColor:[UIColor blackColor]]; | |
NSLog(@"%d", value); | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment