Amazon.com Widgets Fraction Calculator on iPhone problem :-(
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 19, 2014, 04:51:52 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Chapter Study
| | |-+ Chapter 21 - Writing iPhone Applications
| | | |-+ Fraction Calculator on iPhone problem :-(
Pages: [1] Go Down
Print
Author Topic: Fraction Calculator on iPhone problem :-( (Read 1941 times)
lolipop
Newbie
*
Posts: 5






on: October 31, 2009, 01:06:50 PM

So, while making the fraction calculator from the book, I did the exact code in the book, and did everything correctly. And so it worked.
So thats nice, so I tweaked it. And then it didn't work.
There are no errors, but I am keep getting the answer as 0 when I try to use it. What did I do wrong?
What I tweaked it is I made it so you can enter as many fractions you want at once, rather then only two. I made an array, put the fractions in there, and for calculating I enumerated and added (or whatever) them. Its lots of code, but please help!

Fraction Class
Code: (Objective-C)
#import <UIKit/UIKit.h>



@interface Fraction : NSObject <NSCopying>
{
int numerator;
int denominator;

}

@property int numerator, denominator;

- (void) print;
- (void) setTo: (int) n over: (int) d;
- (double) convertToNum;
- (NSString *) convertToString;
- (void) reduce;

- (Fraction*) add: (Fraction *) f;
- (Fraction*) subtract: (Fraction *) f;
- (Fraction*) multiply: (Fraction *) f;
- (Fraction*) divide: (Fraction *) f;


@end
Code: (Objective-C)
#import "Fraction.h"


@implementation Fraction

@synthesize numerator, denominator;

- (void) print
{
NSLog (@"%i/%i", numerator, denominator);
}

- (double) convertToNum
{
if (denominator != 0)
return (double) numerator / denominator;
else
return 1.0;
}

- (NSString *) convertToString {
if (numerator == 0)
return @"0";
if (numerator == denominator) {
return @"1";
}
else if ( denominator ==  1)
return [NSString stringWithFormat: @"%i", numerator];
else
return [NSString stringWithFormat: @"%i/%i", numerator, denominator];
}

- (void) setTo: (int) n over: (int) d
{
numerator = n;
denominator = d;

}



- (void) reduce
{
int u = numerator;
int v = denominator;
int temp;

while (v != 0) {
temp = u % v;
u = v;
v = temp;
}

numerator /= u;
denominator /= u;
}


- (Fraction*) add: (Fraction *) f
{
// To add two fractions:
// a/b + c/d = ((a*d) + (b*c)) / (b * d)

Fraction *result = [[Fraction alloc] init];
int resultNum, resultDenom;

resultNum = (numerator * f.denominator) + (denominator * f.numerator);
resultDenom = denominator * f.denominator;

[result setTo: resultNum over: resultDenom];

[result reduce];
return [result autorelease];
}

- (Fraction*) subtract: (Fraction *) f
{
// To subtract two fractions:
// a/b - c/d = ((a*d) - (b*c)) / (b * d)

Fraction *result = [[Fraction alloc] init];
int resultNum, resultDenom;

resultNum = (numerator * f.denominator) - (denominator * f.numerator);
resultDenom = denominator * f.denominator;

[result setTo: resultNum over: resultDenom];

[result reduce];
return [result autorelease];
}

- (Fraction*) divide: (Fraction *) f
{

Fraction *result = [[Fraction alloc] init];
int resultNum, resultDenom;

resultNum = (numerator * f.denominator);
resultDenom = denominator * f.numerator;

[result setTo: resultNum over: resultDenom];

[result reduce];
return [result autorelease];
}

- (Fraction*) multiply: (Fraction *) f
{

Fraction *result = [[Fraction alloc] init];
int resultNum, resultDenom;

resultNum = (numerator * f.numerator);
resultDenom = denominator * f.denominator;

[result setTo: resultNum over: resultDenom];

[result reduce];
return [result autorelease];
}
- (id) copyWithZone: (NSZone *) zone
{
Fraction *newFraction = [[Fraction allocWithZone: zone] init];

newFraction.numerator = self.numerator;
newFraction.denominator = self.denominator;

return [newFraction autorelease];
}


@end

View Controller
Code: (Objective-C)

#import <UIKit/UIKit.h>
#import "Calculator.h"

@interface Fraction_CalculatorViewController : UIViewController
{
UILabel         *display;
char             op;
int              currentNumber;
NSMutableString *displayString;
BOOL             isNumerator;
Calculator      *myCalculator;


}
@property (nonatomic, retain) IBOutlet UILabel *display;
@property (nonatomic, retain) NSMutableString *displayString;

-(void) processDigit: (int) digit;
-(void) processOp: (char) theOp;
-(void) storeFracPart;

// Numeric keys

-(IBAction) clickDigit: (id) sender;

//Arithmetic Operation keys

-(IBAction) clickPlus: (id) sender;
-(IBAction) clickMinus: (id) sender;
-(IBAction) clickMultiply: (id) sender;
-(IBAction) clickDivide: (id) sender;

// Misc. Keys

-(IBAction) clickOver: (id) sender;
-(IBAction) clickEquals: (id) sender;
-(IBAction) clickClear: (id) sender;

@end

Code: (Objective-C)
#import "Fraction_CalculatorViewController.h"

@implementation Fraction_CalculatorViewController

@synthesize display, displayString;


- (void)viewDidLoad {
isNumerator = YES;
self.displayString = [NSMutableString stringWithCapacity: 40];
myCalculator = [[Calculator alloc] init];
    [super viewDidLoad];
}

-(void) processDigit: (int) digit
{
currentNumber = currentNumber * 10 + digit;
[displayString appendString: [NSString stringWithFormat: @"%i", digit]];
[display setText: displayString];
}

-(IBAction) clickDigit: (id) sender
{
int digit = [sender tag];

[self processDigit: digit];
}

-(void) processOp: (char) theOp
{
NSString *opStr;
op = theOp;

switch (theOp) {
case '+':
opStr = @" + ";
break;
case '-':
opStr = @" - ";
break;
case '*':
opStr = @" * ";
break;
case '/':
opStr = @" / ";
break;
}


[self storeFracPart];
isNumerator = YES;
[myCalculator.fractions addObject: [myCalculator.operand copy]];
[myCalculator.operand setTo: 0 over: 1];

[displayString appendString: opStr];
[display setText: displayString];
}

-(void) storeFracPart
{
if (isNumerator) {
myCalculator.operand.numerator = currentNumber;
myCalculator.operand.denominator = 1; // e.g. 3 * 4/5


}
else {
myCalculator.operand.denominator = currentNumber;


}
currentNumber = 0 ;
}

-(IBAction) clickOver: (id) sender
{
[self storeFracPart];
isNumerator = NO;
[displayString appendString: @"/"];
[display setText: displayString];
}

// Arithmetic Operation keys

-(IBAction) clickPlus: (id) sender
{
[self processOp: '+'];
}

-(IBAction) clickMinus: (id) sender
{
[self processOp: '-'];
}

-(IBAction) clickMultiply: (id) sender
{
[self processOp: '*'];
}

-(IBAction) clickDivide: (id) sender
{
[self processOp: '/'];
}

// Misc. Keys

-(IBAction) clickEquals: (id) sender
{


[self storeFracPart];



[myCalculator performOperation: op];

[displayString appendString: @" = "];
[displayString appendString: [myCalculator.accumulator convertToString]];
[display setText: displayString];

currentNumber = 0;
isNumerator = YES;
[displayString setString: @""];




}

-(IBAction) clickClear: (id) sender
{
isNumerator = YES;

currentNumber = 0;
[myCalculator clear];

[displayString setString: @""];
[display setText: displayString];
}

- (void) dealloc
{
[myCalculator dealloc];
[super dealloc];
}

@end

Calculator Class
Code: (Objective-C)
#import <UIKit/UIKit.h>
#import "Fraction.h"

@interface Calculator : NSObject
{
Fraction *accumulator;
Fraction *operand;
NSMutableArray  *fractions;

}

@property (retain, nonatomic) Fraction *operand, *accumulator;
@property (retain, nonatomic) NSMutableArray *fractions;

- (void) performOperation: (char) op;
- (void) clear;

@end

Code: (Objective-C)
#import "Calculator.h"


@implementation Calculator
@synthesize operand, accumulator, fractions;

- (id) init
{
self = [super init];

operand = [[Fraction alloc] init];
accumulator = [[Fraction alloc] init];
 
return self;
}

- (void) clear
{
if (accumulator) {
accumulator.numerator = 0;
accumulator.denominator = 0;
}
}

- (void) performOperation: (char) op
{
Fraction *nextFrac;
[accumulator setTo: 0 over: 1];
switch (op) {

case '+':

for (nextFrac in fractions) {
accumulator = [accumulator add: nextFrac];
}
break;
case '-':
for (nextFrac in fractions) {
accumulator = [accumulator subtract: nextFrac];
}
break;
case '*':
for (nextFrac in fractions) {
accumulator = [accumulator multiply: nextFrac];
}
break;
case '/':
for (nextFrac in fractions) {
accumulator = [accumulator divide: nextFrac];
}
break;
}



}




-(void) dealloc
{
[operand release];
[accumulator release];
[super dealloc];
}
@end

Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: October 31, 2009, 09:08:06 PM

You're never allocating your fractions array.  I didn't look any further than that, as that's a show stopper.  Look at the init method in the Calculator class.  Don't forget to release it in dealloc if you use alloc to create it.

Cheers,

Steve Kochan
Last Edit: November 01, 2009, 10:39:59 AM by skochan Logged
lolipop
Newbie
*
Posts: 5






Reply #2 on: November 01, 2009, 09:37:01 AM

Wow, I can't believe I missed that! Thanks for bothering to read over the code :-D
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.