Monday, August 31, 2009

Arithmetic expression and operation precedence

the basic mathematic operation

(+ – * /) + ( % )

Operator Precedence

Integer Arithmetic and the Unary Minus Operator

The Modulus Operator

 

Pages from Programming in Objective-C 2.0 (2nd Edition)-4_Page_1 Pages from Programming in Objective-C 2.0 (2nd Edition)-4_Page_2

 

 

//operator

#import <Foundation/Foundation.h>
int main(int var1,char *var2[])
{

NSAutoreleasePool *pool= [NSAutoreleasePool alloc ]init];
int a=400;
int b=200;
int c=100;
int d =10;

NSLog(@"a+b/c*d is :%i\n",(a+b/c*d));
NSLog(@"a+b-c*d is :%i\n",(a+b-c*d));

[pool drain];
}

//output

$ make
This is gnustep-make 2.2.0. Type 'make print-gnustep-make-help' for help.
Making all for tool LogTest...
Compiling file prog403.m ...
Linking tool LogTest ...

giripal sigh rawat@SUNIL ~/objc_prog403
$ ./obj/LogTest
2009-08-31 12:24:23.031 LogTest[2736] The result is -4000

source code download

The Type Cast Operator

Assignment Operators

// Implement a Calculator class
#import <Foundation/Foundation.h>
@interface Calculator: NSObject
{
double accumulator;
}
// accumulator methods
-(void) setAccumulator: (double) value;
-(void) clear;
-(double) accumulator;
// arithmetic methods
-(void) add: (double) value;
-(void) subtract: (double) value;
-(void) multiply: (double) value;
-(void) divide: (double) value;
@end
@implementation Calculator
-(void) setAccumulator: (double) value
{

accumulator = value;
}
-(void) clear
{
accumulator = 0;
}
-(double) accumulator
{
return accumulator;
}
-(void) add: (double) value
{
accumulator += value;
}
-(void) subtract: (double) value
{
accumulator -= value;
}
-(void) multiply: (double) value
{
accumulator *= value;
}
-(void) divide: (double) value
{
accumulator /= value;
}
@end
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Calculator *myCal;
myCal = [[Calculator alloc] init];
[myCal clear];
[myCal setAccumulator: 1000.0];
[myCal add: 2000.];
[myCal divide: 150.0];
[myCal subtract: 100.0];
[myCal multiply: 50];
NSLog (@"The result is %g", [myCal accumulator]);
[myCal release];
[pool drain];
return 0;
}

//output

giripal sigh rawat@SUNIL ~/objc_prog403
$ make
This is gnustep-make 2.2.0. Type 'make print-gnustep-make-help' for help.
Making all for tool LogTest...
Compiling file prog403.m ...
Linking tool LogTest ...

giripal sigh rawat@SUNIL ~/objc_prog403
$ ./obj/LogTest
2009-08-31 12:24:23.031 LogTest[2736] The result is -4000

giripal sigh rawat@SUNIL ~/objc_prog403
$

source code download

No comments:

Post a Comment