Reputation: 6529
i have been looking for a tutorial for a scientific calculator one the web but i could not find anything. my aim is to learn how to write a calculator app similar to the one in iPhone. does any know of any tutorials that can help me learn how to add scientific functions to a calculator app. keep in mind that i have tried the various versions of the basic calculator and have been through a few of those tutorials and just want to learn and may use the methods to add scientific functions such as rad, sin, cos and some other stuff in the app. i will be great full for any help i can get.
AP
Edit:
since there are no real material on how to build a scientific calculator out there( at least i haven't found anything specific yet) here is the run down of how to build one.
first create a NSObject class to hold your operation methods. ( this is optional, you can set it on top of your getter (.m) file if you like.) call it whatever you want. then declare the following the the header file.
{
double operand;
double savedNumber;
NSString *waitingOperation;
double waitingOperand;
}
- (void)setOperand:(double)aDouble;
- (double)performOperation:(NSString *)operation;
- (void)performWaitingOperation;
@end
then in the .m file of your NSObject class declare the methods that operates your functions. something like the following:
-(void)setOperand:(double)num
{
operand=num;
}
-(double)performOperation:(NSString *)operation
{
if([operation isEqual:@"sqrt"]){
operand = sqrt(operand);
}else if ([operation isEqual:@"1/x"]){
if(operand)
operand = 1 / operand;
}else if ([operation isEqual:@"2/x"]){
if(operand)
operand = 2 / operand;
}else if ([operation isEqual:@"3/x"]){
if(operand)
operand = 3 / operand;
}else if ([operation isEqual:@"+/-"]){
if(operand)
operand = (-1) * operand;
}else if ([operation isEqual:@"sin"]){
operand = sin(operand);
}else if ([operation isEqual:@"sinh"]){//you can add more scientific functions in the same way to this list.
operand = sinh(operand);
}else if ([operation isEqual:@"cos"]){
operand = cos(operand);
}else if ([operation isEqual:@"cosh"]){
operand = cosh(operand);
}else if ([operation isEqual:@"tan"]){
operand = tan(operand);
}else if ([operation isEqual:@"tanh"]){
operand = tanh(operand);
}else if ([operation isEqual:@"M"]){
savedNumber = operand;
}else if ([operation isEqual:@"M+"] ){
savedNumber += operand;
}else if ([operation isEqual:@"M-"] ){
savedNumber -= operand;
}else if ([operation isEqual:@"Recall"]){
[self setOperand:savedNumber];
}else if ([operation isEqual:@"C"]){
savedNumber = 0;
operand = 0;
waitingOperand = 0;
savedNumber= 0;
}else {
[self performWaitingOperation];
waitingOperation = operation;
waitingOperand = operand;
}
return operand;
}
- (void) performWaitingOperation
{
if([@"+" isEqual:waitingOperation]){
operand = waitingOperand + operand ;
} else if([@"-" isEqual:waitingOperation]){
operand = waitingOperand - operand ;
}else if([@"X" isEqual:waitingOperation]){
operand = waitingOperand * operand ;
}else if([@"/" isEqual:waitingOperation]){
if(operand)
operand = waitingOperand / operand ;
}
}
now in the view controller that you are trying to display the calculator you need to access all of these functions and display them in your outlets, such as buttons and text field and so on. here is the .h file
IBOutlet UILabel *display;
SCalcAI *ai;
BOOL userIsInTheMiddleOfTypingNumber;
}
- (IBAction) digitPressed: (UIButton *)sender;
- (IBAction) operationPressed: (UIButton *) sender;
rememebr to import your NSObject class header on top of this file otherwise you get an error. then in the .m file you need to declare these functions.
-(NameofyourNSOBjectClass *) ai
{
if(!ai)
{
ai = [[SCalcAI alloc] init];
}
return ai;
}
- (IBAction) digitPressed: (UIButton *)sender
{
NSString *digit = [[sender titleLabel] text];
if(userIsInTheMiddleOfTypingNumber){
[display setText:[[display text] stringByAppendingString:digit]];
} else{
[display setText:digit];
userIsInTheMiddleOfTypingNumber = YES;
}
}
- (IBAction) operationPressed: (UIButton *) sender
{
if(userIsInTheMiddleOfTypingNumber){
[[self ai] setOperand:[[display text] doubleValue]];
userIsInTheMiddleOfTypingNumber = NO;
}
NSString *operation = [[sender titleLabel] text];
double result = [[self ai] performOperation:operation];
[display setText:[NSString stringWithFormat:@"%g", result]];
}
then hook up all the buttons to the operations they need to be connected in the storyboard or xib. thats that. the above is the skeleton version of the whole thing but I'm sure you can build on that. if you need help send me message.
Upvotes: 2
Views: 4596
Reputation: 18497
The Standford iOS programming class cs193p has an example of building a simple calculator. Might be a good place to start. Great course, great videos. Paul Hagarty is a very good instructor. The scientific functions would not be hard to add once you have the core done.
Two resources: http://www.tusharsharma.in/project/open-source/mathematical-calculator-iphone4/
http://emplementation.blogspot.com/2010/11/ios-development-tutorials-on-itune-u.html
Upvotes: 3