Note that there are some explanatory texts on larger screens.

plurals
  1. POscientific calculator tutorial for iPhone/ipad
    primarykey
    data
    text
    <p>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.</p> <p>AP</p> <p>Edit:</p> <p>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.</p> <p>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.</p> <pre><code>{ double operand; double savedNumber; NSString *waitingOperation; double waitingOperand; } - (void)setOperand:(double)aDouble; - (double)performOperation:(NSString *)operation; - (void)performWaitingOperation; @end </code></pre> <p>then in the .m file of your NSObject class declare the methods that operates your functions. something like the following:</p> <pre><code>-(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 ; } } </code></pre> <p>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</p> <pre><code>IBOutlet UILabel *display; SCalcAI *ai; BOOL userIsInTheMiddleOfTypingNumber; } - (IBAction) digitPressed: (UIButton *)sender; - (IBAction) operationPressed: (UIButton *) sender; </code></pre> <p>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.</p> <pre><code>-(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]]; } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload