Question
/* Homework Lab 30 Using a structure Lab Exercise using struct type as function arguments and return values. Demonstrate how to use the member selector
/* Homework Lab 30 Using a structure
Lab Exercise using struct type as function arguments and return values. Demonstrate how to use the member selector (dot) operator to access structure members.
TODO: Complete the function definitions so that they calculate and return the correct results.
Four-function calculator for fractions
Comments in function definitions refer to identities of the form:
a c a*d + b*c (numerator) f1 + f2 = --- + --- = --------- b d b*d (denominator) */
#include
//------------------------------------------------------------------------------
struct Fraction // Fraction structure { long num; long den; };
//------------------------------------------------------------------------------
Fraction fadd(Fraction f1, Fraction f2); // declarations Fraction fsub(Fraction f1, Fraction f2); Fraction fmul(Fraction f1, Fraction f2); Fraction fdiv(Fraction f1, Fraction f2);
//------------------------------------------------------------------------------
int main() { Fraction f1, f2, f3; // some "fractions" char dummy, op;
do { cout << " Enter fraction, operator, fraction"; cout << " form 3/4+3/8 (or 0/1+0/1 to exit): ";
cin >> f1.num >> dummy >> f1.den; // get fraction f1 cin >> op; cin >> f2.num >> dummy >> f2.den; // get fraction f2
switch (op) // select operation { case '+': f3 = fadd(f1, f2); break; case '-': f3 = fsub(f1, f2); break; case '*': f3 = fmul(f1, f2); break; case '/': f3 = fdiv(f1, f2); break; default: cout << "No such operator"; } // display fraction f3
cout << "Answer = " << f3.num << '/' << f3.den;
} while (!(f1.num == 0 && f2.num == 0)); // until nums = zero
cout << endl << endl;
return 0; }
//------------------------------------------------------------------------------
Fraction fadd(Fraction f1, Fraction f2) // add: f1 + f2 { // (a*d+b*c) / (b*d) Fraction f;
// TODO (numerator): a * d + b * c
// TODO (denominator): b * d
return f; }
//------------------------------------------------------------------------------
Fraction fsub(Fraction f1, Fraction f2) // subtract: f1 - f2 { // (a*d-b*c) / (b*d) Fraction f;
// TODO (numerator): a * d - b * c
// TODO (denominator): b * d
return f; }
//------------------------------------------------------------------------------
Fraction fmul(Fraction f1, Fraction f2) // multiply: f1 * f2 { // (a*c) / (b*d) Fraction f;
// TODO (numerator): a * c
// TODO (denominator): b * d
return f; }
//------------------------------------------------------------------------------
Fraction fdiv(Fraction f1, Fraction f2) // divide: f1 / f2 { // (a*d) / (b*c) Fraction f;
// TODO (numerator): a * d
// TODO (denominator): b * c
return f; }
//------------------------------------------------------------------------------
/* Sample program output:
Enter fraction, operator, fraction form 3/4+3/8 (or 0/1+0/1 to exit): 1/2+1/2 Answer = 4/4
Enter fraction, operator, fraction form 3/4+3/8 (or 0/1+0/1 to exit): 1/2-1/2 Answer = 0/4
Enter fraction, operator, fraction form 3/4+3/8 (or 0/1+0/1 to exit): 1/2*1/2 Answer = 1/4
Enter fraction, operator, fraction form 3/4+3/8 (or 0/1+0/1 to exit): 1/2/1/2 Answer = 2/2
Enter fraction, operator, fraction form 3/4+3/8 (or 0/1+0/1 to exit): 0/1+0/1 Answer = 0/1
Press any key to continue . . . */
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started