-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex.cpp
More file actions
89 lines (81 loc) · 2.11 KB
/
Copy pathcomplex.cpp
File metadata and controls
89 lines (81 loc) · 2.11 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
using namespace std;
class complex
{
int real;
int img;
public:
complex()
{
real=0;
img=0;
}
friend complex operator+(complex&, complex&);
friend complex operator-(complex&, complex&);
friend complex operator*(complex&, complex&);
friend void operator/(complex&, complex&);
friend istream& operator>>(istream&, complex&);
friend ostream& operator<<(ostream&, complex&);
};
int main()
{
complex c, c1, c2;
cin>>c1;
cout<<c1<<endl;
cin>>c2;
cout<<c2<<endl;
c = c1 + c2;
cout<<"addition of two complex numbers: "<<endl<<c<<endl;
c = c1 - c2;
cout<<"subtraction of two complex numbers: "<<endl<<c<<endl;
c = c1 * c2;
cout<<"multiplication of two complex number: "<<endl<<c<<endl;
cout<<"division of two complex numbers: ";
c1/c2;
return 0;
}
istream& operator>>(istream& din, complex& c)
{
cout<<"enter real part: ";
din>>c.real;
cout<<"enter imaginary part: ";
din>>c.img;
cout<<"the complex number you have entered: "<<c.real<<" + i("<<c.img<<")"<<endl;
return din;
}
ostream& operator<<(ostream& dout, complex& c)
{
dout<<"real part: "<<c.real<<endl;
dout<<"imaginary part: "<<c.img<<endl;
dout<<"complex number: "<<c.real<<" + i("<<c.img<<")"<<endl;
return dout;
}
complex operator+(complex& c1, complex& c2)
{
complex ans;
ans.real = c1.real + c2.real;
ans.img = c1.img + c2.img;
return ans;
}
complex operator-(complex& c1, complex& c2)
{
complex ans;
ans.real = c1.real - c2.real;
ans.img = c1.img - c2.img;
return ans;
}
complex operator*(complex& c1, complex& c2)
{
complex ans;
ans.real = (c1.real * c2.real) - (c1.img * c2.img);
ans.img = (c1.real * c2.img) + (c1.img * c2.real);
return ans;
}
void operator/(complex& c1, complex& c2)
{
int a, b, c;
a = (c1.real * c2.real) + (c1.img * c2.img);
b = (c1.img * c2.real) - (c1.real * c2.img);
c = (c2.real * c2.real) + (c2.img * c2.img);
cout<<"("<<a<<"/"<<c<<") + i("<<b<<"/"<<c<<")"<<endl;
}