#include using namespace std; struct fraction { int top, bot; }; fraction add(fraction A, fraction B) { fraction R; R.top = A.top*B.bot + B.top*A.bot; R.bot = A.bot*B.bot; return R; } fraction sub(fraction A, fraction B) { fraction R; R.top = A.top*B.bot - B.top*A.bot; R.bot = A.bot*B.bot; return R; } fraction mul(fraction A, fraction B) { fraction R; R.top = A.top*B.top; R.bot = A.bot*B.bot; return R; } fraction div(fraction A, fraction B) { fraction R; R.top = A.top*B.bot; R.bot = A.bot*B.top; return R; } void print(fraction X) { if (X.top == 0) cout << "0"; else if (X.bot == 1) cout << X.top; else cout << X.top << "/" << X.bot; } int main() { while (true) { fraction a, b; cout << "Enter A's top and bottom: "; cin >> a.top >> a.bot; cout << "Enter B's top and bottom: "; cin >> b.top >> b.bot; cout << "A + B = "; print(add(a, b)); cout << "\n"; cout << "A - B = "; print(sub(a, b)); cout << "\n"; cout << "A * B = "; print(mul(a, b)); cout << "\n"; cout << "A / B = "; print(div(a, b)); cout << "\n"; } }