Skip to content

Instantly share code, notes, and snippets.

@b-adams
Created December 2, 2011 04:46
Show Gist options
  • Save b-adams/1421799 to your computer and use it in GitHub Desktop.
Save b-adams/1421799 to your computer and use it in GitHub Desktop.
CS131 Program 8
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define BONUS true
/* REPLACE THIS COMMENT WITH YOUR DOCUMENTED DECLARATIONS (structure and functions) */
/**
@brief Displays result of combining a couple fractions
@param lhs pointer to left-hand fraction
@param rhs pointer to right-hand fraction
@param operation '+' for sum, '*' for product
@returns nothing, like a good void function
@sideeffects Prints lhs, operator, rhs, an equal sign, and what they combine to form
*/
void check(Fraction* lhs, Fraction* rhs, char operation); //DO NOT ALTER
/**
@brief Main. You know, like... main.
@returns 0 if it works.
@sideeffect Checks a bunch of random fractions, then a couple more random fractions, then a couple user-input fractions.
*/
int main(void) //DO NOT ALTER
{
Fraction* x;
Fraction* y;
for(int i=1024; i>0; i/=2) //Weird but valid!
{
check(makeRandomFraction(), makeRandomFraction(), '*');
check(makeRandomFraction(), makeRandomFraction(), '+');
}
for(char c='a'; c<'c'; c++) //Also weird. Also valid.
{
x = getFractionFromUser(); //Fetch once...
y = getFractionFromUser();
check(x,y,'+'); //Use
check(y,x,'+'); //Multiple
check(x,y,'*'); //Times
check(y,x,'*'); //without having to re-fetch the number
}
return EXIT_SUCCESS;
}
void check(Fraction* lhs, Fraction* rhs, char operation) //DO NOT ALTER
{
Fraction* result;
switch(operation)
{
case '+': //If the caller of the function wants to add...
result = fractionSum(lhs, rhs); //then we add
break;
case '*': //If they want to multiply
result = fractionProduct(lhs, rhs); //then we multiply
break;
default: //If they want something else
printf("Sorry, '%c' is not a supported operation.", operation);
return; //then tough.
}
displayFraction(lhs); //Show the first fraction
printf(" %c ", operation); //Show the operation
displayFraction(rhs); //Show the second fraction
printf(" = "); //Show an equals sign
displayFraction(result); //Show the result
printf("\n"); //End of line
}
/* REPLACE THIS COMMENT WITH YOUR DEFINITIONS */
4/5 * 4/9 = 16/45
1/3 + 5/9 = 8/9
2/5 * 1/6 = 1/15
1/1 + 2/1 = 3/1
4/5 * 1/3 = 4/15
2/5 + 5/4 = 33/20
1/4 * 10/9 = 5/18
7/6 + 8/7 = 97/42
3/8 * 1/4 = 3/32
1/1 + 5/1 = 6/1
8/3 * 4/7 = 32/21
1/1 + 9/2 = 11/2
5/8 * 1/2 = 5/16
9/5 + 9/1 = 54/5
5/7 * 1/4 = 5/28
3/7 + 2/1 = 17/7
1/2 * 8/9 = 4/9
9/4 + 9/2 = 27/4
3/2 * 6/5 = 9/5
4/7 + 3/5 = 41/35
6/5 * 5/1 = 6/1
4/3 + 6/5 = 38/15
Please enter fraction as NUMERATOR/DENOMINATOR: 1/2
Please enter fraction as NUMERATOR/DENOMINATOR: 2/4
1/2 + 1/2 = 1/1
1/2 + 1/2 = 1/1
1/2 * 1/2 = 1/4
1/2 * 1/2 = 1/4
Please enter fraction as NUMERATOR/DENOMINATOR: 2/5
Please enter fraction as NUMERATOR/DENOMINATOR: 5/2
2/5 + 5/2 = 29/10
5/2 + 2/5 = 29/10
2/5 * 5/2 = 1/1
5/2 * 2/5 = 1/1
8/10 * 4/9 = 32/90
1/3 + 5/9 = 24/27
4/10 * 1/6 = 4/60
3/3 + 8/4 = 36/12
8/10 * 1/3 = 8/30
4/10 + 10/8 = 132/80
1/4 * 10/9 = 10/36
7/6 + 8/7 = 97/42
3/8 * 1/4 = 3/32
10/10 + 10/2 = 120/20
8/3 * 4/7 = 32/21
6/6 + 9/2 = 66/12
5/8 * 2/4 = 10/32
9/5 + 9/1 = 54/5
5/7 * 1/4 = 5/28
3/7 + 10/5 = 85/35
2/4 * 8/9 = 16/36
9/4 + 9/2 = 54/8
6/4 * 6/5 = 36/20
4/7 + 6/10 = 82/70
6/5 * 10/2 = 60/10
8/6 + 6/5 = 76/30
Please enter fraction as NUMERATOR/DENOMINATOR: 1/2
Please enter fraction as NUMERATOR/DENOMINATOR: 2/4
1/2 + 2/4 = 8/8
2/4 + 1/2 = 8/8
1/2 * 2/4 = 2/8
2/4 * 1/2 = 2/8
Please enter fraction as NUMERATOR/DENOMINATOR: 2/5
Please enter fraction as NUMERATOR/DENOMINATOR: 5/2
2/5 + 5/2 = 29/10
5/2 + 2/5 = 29/10
2/5 * 5/2 = 10/10
5/2 * 2/5 = 10/10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment