Skip to content

Instantly share code, notes, and snippets.

@chyld
Created August 7, 2012 19:56
Show Gist options
  • Save chyld/3288816 to your computer and use it in GitHub Desktop.
Save chyld/3288816 to your computer and use it in GitHub Desktop.
iOS calc
//
// 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