ARTICLE

Calculator in iPhone

Posted by Sachin Bhardwaj Articles | iPhone/iPad November 24, 2012
In this article I will explain how to create a calculator application in iPhone.
Reader Level:
Download Files:
 

Introduction

In this article I will create a single-view application. Here I use one text field, 10 buttons for each digit and another 6 buttons for operations. To better understand use this walthrough.

Step 1

Open XCode by double-clicking on it.

Select-xcode-in-iphone.jpg

Step 2

Create a New XCode Project by clicking on it.

create-project-in-iphone.jpg

Step 3

Now select Single View Application and click on Next.

single-view-application-in-iphone.jpg

Step 4

Now provide your Product Name and Company Identifier then click on Next.

project-name-in-iPhone.jpg

Step 5

Select the location where you want to save your project and click on Create.

save-project-location-in-iphone.jpg

Step 6

Here we write code:

CalciViewController.h

//
// CalciViewController.h
// Calculator
//
// Created by Sachin Bhardwaj on 23/11/12.
// Copyright (c) 2012 Sachin Bhardwaj. All rights reserved.
//

#import <UIKit/UIKit.h>
typedef enum{ Plus,Minus,Multiply,Divide} CalcOperation;
@interface CalciViewController : UIViewController<UITextFieldDelegate>
{
IBOutlet UITextField *display;
IBOutlet UIButton *cbutton;
NSString *storage;
CalcOperation operation;

}
@property(strong,nonatomic)IBOutlet UITextField *display;

@property(strong,nonatomic) IBOutlet UIButton *cbutton;

@property(strong,nonatomic)NSString *storage;

- (IBAction) button1;
- (IBAction) button2;
- (IBAction) button3;
- (IBAction) button4;
- (IBAction) button5;
- (IBAction) button6;
- (IBAction) button7;
- (IBAction) button9;
- (IBAction) button0;
- (IBAction) plusbutton;
- (IBAction) equalsbutton;
- (IBAction) clearDisplay;

@end

CalciViewController.m

//
//  CalciViewController.m
//  Calculator
//
//  Created by Sachin Bhardwaj on 23/11/12.
//  Copyright (c) 2012 Sachin Bhardwaj. All rights reserved.
//

#import "CalciViewController.h"

@interface CalciViewController ()

@end

@implementation CalciViewController
@synthesize display,cbutton,storage;

- (IBAction) clearDisplay {
    display.text = @"";
}

- (IBAction) button1 {
   
    display.text=[NSString stringWithFormat:@"%@1",display.text];
}
- (IBAction) button2 {
    display.text=[NSString stringWithFormat:@"%@2",display.text];
}
- (IBAction) button3 {
    display.text=[NSString stringWithFormat:@"%@3",display.text];
}

- (IBAction) button4 {
    display.text=[NSString stringWithFormat:@"%@4",display.text];
}

- (IBAction) button5 {
    display.text=[NSString stringWithFormat:@"%@5",display.text];
}

- (IBAction) button6 {
    display.text=[NSString stringWithFormat:@"%@6",display.text];
}

- (IBAction) button7 {
    display.text=[NSString stringWithFormat:@"%@7",display.text];
}

- (IBAction) button8 {
    display.text=[NSString stringWithFormat:@"%@8",display.text];
}

- (IBAction) button9 {
    display.text=[NSString stringWithFormat:@"%@9",display.text];
}

- (IBAction) button0 {
    display.text=[NSString stringWithFormat:@"%@0",display.text];
}

- (IBAction) plusbutton {
    operation = Plus;
    storage = display.text;
    display.text=@"";
}

- (IBAction) minusbutton {
    operation = Minus;
    storage = display.text;
    display.text=@"";
}

- (IBAction) multiplybutton {
    operation = Multiply;
    storage = display.text;
    display.text=@"";
}

- (IBAction) dividebutton {
    operation = Divide;
    storage = display.text;
    display.text=@"";
}

- (IBAction) equalsbutton {
    NSString *val = display.text;
    switch(operation) {
        case Plus :
            display.text= [NSString stringWithFormat:@"%qi",[val longLongValue]+[storage longLongValue]];
            break;
        case Minus:
            display.text= [NSString stringWithFormat:@"%qi",[storage longLongValue]-[val longLongValue]];
            break;
        case Divide:
            display.text= [NSString stringWithFormat:@"%qi",[storage longLongValue]/[val longLongValue]];
            break;
        case Multiply:
            display.text= [NSString stringWithFormat:@"%qi",[val longLongValue]*[storage longLongValue]];
            break;
    }
}


- (void)viewDidLoad
{
    [super viewDidLoad];
      // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

CalciViewController.xib

CalciViewController.xib-in-iPhone.jpg

Step 7

After mapping all connections we click on the file owner:

action-in-iPhone.jpg

Step 8

At last we run our application and see the output.

run-in-iPhone.jpg

Output

Here we add two numbers: 54+42=96.

Output 1 in iPhone:

output1-in-iPhone.jpg

Output 2 in iPhone:

output2-in-iPhone.jpg

Output 3 in iPhone:

output3-in-iPhone.jpg

Output 4 in iPhone:

output4-in-iPhone.jpg

Login to add your contents and source code to this article
post comment
     

it is compatible to only mac environment. I mean it run only on Xcode(ide).

Posted by Sachin Bhardwaj Dec 26, 2012

i was amazing but i have one query that it is compatible to all operating system i.e.,window xp,2007 or it is only compatible to mac os.

Posted by kinjal patel Dec 21, 2012
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter