This is the Very simple application.If the user wants to do Addition,Subtraction, Multiplication, Division between two numbers, then they can easily do it using the this aaplication.
Step 1: Create a View base application using template. Give the application name “calci”.
Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.
Step 3: Xpand classes and notice Interface Builder created the calciViewController class for you. Expand Resources and notice the template generated a separate nib,calciViewController.xib, for the “calci”.
Step 4: In the calciViewController.h file, we have added two String and six methods. So make the following changes in the file:
@interface calciViewController : UIViewController
{
IBOutlet id textViewer;
int operator;
NSString *previous;
NSString *current;
}
-(IBAction)butonDigit:(id)sender;
-(IBAction)butonEqual:(id)sender;
-(void)doEquals;
-(IBAction)buttonOperator:(id)sender;
-(IBAction)butonDecimal:(id)sender;
-(IBAction)butonclear:(id)sender;
Step 5: Double click the calciViewController.xib file open it to the Interface Builder. First drag seventeen Round Rect from the library and place it to the view window, give the name in the round rect 1 to 10 digits and operators sign that means “+”,”-”,”*”,”/”, dot sing, and clr (See the figure below). Next drag label from the library and place it to the view window. Now connect File’s Owner icon to the view icon and select view. Drag from the File’s Owner icon to view icon and select textViewer. Select the 1st Round Rect from the view window and bring up Connection Inspector and drag from the Touch Up Inside to the File’s owner icon and select buttonDigit: method, do the same thing for the next 9 digit. Select the “+” operator from the view window and bring up Connection Inspector drag from the Touch Up Inside to the File’s Owner icon and select buttonOperator: method , follow the same procedure for the next 3 operators and select the clr button from the view window and bring up Connection Inspector and drag from the Touch Up Inside to the File’s Owner icon and select “buttonclear” . Lastly select the dot button and bring up Connection Inspector and drag from the Touch Up Inside to the File’s Owner icon and select the buttonDecimal: method. You have done all the connection, now save the calciViewController.xib file , save it and lose the Xcode.
Step 6: Open the calciViewController.m file and make the following changes in the file:
{
self=[super init];
if(self)
{
operator=0;
current =[[NSString stringWithString:@"0"]retain];
previous =[[NSString stringWithString:@"0"]retain];
}
return self;
}
-(IBAction)butonDigit:(id)sender
{
//NSLog(@"aaaaaaaaaaaaaaaaaaa");
NSString *str= (NSString* )[sender currentTitle];
if([current isEqualToString:@"0"])
{
current= str;
}else
{
current=[current stringByAppendingString:str];
}
[textViewer setText:current];
}
-(IBAction)butonEqual:sender
{
[self doEquals];
}
-(IBAction)buttonOperator:sender
{
NSString *tmpstr =[current substringFromIndex:([current length]-1)];
if([tmpstr isEqualToString:@"."])
{
current = (NSString *)[current substringToIndex:([current length] - 1)];
[textViewer setText:current];
}
NSString *str=(NSString *)[sender currentTitle];
if(operator >=1 && operator<=4)
{
[self doEquals];
}
if(operator!=5)
{
previous = [current copy];
current =@"0";
}
if ([str isEqualToString:@"+"])
{
operator=1;
}else if([str isEqualToString:@"-"]){
operator=2;
}else if([str isEqualToString:@"*"])
{
operator=3;
}else if([str isEqualToString:@"/"])
{
operator=4;
}
}
-(void) doEquals
{
if (operator >= 1 && operator <= 4) {
NSDecimalNumber* num1 = 0;
num1 = [NSDecimalNumber decimalNumberWithString:previous];
NSDecimalNumber* num2 = 0;
num2 = [NSDecimalNumber decimalNumberWithString:current];
if (operator == 1)
{
num1 = [num1 decimalNumberByAdding:num2];
current = [NSString stringWithString:[num1 stringValue]];
} else if (operator == 2){
num1 = [num1 decimalNumberBySubtracting:num2];
current = [NSString stringWithString:[num1 stringValue]];
} else if (operator == 3){
num1 = [num1 decimalNumberByMultiplyingBy:num2];
current = [NSString stringWithString:[num1 stringValue]];
} else if (operator == 4){
if (![current isEqualToString:@"0"]) {
num1 = [num1 decimalNumberByDividingBy:num2];
current = [NSString stringWithString:[num1 stringValue]];
}
}
[textViewer setText:current];
previous = [current copy];
current = @"0";
operator = 5;
}
}
-(IBAction)butonDecimal:(id)sender
{
NSRange range = [current rangeOfString:@"."options:(NSCaseInsensitiveSearch)];
if (range.location == NSNotFound)
{
current =[current stringByAppendingString:@"."];
}
[textViewer setText:current];
}
-(IBAction)butonclear:(id)sender
{
current=@"0";
previous=@"0";
operator=0;
[textViewer setText:current];
}
Step 7: Now compile and run the application in the Simulator.
You can Download SourceCode from here calci













this is very bad the functions dont work the decimals are returning errors which make it freeze