iPhone SDK math - pythagorean theorem problem!
Posted
by Flafla2
on Stack Overflow
See other posts from Stack Overflow
or by Flafla2
Published on 2010-05-12T20:58:03Z
Indexed on
2010/05/12
21:04 UTC
Read the original article
Hit count: 287
Just as a practice, I am working on an app that solves the famous middle school pythagorean theorem, a squared + b squared = c squared. Unfortunately, the out-coming answer has, in my eyes, nothing to do with the actual answer. Here is the code used during the "solve" action.
- (IBAction)solve {
int legoneint;
int legtwoint;
int hypotenuseint;
int lonesq = legoneint * legoneint;
int ltwosq = legtwoint * legtwoint;
int hyposq = hypotenuseint * hypotenuseint;
hyposq = lonesq + ltwosq;
if ([legone.text isEqual:@""]) {
legtwoint = [legtwo.text intValue];
hypotenuseint = [hypotenuse.text intValue];
answer.text = [NSString stringWithFormat:@"%d", legoneint];
self.view.backgroundColor = [UIColor blackColor];
}
if ([legtwo.text isEqual:@""]) {
legoneint = [legone.text intValue];
hypotenuseint = [hypotenuse.text intValue];
answer.text = [NSString stringWithFormat:@"%d", legtwoint];
self.view.backgroundColor = [UIColor blackColor];
}
if ([hypotenuse.text isEqual:@""]) {
legoneint = [legone.text intValue];
legtwoint = [legtwo.text intValue];
answer.text = [NSString stringWithFormat:@"%d", hypotenuseint];
self.view.backgroundColor = [UIColor blackColor];
}
}
By the way, legone, legtwo, and hypotenuse
all represent the UITextField
that corresponds to each mathematical part of the right triangle. Answer
is the UILabel
that tells, you guessed it, the answer. Does anyone see any flaws in the program? Thanks in advance!
© Stack Overflow or respective owner