Simple Calculator
main.m
// calculator
//
// Created by Venkatesh Pachigulla on 8/24/12.
// For Main.m, AppDelegate.h, AppDelegate.m There is No need to do any changes.
//leave it....ViewController.xib
->This is the main part for the application.
->Drag and drop the label,buttons from UIControl panel as shown in the image.
-> Tag the numbers from 0-9 and for operators 0-6 [ for related oreder verify ViewController.m]
-> tag is left side ->AttributeInspector->view->tag
ViewController.h
#import <UIKit/UIKit.h>
@interface venkiViewController : UIViewController
{
float result;
IBOutlet UILabel *calculatescreen;
int currentoperation;
float currentnumber;
}
-(IBAction)buttonDigitPressed:(id)sender;
-(IBAction)buttonOperatorPressed:(id)sender;
-(IBAction)cancelInput;
-(IBAction)cancelOperation;
@end
#import "venkiViewController.h"
@implementation venkiViewController
-(IBAction)buttonDigitPressed:(id)sender{
currentnumber= currentnumber*10+(float)[sender tag];
calculatescreen.text=[NSString stringWithFormat:@"%2f",currentnumber];
}
-(IBAction)buttonOperatorPressed:(id)sender{
if(currentoperation==0)
{
result=currentnumber;
}
else {
switch (currentoperation) {
case 1:
result=result+currentnumber;
break;
case 2:
result=result-currentnumber;
break;
case 3:
result=result*currentnumber;
break;
case 4:
result=result/currentnumber;
break;
case 5:
result=(int)result % (int)currentnumber;
break;
case 6:
currentoperation=0;
break;
}
}
currentnumber=0;
calculatescreen.text=[NSString stringWithFormat:@"%2f",result];
if([sender tag]==0)
result=0;
currentoperation=[sender tag];
}
-(IBAction)cancelInput{
currentnumber=0;
calculatescreen.text=@"0";
}
-(IBAction)cancelOperation{
currentnumber=0;
calculatescreen.text=@"0";
currentoperation=0;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// UIImage *img= [UIImage imageNamed:@"iPhone.png"];
// UIImageView *imgview =[[UIImageView alloc]initWithImage:img];
//
// [self.view addSubview:imgview];
// [self.view addSubview:calculatescreen];
}
@end
tags:simple calculator in ios,sample calculator program in iphone,iphone sample codes,iphone sample application, iphone sample projects, sample programs in iphone, sample apps for iphone with source code

Posted in:
0 comments:
Post a Comment