Calculator
Here we are start new project “Calculator” in platform Xcode V6.4 otherwise above that.
Step 2-And give product name as you wish or ‘CalculatorDemo’ given bellow type.
Step 3- Now go on simulator in ‘Main.storyboard’ and create bellow type formate of calculator using ‘buttons’ and ‘textfield’.
Step 4-Now give separate ‘Action’ and ‘Outlet’ as same as bellow type by press, hold and drag and drop on ‘ViewController.h’.
Step 5-Now take property bellow type
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
NSString *storage;
NSString *operation;
double sum;
int i;
}
Here we are take string ‘storage’ that is using for temporary storage because when we are put value on calculator that time that is store data into the ‘storage’.
And work of ‘operation’ that is store string data of operations like :Addition,Subtraction,Multiplication and Division.
here ’sum’ is take double that is data type and ‘i’ also integer data type.All of use are understanding when you are run this project.
Step 6-Now we are writing bellow type code between curly brackets of created buttons.Show down
- (IBAction)button1:(id)sender {
display.text=[NSString stringWithFormat:@"%@1",display.text];
}
- (IBAction)button2:(id)sender {
display.text=[NSString stringWithFormat:@"%@2",display.text];
}
- (IBAction)button3:(id)sender {
display.text=[NSString stringWithFormat:@"%@3",display.text];
}
- (IBAction)button4:(id)sender {
display.text=[NSString stringWithFormat:@"%@4",display.text];
}
- (IBAction)button5:(id)sender {
display.text=[NSString stringWithFormat:@"%@5",display.text];
}
- (IBAction)button6:(id)sender {
display.text=[NSString stringWithFormat:@"%@6",display.text];
}
- (IBAction)button7:(id)sender {
display.text=[NSString stringWithFormat:@"%@7",display.text];
}
- (IBAction)button8:(id)sender {
display.text=[NSString stringWithFormat:@"%@8",display.text];
}
- (IBAction)button9:(id)sender {
display.text=[NSString stringWithFormat:@"%@9",display.text];
}
- (IBAction)button10:(id)sender {
display.text=[NSString stringWithFormat:@"%@0",display.text];
}
- (IBAction)dotbutton:(id)sender {
if ( i==1 ) {
i=2;
display.text=[NSString stringWithFormat:@"%@.",display.text];
}
}
- (IBAction)clearbutton:(id)sender {
i=1;
display.text=@"";
sum=0;
}
Show up here we take i=1 because when we are pressed ‘dotbutton’ then if next time press that so that can not except method because here we are take also i=2 so next time here we got 2 and our condition is if( i==1 ) then that passing that event otherwise dot(‘.’) can’t call.Same as clear button.
Step 7-Now write into ‘Operational’ buttons bellow type.
- (IBAction)addbutton:(id)sender {
operation=@"add";
sum = sum+[display.text doubleValue];
display.text=[NSString stringWithFormat:@"%f",sum];
[self calculation];
}
- (IBAction)subbutton:(id)sender {
operation=@"sub";
sum = sum+[display.text doubleValue];
display.text=[NSString stringWithFormat:@"%f",sum];
[self calculation];
}
- (IBAction)mulbutton:(id)sender {
operation=@"mul";
sum = sum+[display.text doubleValue];
display.text=[NSString stringWithFormat:@"%f",sum];
[self calculation];
}
- (IBAction)divibutton:(id)sender {
operation=@"divi";
sum = sum+[display.text doubleValue];
display.text=[NSString stringWithFormat:@"%f",sum];
[self calculation];
}
Here we are take method ‘calculation’ and that is add into operation button because that is store data into storage that is we are understanding above.
-(void)calculation{
storage=display.text;
display.text=@"";
}
Now we are understand use of ’operation’ here operations are different of different buttons so when we are passed string into so computer fetch direct data form string here taken (operation=@"add"; operation=@"sub"; operation=@"mul”; and operation=@"divi”;) show bellow.
Step 8-Now we are write into equal buttons bellow type code
- (IBAction)equalbutton:(id)sender {
i=1;
i=1;
NSString *val=display.text;
if ( [operation isEqualToString:@"add"]) {
display.text= [NSString stringWithFormat:@"%lld",[val longLongValue]+[storage longLongValue]];
}
else if ([operation isEqualToString:@"sub"]){
if (val > storage) {
display.text= [NSString stringWithFormat:@"%lld",[val longLongValue]-[storage longLongValue]];
}
else{
display.text= [NSString stringWithFormat:@"%lld",[storage longLongValue]-[val longLongValue]];
}
}
else if ([operation isEqualToString:@"mul"]){
display.text= [NSString stringWithFormat:@"%lld",[val longLongValue]*[storage longLongValue]];
}
else if([operation isEqualToString:@"divi"]){
display.text= [NSString stringWithFormat:@"%lld",[storage longLongValue]/[val longLongValue]];
}
Here you can see our full code of ‘ViewController.m’.
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize display;
- (void)viewDidLoad {
[super viewDidLoad];
i=1;
sum=0;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)calculation{
storage=display.text;
display.text=@"";
}
- (IBAction)button1:(id)sender {
display.text=[NSString stringWithFormat:@"%@1",display.text];
}
- (IBAction)button2:(id)sender {
display.text=[NSString stringWithFormat:@"%@2",display.text];
}
- (IBAction)button3:(id)sender {
display.text=[NSString stringWithFormat:@"%@3",display.text];
}
- (IBAction)button4:(id)sender {
display.text=[NSString stringWithFormat:@"%@4",display.text];
}
- (IBAction)button5:(id)sender {
display.text=[NSString stringWithFormat:@"%@5",display.text];
}
- (IBAction)button6:(id)sender {
display.text=[NSString stringWithFormat:@"%@6",display.text];
}
- (IBAction)button7:(id)sender {
display.text=[NSString stringWithFormat:@"%@7",display.text];
}
- (IBAction)button8:(id)sender {
display.text=[NSString stringWithFormat:@"%@8",display.text];
}
- (IBAction)button9:(id)sender {
display.text=[NSString stringWithFormat:@"%@9",display.text];
}
- (IBAction)button10:(id)sender {
display.text=[NSString stringWithFormat:@"%@0",display.text];
}
- (IBAction)dotbutton:(id)sender {
if ( i==1 ) {
i=2;
display.text=[NSString stringWithFormat:@"%@.",display.text];
}
}
- (IBAction)clearbutton:(id)sender {
i=1;
display.text=@"";
sum=0;
}
- (IBAction)addbutton:(id)sender {
operation=@"add";
sum = sum+[display.text doubleValue];
display.text=[NSString stringWithFormat:@"%f",sum];
[self calculation];
}
- (IBAction)subbutton:(id)sender {
operation=@"sub";
sum = sum+[display.text doubleValue];
display.text=[NSString stringWithFormat:@"%f",sum];
[self calculation];
}
- (IBAction)mulbutton:(id)sender {
operation=@"mul";
sum = sum+[display.text doubleValue];
display.text=[NSString stringWithFormat:@"%f",sum];
[self calculation];
}
- (IBAction)divibutton:(id)sender {
operation=@"divi";
sum = sum+[display.text doubleValue];
display.text=[NSString stringWithFormat:@"%f",sum];
[self calculation];
}
- (IBAction)equalbutton:(id)sender {
i=1;
NSString *val=display.text;
if ( [operation isEqualToString:@"add"]) {
display.text= [NSString stringWithFormat:@"%lld",[val longLongValue]+[storage longLongValue]];
}
else if ([operation isEqualToString:@"sub"]){
if (val > storage) {
display.text= [NSString stringWithFormat:@"%lld",[val longLongValue]-[storage longLongValue]];
}
else{
display.text= [NSString stringWithFormat:@"%lld",[storage longLongValue]-[val longLongValue]];
}
}
else if ([operation isEqualToString:@"mul"]){
display.text= [NSString stringWithFormat:@"%lld",[val longLongValue]*[storage longLongValue]];
}
else if([operation isEqualToString:@"divi"]){
display.text= [NSString stringWithFormat:@"%lld",[storage longLongValue]/[val longLongValue]];
}
}
@end
Step 9-Now run your project and you show bellow type window.
Step 10-Now you can use of your own calculator.
Now click HERE.
Comments