Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Cplx
- {
- friend ostream& operator<<(ostream &os, Cplx z);
- friend Cplx operator+(Cplx &a, Cplx &b);
- friend Cplx operator-(Cplx &a, Cplx &b);
- friend Cplx operator*(Cplx &a, Cplx &b);
- private:
- float re, im;
- public:
- Cplx(float x = 0, float y = 0) { re = x; im = y; }
- void Setri(float x, float y) { re = x; im = y; }
- float Getr() { return re; }
- void Seti(float x) { im = x; }
- };
- Cplx operator+(Cplx &a, Cplx &b)
- {
- return Cplx(a.re + b.re, a.im + b.im);
- }
- Cplx operator-(Cplx &a, Cplx &b)
- {
- return Cplx(a.re - b.re, a.im - b.im);
- }
- Cplx operator*(Cplx &a, Cplx &b)
- {
- return Cplx(a.re*b.re - a.im*b.im, a.re*b.im + a.im*b.re);
- }
- Cplx operator/(Cplx &a, Cplx &b)
- {
- return Cplx((a.re*b.re + a.im*b.im)/b.re*b.re+b.im*b.im, (a.im*b.re-a.re*b.im)/b.re*b.re+b.im*b.im);
- }
- ostream& operator<<(ostream &os, Cplx z)
- {
- os << z.re;
- if(z.im >= 0) os << " + ";
- return os << z.im << "i";
- }
- int main()
- {
- Cplx a(5, 6), b(8, 3), c; // deklaracija tri kompleksna broja
- c = a + b;
- cout << c;
- c = a - b;
- cout << c;
- c = a * b;
- cout << c;
- c = a / b;
- cout << c;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement