#include using namespace std; struct fraction { int top, bot; }; fraction makefr(const int t, const int b) { fraction r; r.top = t; r.bot = b; return r; } void print(const fraction f) { cout << f.top << "/" << f.bot; } fraction multiply(const fraction f, const fraction g) { return makefr(f.top * g.top, f.bot * g.bot); } fraction divide(const fraction f, const fraction g) { return makefr(f.top * g.bot, f.bot * g.top); } // beware of zeros fraction add(const fraction f, const fraction g) { return makefr(f.top * g.bot + f.bot * g.top, f.bot * g.bot); } fraction subtract(const fraction f, const fraction g) { return makefr(f.top * g.bot - f.bot * g.top, f.bot * g.bot); } bool lessthan(const fraction f, const fraction g) // "less" is already defined in a library { return f.top * g.bot < f.bot * g.top; } // beware of negatives int main() { fraction half = makefr(1, 2); fraction quarter = makefr(1, 4); fraction seven_eighths = makefr(7, 8); fraction x = add(half, quarter); print(half); cout << " * "; print(quarter); cout << " = "; print(x); cout << "\n"; print(x); cout << " < "; print(seven_eighths); cout << " = "; bool y = lessthan(x, seven_eighths); cout << (y ? "true" : "false") << "\n"; }