-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComplex.cpp
68 lines (53 loc) · 1.47 KB
/
Complex.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
using namespace std;
class complex {
float realp, imagp;
public:
complex() {
realp = 0;
imagp = 0;
}
complex(float x, float y) {
realp = x;
imagp = y;
}
complex operator+(complex &);
complex operator*(complex &);
friend istream &operator>>(istream &, complex &);
friend ostream &operator<<(ostream &, const complex &);
};
istream &operator>>(istream &din, complex &c) {
cout << "Enter real part of complex number: ";
din >> c.realp;
cout << "Enter imaginary part of complex number: ";
din >> c.imagp;
return din;
}
ostream &operator<<(ostream &dout, const complex &c) {
dout << c.realp << " + " << c.imagp << "i";
return dout;
}
complex complex::operator+(complex &c) {
complex temp;
temp.realp = realp + c.realp;
temp.imagp = imagp + c.imagp;
return temp;
}
complex complex::operator*(complex &c) {
complex mul;
mul.realp = (realp * c.realp) - (imagp * c.imagp);
mul.imagp = (imagp * c.realp) + (realp * c.imagp);
return mul;
}
int main() {
complex c1, c2, c3;
cout << "Enter complex number 1" << endl;
cin >> c1;
cout << "Enter complex number 2" << endl;
cin >> c2;
c3 = c1 + c2;
cout << "Addition of two complex numbers is: " << c3 << endl;
c3 = c1 * c2;
cout << "Multiplication of two complex numbers is: " << c3 << endl;
return 0;
}