-
Notifications
You must be signed in to change notification settings - Fork 0
/
OOP-1-complex.cpp
96 lines (90 loc) · 2.35 KB
/
OOP-1-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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
using namespace std;
class complex
{
int x, y;
public:
complex()
{
x = 0;
y = 0;
}
friend void operator>>(complex &u, complex &v)
{
cout << "Please enter the real value for first no. (x1) =>" << endl;
cin >> u.x;
cout << "Enter the imaginary value of the first no. (y1)" << endl;
cin >> u.y;
cout << "Please enter the real value for second no. (x2) =>" << endl;
cin >> v.x;
cout << "Enter the imaginary value of the second no. (y2)" << endl;
cin >> v.y;
}
friend void operator<<(complex &u, complex &v)
{
cout << "First complex no is =>" << u.x << "+" << u.y << "i" << endl;
cout << "second complex no is =>" << v.x << "+" << v.y << "i" << endl;
}
friend void operator+(complex &u, complex &v)
{
complex add;
add.x = u.x + v.x;
add.y = u.y + v.y;
if (add.y > 0)
cout << "addition of the given complex nos. is =>" << add.x << "+" << add.y << "i" << endl;
else
cout << "addition of the given complex nos. is =>" << add.x << "-i" << add.y << endl;
}
friend void operator-(complex &u, complex &v)
{
complex sub;
sub.x = u.x - v.x;
sub.y = u.y - v.y;
if (sub.y > 0)
cout << "substraction of the given complex nos. is =>" << sub.x << "+" << sub.y << "i" << endl;
else
cout << "substraction of the given complex nos. is =>" << sub.x << (-1) * sub.y << "-i" << endl;
}
friend void operator*(complex &u, complex &v)
{
complex mul;
mul.x = (u.x * v.x) - (u.y * v.y);
mul.y = (u.x * v.y) + (v.x * u.y);
if (mul.y > 0)
cout << "Multiplication of the given complex nos. is => " << mul.x << "+" << mul.y << "i" << endl;
else
cout << "Multiplication of the given complex nos. is => " << mul.x << (-1) * mul.y << "-i" << endl;
}
};
int main()
{
char ch;
char des;
complex s1, s2;
operator>>(s1, s2);
operator<<(s1, s2);
do
{
cout << "Choose the operation to be performed=> " << endl;
cout << "(+)addition" << endl;
cout << "(-)subtraction" << endl;
cout << "(*)Multiplication" << endl;
cin >> ch;
switch (ch)
{
case '+':
s1 + s2;
break;
case '-':
s1 - s2;
break;
case '*':
s1 *s2;
break;
default:
cout << "Invalid Operation" << endl;
}
cout << "Do you Wish To Perform Operation Again ? => (Y/N)";
cin >> des;
} while (des == 'y' || des == 'Y');
}