Created
March 5, 2012 04:24
-
-
Save b-adams/1976608 to your computer and use it in GitHub Desktop.
CS132 Program 3 code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********************************/ | |
/* WARNING! These are snippets */ | |
/* and NOT the entire code! */ | |
/* You'll need to implement the */ | |
/* designated initializer and */ | |
/* most of the arithmetic. */ | |
/********************************/ | |
-(id) initWithReal:(int)reNum | |
over:(int)reDen | |
andImaginary:(int)imNum | |
over:(int)imDen | |
{ | |
return [self initWithReal:[[Fraction alloc] initWithNumerator: reNum | |
andDenominator: reDen] | |
andImaginary:[[Fraction alloc] initWithNumerator: imNum | |
andDenominator: imDen]]; | |
} | |
-(id) init | |
{ | |
//By default, initialize to 0+0i | |
return [self initWithReal:[[Fraction alloc] initWithInteger:0] | |
andImaginary:[[Fraction alloc] initWithInteger:0]]; | |
} | |
-(NSString*) description | |
{ | |
/* UNCOMMENT FOR PRETTIER COMPLEX NUMBERS */ | |
/* | |
BOOL noReal = ([[self realPart] numerator] == 0); | |
BOOL noImag = ([[self imaginaryPart] numerator] == 0); | |
if(noReal && noImag) | |
return @"[0]"; | |
if(noReal) | |
return [NSString stringWithFormat:@"[i(%@)]", | |
[self imaginaryPart]]; | |
if(noImag) | |
return [NSString stringWithFormat:@"[(%@)]", | |
[self realPart]]; | |
*/ | |
return [NSString stringWithFormat:@"[(%@) + i(%@)]", | |
[self realPart], [self imaginaryPart]]; | |
} | |
-(ComplexFraction*) subtract: (ComplexFraction*) otherNumber | |
{ | |
// [a+ib] - [c+id] = [a+ib] + neg([c+id]) | |
return [self add:[otherNumber negative]]; | |
} | |
-(ComplexFraction*) divide: (ComplexFraction*) otherNumber | |
{ | |
// [a+ib] / [c+id] = [a+ib] * reciprocal([c+id]) | |
return [self multiply:[otherNumber reciprocal]]; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2012-03-04 23:11:14.900 cs132_program_2_badams[54909:403] Initial Complex Fraction: [(1/4) + i(1/4)] | |
2012-03-04 23:11:14.903 cs132_program_2_badams[54909:403] Testing -add | |
x + x is: [(1/2) + i(1/2)] | |
should be [(1/2) + i(1/2)] | |
2012-03-04 23:11:14.904 cs132_program_2_badams[54909:403] Testing -multiply | |
x * x is: [(0) + i(1/8)] | |
should be [(0) + i(1/8)] | |
2012-03-04 23:11:14.904 cs132_program_2_badams[54909:403] Testing -negative | |
0 - x is: [(-1/4) + i(-1/4)] | |
should be [(-1/4) + i(-1/4)] | |
2012-03-04 23:11:14.906 cs132_program_2_badams[54909:403] Testing -reciprocal | |
1 / x is: [(2) + i(-2)] | |
should be [(2) + i(-2)] | |
2012-03-04 23:11:14.907 cs132_program_2_badams[54909:403] Testing -subtract | |
x - x is: [(0) + i(0)] | |
should be [(0) + i(0)] | |
2012-03-04 23:11:14.907 cs132_program_2_badams[54909:403] Testing -divide | |
x / x is: [(1) + i(0)] | |
should be [(1) + i(0)] | |
Testing your patience |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
New accounts opened! | |
Balances are: | |
checking: 5.00 | |
loSavings: 5.00 | |
noSavings: 5.00 | |
Making deposits... | |
Depositing 95.00 to loSavings | |
Depositing 100.00 to noSavings | |
Depositing 105.00 to checking | |
Balances are: | |
checking: 110.00 | |
loSavings: 100.00 | |
noSavings: 105.00 | |
Time passes... | |
Balances are: | |
checking: 110.00 | |
loSavings: 164.46 | |
noSavings: 105.00 | |
Can we successfully withdraw $101.00 from... | |
checking: Sure | |
loSavings: Sure | |
noSavings: Sure | |
Balances are: | |
checking: 9.00 | |
loSavings: 63.46 | |
noSavings: 4.00 | |
Time passes... | |
Balances are: | |
checking: 9.00 | |
loSavings: 104.37 | |
noSavings: 4.00 | |
Can we successfully withdraw $101.00 from... | |
checking: Nope | |
loSavings: Sure | |
noSavings: Nope | |
Balances are: | |
checking: 9.00 | |
loSavings: 3.37 | |
noSavings: 4.00 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* \file main.m | |
* \author Prof. Adams | |
* \brief Suite of test cases for Prog2 Fraction and ComplexFraction classes | |
* \version 1.0 - 120205 | |
* \version 1.1 - 120209 - Changed at-dox to slash-dox; testReducedDisplay now has void params rather than empty params; testReducableArithmetic has improved left-right differentiation; | |
* \version 1.2 - 120210 - Bad function call with too many arguments fixed | |
* \version 2.0 - 120304 - Moving Fraction tests to commented-out subfunction. Adding ComplexFraction tests | |
*/ | |
#import <Foundation/Foundation.h> | |
#import "Fraction.h" | |
#import "ComplexFraction.h" | |
void testComplexFractions(void); | |
void testFractions(void); | |
/** | |
\brief Make sure the various special display cases are being handled | |
*/ | |
void testDisplay(Fraction* lFraction); | |
/** | |
\brief Make sure that the reduce message results in a reduced fraction | |
*/ | |
void testReduction(Fraction* lFraction); | |
/** | |
\brief Check that the arithmetic operators are working | |
*/ | |
void testArithmetic(Fraction* lFraction, Fraction* rFraction); | |
/** | |
\brief Check that the arithmetic operators are working and give reduced results | |
*/ | |
void testReducableArithmetic(Fraction* lFraction, Fraction* rFraction); | |
/** | |
\brief Check that reducedDisplay message doesn't change the fraction | |
*/ | |
void testReducedDisplay(void); | |
/** | |
\brief Good 'ol main! Here's where it all begins... | |
*/ | |
int main (void) | |
{ | |
@autoreleasepool { | |
//testFractions(); | |
testComplexFractions(); | |
} | |
printf("\n\nTesting your patience\n\n"); | |
return EXIT_SUCCESS; | |
} | |
void testDisplay(Fraction* lFraction) | |
{ | |
printf("\n\nTesting display\n\n"); | |
NSLog(@"Setting numerator to %d and denominator to %d\n",1,4); | |
[lFraction setNumerator:1 andDenominator:4]; | |
NSLog(@"Left Fraction is [%@] = [%g] (should be 1/4)", | |
[lFraction description], | |
[lFraction convertToNum]); | |
NSLog(@"Setting numerator to %d\n",2); | |
[lFraction setNumerator:2]; | |
NSLog(@"Left Fraction is [%@] = [%g] (should be 2/4)", | |
[lFraction description], | |
[lFraction convertToNum]); | |
NSLog(@"Setting denominator to %d\n",2); | |
[lFraction setDenominator:2]; | |
NSLog(@"Left Fraction is [%@] = [%g] (should be 2/2)", | |
[lFraction description], | |
[lFraction convertToNum]); | |
NSLog(@"Reducing\n"); | |
[lFraction reduce]; | |
NSLog(@"Left Fraction is [%@] = [%g] (should be 1)", | |
[lFraction description], | |
[lFraction convertToNum]); | |
NSLog(@"Setting numerator to %d\n",4); | |
[lFraction setNumerator:4]; | |
NSLog(@"Left Fraction is [%@] = [%g] (should be 4)", | |
[lFraction description], | |
[lFraction convertToNum]); | |
NSLog(@"Setting denominator to %d\n",0); | |
[lFraction setDenominator:0]; | |
NSLog(@"Left Fraction is [%@] = [%g] (should be NaN)", | |
[lFraction description], | |
[lFraction convertToNum]); | |
NSLog(@"Setting numerator to %d\n",0); | |
[lFraction setNumerator:0]; | |
NSLog(@"Left Fraction is [%@] = [%g] (should be NaN)", | |
[lFraction description], | |
[lFraction convertToNum]); | |
NSLog(@"Setting denominator to %d\n",1); | |
[lFraction setDenominator:1]; | |
NSLog(@"Left Fraction is [%@] = [%g] (should be 0)", | |
[lFraction description], | |
[lFraction convertToNum]); | |
NSLog(@"Setting denominator to %d\n",5); | |
[lFraction setDenominator:5]; | |
NSLog(@"Left Fraction is [%@] = [%g] (should be 0)", | |
[lFraction description], | |
[lFraction convertToNum]); | |
} | |
void testReduction(Fraction* lFraction) | |
{ | |
void testReduction(Fraction* lFraction); | |
printf("\n\nTesting reduction\n\n"); | |
NSLog(@"Setting numerator to %d and denominator to %d\n",10,-5); | |
[lFraction setNumerator:10 andDenominator:-5]; | |
NSLog(@"Left Fraction is [%@] = [%g] (should be 10/-5)", | |
[lFraction description], | |
[lFraction convertToNum]); | |
NSLog(@"Reducing\n"); | |
[lFraction reduce]; | |
NSLog(@"Left Fraction is [%@] = [%g] (should be -2)", | |
[lFraction description], | |
[lFraction convertToNum]); | |
} | |
void testArithmetic(Fraction* lFraction, Fraction* rFraction) | |
{ | |
Fraction * resultFraction = NULL; | |
printf("\n\nTesting arithmetic\n\n"); | |
NSLog(@"Setting Left numerator to %d and denominator to %d\n",2,3); | |
[lFraction setNumerator:2 andDenominator:3]; | |
NSLog(@"Left Fraction is [%@] = [%g] (should be 2/3)", | |
[lFraction description], | |
[lFraction convertToNum]); | |
NSLog(@"Setting Right numerator to %d and denominator to %d\n",5,7); | |
[rFraction setNumerator:5 andDenominator:7]; | |
NSLog(@"Right Fraction is [%@] = [%g] (should be 5/7)", | |
[rFraction description], | |
[rFraction convertToNum]); | |
NSLog(@"Adding left and right into result"); | |
resultFraction = [lFraction add:rFraction]; | |
NSLog(@"Result is [%@] = [%g] (should be 29/21)", | |
[resultFraction description], | |
[resultFraction convertToNum]); | |
NSLog(@"Left Fraction is [%@] = [%g] (should still be 2/3)", | |
[lFraction description], | |
[lFraction convertToNum]); | |
NSLog(@"Right Fraction is [%@] = [%g] (should still be 5/7)", | |
[rFraction description], | |
[rFraction convertToNum]); | |
//Not needed with ARC | |
resultFraction = NULL; | |
NSLog(@"Result = Left minus Right"); | |
resultFraction = [lFraction subtract:rFraction]; | |
NSLog(@"Result is [%@] = [%g] (should be -1/21 or 1/-21)", | |
[resultFraction description], | |
[resultFraction convertToNum]); | |
NSLog(@"Left Fraction is [%@] = [%g] (should still be 2/3)", | |
[lFraction description], | |
[lFraction convertToNum]); | |
NSLog(@"Right Fraction is [%@] = [%g] (should still be 5/7)", | |
[rFraction description], | |
[rFraction convertToNum]); | |
//Not needed with ARC | |
resultFraction = NULL; | |
NSLog(@"Result = Left times Right"); | |
resultFraction = [lFraction multiply:rFraction]; | |
NSLog(@"Result is [%@] = [%g] (should be 10/21)", | |
[resultFraction description], | |
[resultFraction convertToNum]); | |
NSLog(@"Left Fraction is [%@] = [%g] (should still be 2/3)", | |
[lFraction description], | |
[lFraction convertToNum]); | |
NSLog(@"Right Fraction is [%@] = [%g] (should still be 5/7)", | |
[rFraction description], | |
[rFraction convertToNum]); | |
//Not needed with ARC | |
resultFraction = NULL; | |
NSLog(@"Result = Left divided by Right"); | |
resultFraction = [lFraction divideBy:rFraction]; | |
NSLog(@"Result is [%@] = [%g] (should be 14/15)", | |
[resultFraction description], | |
[resultFraction convertToNum]); | |
NSLog(@"Left Fraction is [%@] = [%g] (should still be 2/3)", | |
[lFraction description], | |
[lFraction convertToNum]); | |
NSLog(@"Right Fraction is [%@] = [%g] (should still be 5/7)", | |
[rFraction description], | |
[rFraction convertToNum]); | |
//Not needed with ARC | |
resultFraction = NULL; | |
} | |
void testReducableArithmetic(Fraction* lFraction, Fraction* rFraction) | |
{ | |
Fraction * resultFraction = NULL; | |
printf("\n\nTesting arithmetic with reducable results\n\n"); | |
NSLog(@"Setting Left numerator to %d and denominator to %d\n",4,6); | |
[lFraction setNumerator:4 andDenominator:6]; | |
NSLog(@"Left Fraction is [%@] = [%g] (should be 4/6)", | |
[lFraction description], | |
[lFraction convertToNum]); | |
NSLog(@"Setting Right numerator to %d and denominator to %d\n",3,9); | |
[rFraction setNumerator:3 andDenominator:9]; | |
NSLog(@"Right Fraction is [%@] = [%g] (should be 3/9)", | |
[rFraction description], | |
[rFraction convertToNum]); | |
NSLog(@"Adding left and right into result"); | |
resultFraction = [lFraction add:rFraction]; | |
NSLog(@"Result is [%@] = [%g] (should be 1)", | |
[resultFraction description], | |
[resultFraction convertToNum]); | |
//Not needed with ARC | |
resultFraction = NULL; | |
NSLog(@"Result = Left minus Right"); | |
resultFraction = [lFraction subtract:rFraction]; | |
NSLog(@"Result is [%@] = [%g] (should be 1/3)", | |
[resultFraction description], | |
[resultFraction convertToNum]); | |
//Not needed with ARC | |
resultFraction = NULL; | |
NSLog(@"Result = Left times Right"); | |
resultFraction = [lFraction multiply:rFraction]; | |
NSLog(@"Result is [%@] = [%g] (should be 2/9)", | |
[resultFraction description], | |
[resultFraction convertToNum]); | |
//Not needed with ARC | |
resultFraction = NULL; | |
NSLog(@"Result = Left divided by Right"); | |
resultFraction = [lFraction divideBy:rFraction]; | |
NSLog(@"Result is [%@] = [%g] (should be 2)", | |
[resultFraction description], | |
[resultFraction convertToNum]); | |
//Not needed with ARC | |
resultFraction = NULL; | |
NSLog(@"Left Fraction is [%@] = [%g] (should still be 4/6)", | |
[lFraction description], | |
[lFraction convertToNum]); | |
NSLog(@"Right Fraction is [%@] = [%g] (should still be 3/9)", | |
[rFraction description], | |
[rFraction convertToNum]); | |
} | |
void testReducedDisplay(void) | |
{ | |
printf("\n\nTesting reduced display\n\n"); | |
Fraction* testFraction = [[Fraction alloc] init]; | |
[testFraction setNumerator:14 andDenominator:21]; | |
NSLog(@"Displaying %d/%d standard and reduced: \n\t standard: [%@] (should be 14/21)\n\t reduced: [%@] (should be 2/3)\n\t standard: [%@] (should be 14/21)", | |
14, 21, [testFraction description], [testFraction reducedDescription], [testFraction description]); | |
testFraction = NULL; | |
} | |
void testFractions(void) | |
{ | |
Fraction * lFraction = NULL; | |
Fraction * rFraction = NULL; | |
printf("\n\nTesting object instantiation\n\n"); | |
NSLog(@"Creating Left Fraction"); | |
lFraction = [[Fraction alloc] init]; | |
NSLog(@"Left Fraction is [%@] = [%g]", | |
[lFraction description], | |
[lFraction convertToNum]); | |
NSLog(@"Creating Right Fraction"); | |
rFraction = [[Fraction alloc] init]; | |
NSLog(@"Right Fraction is [%@] = [%g] (is probably NaN)", | |
[rFraction description], | |
[rFraction convertToNum]); | |
testDisplay(lFraction); | |
testReduction(lFraction); | |
testArithmetic(lFraction, rFraction); | |
testReducableArithmetic(lFraction, rFraction); | |
testReducedDisplay(); | |
} | |
void testComplexFractions(void) | |
{ | |
ComplexFraction* x = nil; | |
ComplexFraction* y = nil; | |
x = [[ComplexFraction alloc] initWithReal:1 over:4 andImaginary:1 over:4]; | |
NSLog(@"Initial Complex Fraction: %@\n\n", x); | |
y = [x add:x]; | |
NSLog(@"Testing %s\nx %c x is:\t%@\nshould be\t[(%s) + i(%s)]\n\n", | |
"-add", '+', y,"1/2","1/2"); | |
y = [x multiply:x]; | |
NSLog(@"Testing %s\nx %c x is:\t%@\nshould be\t[(%s) + i(%s)]\n\n", | |
"-multiply", '*', y,"0","1/8"); | |
y = [x negative]; | |
NSLog(@"Testing %s\n0 %c x is:\t%@\nshould be\t[(%s) + i(%s)]\n\n", | |
"-negative", '-', y,"-1/4","-1/4"); | |
y = [x reciprocal]; | |
NSLog(@"Testing %s\n1 %c x is:\t%@\nshould be\t[(%s) + i(%s)]\n\n", | |
"-reciprocal", '/', y,"2","-2"); | |
y = [x subtract:x]; | |
NSLog(@"Testing %s\nx %c x is:\t%@\nshould be\t[(%s) + i(%s)]\n\n", | |
"-subtract", '-', y,"0","0"); | |
y = [x divide:x]; | |
NSLog(@"Testing %s\nx %c x is:\t%@\nshould be\t[(%s) + i(%s)]\n\n", | |
"-divide", '/', y,"1","0"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @file main.m | |
* @brief Test code for use with Program 3 | |
* @author Prof. Adams | |
* @date Mar 4 2012 | |
* @version 1.0 - 120304 | |
*/ | |
#import <Foundation/Foundation.h> | |
#import "InterestAccount.h" | |
#import "BankAccount.h" | |
void testAccounts(void); | |
/** @brief Main for program 3 | |
* | |
* Runs account-testing and complex-fraction-testing code | |
*/ | |
int main(void) | |
{ | |
@autoreleasepool { | |
testAccounts(); | |
return EXIT_SUCCESS; | |
} | |
} | |
/** @brief Account testing for program 3 | |
* | |
* Create three accounts (one regular bank, two interest) | |
* Alternate displaying balances with making deposits, | |
* withdrawals, and passing time (to accrue interest) | |
*/ | |
void testAccounts(void) | |
{ | |
BankAccount* checking=nil; | |
InterestAccount* loSavings=nil; | |
InterestAccount* noSavings=nil; | |
float amount=NAN; | |
checking = [[BankAccount alloc] init]; | |
loSavings = [[InterestAccount alloc] initWithInterestRate:0.01]; | |
noSavings = [[InterestAccount alloc] init]; | |
printf("New accounts opened!\n"); | |
printf("\nBalances are: \n"); | |
printf("%s:\t%4.2f\n", "checking", [checking balance]); | |
printf("%s:\t%.2f\n", "loSavings", [loSavings balance]); | |
printf("%s:\t%.2f\n", "noSavings", [noSavings balance]); | |
printf("\nMaking deposits...\n"); | |
amount = 95; | |
printf("Depositing %.2f to %s\n", amount, "loSavings"); | |
[loSavings deposit:amount]; | |
amount = 100; | |
printf("Depositing %.2f to %s\n", amount, "noSavings"); | |
[noSavings deposit:amount]; | |
amount = 105; | |
printf("Depositing %.2f to %s\n", amount, "checking"); | |
[checking deposit:amount]; | |
printf("\nBalances are: \n"); | |
printf("%s:\t%4.2f\n", "checking", [checking balance]); | |
printf("%s:\t%.2f\n", "loSavings", [loSavings balance]); | |
printf("%s:\t%.2f\n", "noSavings", [noSavings balance]); | |
printf("\nTime passes...\n"); | |
[loSavings passTime:50]; | |
[noSavings passTime:50]; | |
printf("\nBalances are: \n"); | |
printf("%s:\t%4.2f\n", "checking", [checking balance]); | |
printf("%s:\t%.2f\n", "loSavings", [loSavings balance]); | |
printf("%s:\t%.2f\n", "noSavings", [noSavings balance]); | |
amount = 101; | |
printf("\nCan we successfully withdraw $%.2f from...\n", amount); | |
printf("%s:\t%s\n", "checking", [checking withdraw:amount]?"Sure":"Nope"); | |
printf("%s:\t%s\n", "loSavings", [loSavings withdraw:amount]?"Sure":"Nope"); | |
printf("%s:\t%s\n", "noSavings", [noSavings withdraw:amount]?"Sure":"Nope"); | |
printf("\nBalances are: \n"); | |
printf("%s:\t%4.2f\n", "checking", [checking balance]); | |
printf("%s:\t%.2f\n", "loSavings", [loSavings balance]); | |
printf("%s:\t%.2f\n", "noSavings", [noSavings balance]); | |
printf("\nTime passes...\n"); | |
[loSavings passTime:50]; | |
[noSavings passTime:50]; | |
printf("\nBalances are: \n"); | |
printf("%s:\t%4.2f\n", "checking", [checking balance]); | |
printf("%s:\t%.2f\n", "loSavings", [loSavings balance]); | |
printf("%s:\t%.2f\n", "noSavings", [noSavings balance]); | |
amount = 101; | |
printf("\nCan we successfully withdraw $%.2f from...\n", amount); | |
printf("%s:\t%s\n", "checking", [checking withdraw:amount]?"Sure":"Nope"); | |
printf("%s:\t%s\n", "loSavings", [loSavings withdraw:amount]?"Sure":"Nope"); | |
printf("%s:\t%s\n", "noSavings", [noSavings withdraw:amount]?"Sure":"Nope"); | |
printf("\nBalances are: \n"); | |
printf("%s:\t%4.2f\n", "checking", [checking balance]); | |
printf("%s:\t%.2f\n", "loSavings", [loSavings balance]); | |
printf("%s:\t%.2f\n", "noSavings", [noSavings balance]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment