#include #include struct complex { double real, imag; complex(); complex(double r, double i); void print(); double magnitude(); void add(const complex & x); void sub(const complex & x); void mul(const complex & x); void div(const complex & x); }; struct fraction { int top, bot; fraction(); fraction(int t, int b); void print(); double magnitude(); void normalise(); void add(const fraction & x); void sub(const fraction & x); void mul(const fraction & x); void div(const fraction & x); }; void main() { fraction a; fraction b(3, 7); fraction c(2, 9); fraction d(7, 8); b.print(); cout << " * "; c.print(); cout << " + "; d.print(); cout << " = "; b.mul(c); b.add(d); b.print(); cout << "\n"; b.print(); cout << " + "; d.print(); cout << " = "; b.add(d); b.print(); cout << ", magnitude = " << b.magnitude() << "\n"; complex x(1, 3); complex y(2, -1); complex z(1, 1); x.print(); cout << " * "; y.print(); cout << " + "; z.print(); cout << " = "; x.mul(y); x.add(z); x.print(); cout << "\n"; x.print(); cout << " + "; z.print(); cout << " = "; x.add(z); x.print(); cout << ", magnitude = " << x.magnitude() << "\n"; } complex::complex() { real = 0.0; imag = 0.0; } complex::complex(double r, double i) { real = r; imag = i; } void complex::print() { cout << real; if (imag >= 0.0) cout << "+"; cout << imag << "i"; } double complex::magnitude() { return sqrt(real*real + imag*imag); } void complex::add(const complex & x) { real = real + x.real; imag = imag + x.imag; } void complex::sub(const complex & x) { real = real - x.real; imag = imag - x.imag; } void complex::mul(const complex & x) { real = real * x.real - imag * x.imag; imag = real * x.imag + imag * x.real; } void complex::div(const complex & x) { double d = x.real * x.real + x.imag * x.imag; real = (real * x.real + imag * x.imag) / d; imag = (imag * x.real - real * x.imag) / d; } fraction::fraction() { top = 0; bot = 1; } fraction::fraction(int t, int b) { top = t; bot = b; } void fraction::print() { cout << top << "/" << bot; } double fraction::magnitude() { return (double)top / (double)bot; } void fraction::normalise() { int gcd = top, b = bot; while (b != 0) { int t = gcd % b; gcd = b; b = t; } top /= gcd; bot /= gcd; } void fraction::add(const fraction & x) { top = top * x.bot + bot * x.top; bot = bot * x.bot; normalise(); } void fraction::sub(const fraction & x) { top = top * x.bot - bot * x.top; bot = bot * x.bot; normalise(); } void fraction::mul(const fraction & x) { top = top * x.top; bot = bot * x.bot; normalise(); } void fraction::div(const fraction & x) { top = top * x.bot; bot = bot * x.top; normalise(); }