// Fraction structure, using typedef #include typedef struct fraction { int num; int denom; } Fraction; // new type name // some useful functions void PrintFraction(Fraction f); // pass by value void InputFraction(Fraction * fptr); // pass by address Fraction AddByValue(Fraction f1, Fraction f2); Fraction AddByAddress(const Fraction* p1, const Fraction* p2); // Test routine (main) int main() { Fraction f1 = {0,1}, f2 = {3,4}, f3 = {1,2}; Fraction r1, r2; printf("f1 = "); PrintFraction(f1); printf("\tf2 = "); PrintFraction(f2); printf("\tf3 = "); PrintFraction(f3); // Test out the InputFraction funtion printf("\n\nInput new fraction for f1: "); InputFraction(&f1); printf("\nInput new fraction for f2: "); InputFraction(&f2); printf("\nInput new fraction for f3: "); InputFraction(&f3); printf("\nNew values:\n"); printf("\nf1 = "); PrintFraction(f1); printf("\tf2 = "); PrintFraction(f2); printf("\tf3 = "); PrintFraction(f3); // Test out the add functions r1 = AddByValue(f1, f2); r2 = AddByAddress(&f1, &f3); printf("\n\nf1 + f2 = "); PrintFraction(r1); printf("\nf1 + f2 = "); PrintFraction(r2); printf("\nf2 + f3 = "); PrintFraction(AddByValue(f2, f3)); printf("\n"); return 0; } void PrintFraction(Fraction f) { printf("%d/%d", f.num, f.denom); } void InputFraction(Fraction * fptr) { scanf("%d/%d", &(fptr->num), &(fptr->denom)); } Fraction AddByValue(Fraction f1, Fraction f2) // pass by value. f1 and f2 are copies of the incoming data { Fraction r; // to store the result r.num = f1.num * f2.denom + f2.num * f1.denom; r.denom = f1.denom * f2.denom; return r; // returned by value } Fraction AddByAddress(const Fraction* p1, const Fraction* p2) { Fraction r; r.num = p1->num * p2->denom + p2->num * p1->denom; r.denom = p1->denom * p2->denom; return r; // returned by value }