XCODE - Source Code + GUI "Calculator v1.0"
View Controller :
calViewController.h
#import <UIKit/UIKit.h>
@interface calViewController : UIViewController
{ /* Declare variable as private */
double currentNumber; // store current value
double storeNumber; // store old value
char Operator; // store sign of operator
bool point; // condition to floating point
int pointCount; // condition to number of floating point
}
// Prototype Function to use
// getCurrentNumber
- (void) getCurrentNumber:(double)number;
- (void) showCurrentNumber;
- (void) showStoreNumber;
/* Label for Showing number */
@property (weak, nonatomic) IBOutlet UIButton *bEqualOutlet;
@property (weak, nonatomic) IBOutlet UILabel *labelShow;
/* Button for all operand and operator */
- (IBAction)b:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *ac;
- (IBAction)bPlus:(id)sender;
- (IBAction)bMinus:(id)sender;
- (IBAction)bMulti:(id)sender;
- (IBAction)bDivid:(id)sender;
- (IBAction)bClear:(id)sender;
- (IBAction)bPower:(id)sender;
- (IBAction)bEqual:(id)sender;
- (IBAction)bPoint:(id)sender;
@end
calViewController.m
#import "calViewController.h"
@implementation calViewController
@synthesize ac;
@synthesize bEqualOutlet;
@synthesize labelShow;
/* Start Function */
//Function getCurrentNumber
- (void) getCurrentNumber:(double)number{
if(point == false){
currentNumber = currentNumber*10 + number;
}else{
currentNumber += number/pow(10,++pointCount);
}
}
//Function getLastStore
- (void) getLastStore{
if(Operator == '+'){
storeNumber += currentNumber;
}else if(Operator == '-'){
storeNumber -= currentNumber;
}else if(Operator == '*'){
storeNumber *= currentNumber;
}else if(Operator == '/'){
storeNumber /= currentNumber;
}else if(Operator == '^'){
storeNumber = pow(storeNumber,currentNumber);
}
currentNumber = 0;
}
//Function showCurrentNumber
- (void) showCurrentNumber{
self.labelShow.text = [NSString stringWithFormat:@"%g",currentNumber];
}
//Function showStoreNumber
-(void) showStoreNumber{
self.labelShow.text = [NSString stringWithFormat:@"%g",storeNumber];
}
/* End Function */
- (IBAction)b:(id)sender{ // button 0
double n = [sender tag];
[self getCurrentNumber:n];
[self showCurrentNumber];
}
- (IBAction)bPlus:(id)sender{ // button Plus +
[self getLastStore];
[self showStoreNumber];
Operator = '+';
point = false;
pointCount = 0;
}
- (IBAction)bMinus:(id)sender{ // button Minus -
[self getLastStore];
[self showStoreNumber];
Operator = '-';
point = false;
pointCount = 0;
}
- (IBAction)bMulti:(id)sender{ // button Multi *
[self getLastStore];
[self showStoreNumber];
Operator = '*';
point = false;
pointCount = 0;
}
- (IBAction)bDivid:(id)sender{ // button Divid /
[self getLastStore];
[self showStoreNumber];
Operator = '/';
point = false;
pointCount = 0;
}
- (IBAction)bClear:(id)sender{ // button Clear AC
currentNumber = 0;
storeNumber = 0;
point = false;
pointCount = 0;
Operator = '+';
[self showStoreNumber];
}
- (IBAction)bPower:(id)sender{ // button Power ^
[self getLastStore];
[self showStoreNumber];
Operator = '^';
point = false;
pointCount = 0;
}
- (IBAction)bEqual:(id)sender{ // button Equal =
[self getLastStore];
[self showStoreNumber];
point = true;
pointCount = 0;
Operator = '+';
}
- (IBAction)bPoint:(id)sender{ // button Point .
point = true;
pointCount = 0;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
/* Constructure */
currentNumber = 0;
storeNumber = 0;
Operator = '+';
point = false;
pointCount = 0;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setLabelShow:nil];
[self setBEqualOutlet:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
No comments:
Post a Comment