Amazon.com Widgets Chapter 7 Exercises 1 - 5. My solutions
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 23, 2014, 03:05:32 AM
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
| |-+ Answers to Exercises
| | |-+ Chapter 7
| | | |-+ Chapter 7 Exercises 1 - 5. My solutions
Pages: [1] Go Down
Print
Author Topic: Chapter 7 Exercises 1 - 5. My solutions (Read 760 times)
Nick Harvey
Newbie
*
Posts: 19






on: February 04, 2012, 01:19:48 AM

Here are my solutions for the exercises in chapter 7. I am new to C (and object-oriented programming in general) so I would really appreciate some feedback. As long as you are constructive, you can be as brutal as you like. This includes naming standards and readability as well as programming logic.

My projects have been created on XCode 4 with automatic reference counting so I have no memory management.

When working with my Fraction class, I encountered the following problems (amongst others) which I hope to have fixed:
Negative numerator, denominator (or both).
Resulting fraction from operation being negative.
Zero denominator, controlled program exit.
1/1 being entered as a fraction.

-- Fraction.h
#import <Foundation/Foundation.h>

@interface Fraction : NSObject
{
  int numerator;
  int denominator;
}

@property int numerator, denominator;

-(void) printWithReduce: (BOOL) reduceFirst;
-(void) setTo: (int) n over: (int) d;
-(Fraction *) add: (Fraction *) inFr;
-(Fraction *) subtract: (Fraction *) inFr;
-(Fraction *) multiply: (Fraction *) inFr;
-(Fraction *) divide: (Fraction *) inFr;
-(void) reduce;
-(void) testNegative;
-(double) convertToNum;

@end

-- Fraction.m
#import "Fraction.h"

@implementation Fraction

@synthesize numerator, denominator;

-(void) printWithReduce: (BOOL) reduceFirst
{
  [self testNegative];
  Fraction *printFraction = [Fraction new];
  int printNumber = 0;
 
  [printFraction setTo:numerator over:denominator];
  if (abs(printFraction.numerator) >= abs(printFraction.denominator))
  {
    printNumber = printFraction.numerator / printFraction.denominator;
    printFraction.numerator -= printNumber * printFraction.denominator;
    printFraction.numerator = abs(printFraction.numerator);
  }
  if (reduceFirst)
    [printFraction reduce];
 
  if (printFraction.numerator == 0)
    NSLog(@"%i", printNumber);
  else
    if (printNumber != 0)
      NSLog(@"%i %i/%i", printNumber, printFraction.numerator, printFraction.denominator);     
    else
      NSLog(@"%i/%i", printFraction.numerator, printFraction.denominator);
}

-(void) setTo: (int) n over: (int) d
{
  if (d == 0)
  {
    NSLog(@"Invalid fraction entered!");
    exit(0);
  }
  numerator = n;
  denominator = d;
  [self testNegative];
}

-(Fraction *) add: (Fraction *) inFr
{
  Fraction *retFr = [Fraction new];

  retFr.numerator = (numerator * inFr.denominator) + (denominator * inFr.numerator);
  retFr.denominator = denominator * inFr.denominator;
 
  [retFr reduce];

  return retFr;
}

-(Fraction *) subtract:(Fraction *)inFr
{
  Fraction *retFr = [Fraction new];
 
  retFr.numerator = (numerator * inFr.denominator) - (denominator * inFr.numerator);
  retFr.denominator = denominator * inFr.denominator;
 
  [retFr reduce];
 
  return retFr;
}

-(Fraction *) multiply:(Fraction *)inFr
{
  Fraction *retFr = [Fraction new];
 
  retFr.numerator = (numerator * inFr.numerator);
  retFr.denominator = denominator * inFr.denominator;
 
  [retFr reduce];
 
  return retFr;
}

-(Fraction *) divide:(Fraction *)inFr
{
  Fraction *retFr = [Fraction new];
 
  retFr.numerator = (numerator * inFr.denominator);
  retFr.denominator = denominator * inFr.numerator;

  [retFr reduce];
 
  return retFr;
}

-(void) testNegative
{
  if (denominator <= 0)
  {
    numerator = -numerator;
    denominator = -denominator;
  }
}

-(void) reduce
{
  int u = numerator;
  int v = denominator;
  int temp;
  while (v != 0)
  {
    temp = u % v;
    u = v;
    v = temp;
  }
  numerator /= u;
  denominator /= u;
  [self testNegative];
}

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

-- main.m
#import "Fraction.h"

int main (int argc, const char * argv[])
{

  @autoreleasepool
  {
    Fraction *aFraction = [Fraction new];
    Fraction *bFraction = [Fraction new];
    Fraction *resFraction;
    int inNumerator, inDenominator;
   
    NSLog(@"Type in first Fraction:");
    scanf("%i/%i", &inNumerator, &inDenominator);
   
    [aFraction setTo:inNumerator over:inDenominator];

    NSLog(@"Type in second Fraction:");
    scanf("%i/%i", &inNumerator, &inDenominator);
   
    [bFraction setTo:inNumerator over:inDenominator];
   
    NSLog(@"Fraction a is:");
    [aFraction printWithReduce:YES];

    NSLog(@"Fraction b is:");
    [bFraction printWithReduce:YES];
   
    resFraction = [aFraction add:bFraction];
    NSLog(@"Fraction a + Fraction b is:");
    [resFraction printWithReduce:YES];
   
    resFraction = [aFraction subtract:bFraction];
    NSLog(@"Fraction a - Fraction b is:");
    [resFraction printWithReduce:YES];
   
    resFraction = [aFraction multiply:bFraction];
    NSLog(@"Fraction a * Fraction b is:");
    [resFraction printWithReduce:YES];
   
    resFraction = [aFraction divide:bFraction];
    NSLog(@"Fraction a / Fraction b is:");
    [resFraction printWithReduce:YES];
  }
    return 0;
}
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.