#include "library.h" struct complex { double real, imag; }; complex make_complex(double r, double im) { complex x; x.real = r; x.imag = im; return x; } void print(complex x) { if (x.imag >= 0) cout << x.real << " + " << x.imag << "i"; else cout << x.real << " - " << -x.imag << "i"; } complex read_complex() { complex x; cout << "Enter the real part: "; cin >> x.real; cout << "Enter imaginary part: "; cin >> x.imag; return x; } complex add(complex a, complex b) { return make_complex(a.real + b.real, a.imag + b.imag); } complex sub(complex a, complex b) { return make_complex(a.real - b.real, a.imag - b.imag); } complex mul(complex a, complex b) { return make_complex(a.real * b.real - a.imag * b.imag, a.real * b.imag + a.imag * b.real); } complex div(complex a, complex b) { double bot = b.real*b.real + b.imag*b.imag; double topreal = a.real*b.real + a.imag*b.imag; double topimag = a.imag*b.real - a.real*b.imag; return make_complex(topreal/bot, topimag/bot); } const double minreal = -2, maxreal = 2, minimag = -2, maximag = 2; const double maxx = 600, maxy = 600; double real_to_x(double r) { return (r - minreal) / (maxreal - minreal) * maxx; } double imag_to_y(double i) { return maxy - (i - minimag) / (maximag - minimag) * maxy; } void plot_complex(complex x) { double sx = real_to_x(x.real); double sy = imag_to_y(x.imag); move_to(sx-10, sy-10); draw_to(sx+10, sy+10); move_to(sx-10, sy+10); draw_to(sx+10, sy-10); } double x_to_real(double x) { return x/maxx * (maxreal - minreal) + minreal; } double y_to_imag(double y) { y = maxy - y; return y/maxy * (maximag - minimag) + minimag; } complex read_click() { wait_for_mouse_click(); double sx = get_click_x(), sy = get_click_y(); return make_complex(x_to_real(sx), y_to_imag(sy)); } void move_to(complex x) { double sx = real_to_x(x.real); double sy = imag_to_y(x.imag); move_to(sx, sy); } void draw_to(complex x) { double sx = real_to_x(x.real); double sy = imag_to_y(x.imag); draw_to(sx, sy); } void draw_axes() { double yzero = imag_to_y(0); move_to(0.0, yzero); draw_to(maxx, yzero); double xzero = real_to_x(0); move_to(xzero, 0.0); draw_to(xzero, maxy); } void main() { make_window(maxx, maxy); draw_axes(); while (true) { cout << "Enter C:\n"; complex C = read_click(); complex Z = make_complex(0.0, 0.0); move_to(C); for (int i = 0; i < 100; i += 1) { Z = add(mul(Z, Z), C); draw_to(Z); } } } /* void main() { cout << "Enter A:\n"; complex a = read_complex(); cout << "Enter B:\n"; complex b = read_complex(); complex plus = add(a, b); complex min = sub(a, b); complex tim = mul(a, b); complex divi = div(a, b); cout << "A+B = "; print(plus); cout << "\n"; cout << "A-B = "; print(min); cout << "\n"; cout << "A*B = "; print(tim); cout << "\n"; cout << "A/B = "; print(divi); cout << "\n"; } */