-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComplexOverloading.cpp
105 lines (95 loc) · 2.54 KB
/
ComplexOverloading.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
97
98
99
100
101
102
103
104
105
#include<iostream>
using namespace std;
class Overload {
float rno, ino;
public:
Overload() {
rno = 0;
ino = 0;
}
Overload(float a, float b) {
rno = a;
ino = b;
}
Overload operator+(Overload c1) {
Overload temp;
temp.rno = rno + c1.rno;
temp.ino = ino + c1.ino;
return temp;
}
Overload operator-(Overload c1) {
Overload temp;
temp.rno = rno - c1.rno;
temp.ino = ino - c1.ino;
return temp;
}
Overload operator*(Overload c1) {
Overload temp;
temp.rno = (rno * c1.rno) - (ino * c1.ino);
temp.ino = (ino * c1.rno) + (rno * c1.ino);
return temp;
}
Overload operator/(Overload c1) {
Overload temp, c2;
c2.ino = -c1.ino;
float x;
temp.rno = (rno * c1.rno) - (ino * (c2.ino));
temp.ino = (rno * c1.rno) + (rno * (c2.ino));
x = (c1.rno) * (c1.rno) + (c1.ino) * (c1.ino);
temp.rno = temp.rno / x;
temp.ino = temp.ino / x;
return temp;
}
friend ostream &operator<<(ostream &out, Overload &c) {
out << c.rno << " + " << c.ino << "i";
return out;
}
friend istream &operator>>(istream &in, Overload &c) {
in >> c.rno >> c.ino;
return in;
}
};
int main() {
Overload c1, c2, c3;
int choice;
x:
cout << "\n==========================MENU==========================";
cout
<< "\n1.Addition of complex number\n2.Subtraction of complex number\n3.Multiplication of complex number\n4.Division of complex number";
cout << "\n\tEnter your choice: ";
cin >> choice;
cout << "Enter real and imaginary part of first complex number: \n";
cin >> c1;
cout << "Enter real and imaginary part of second complex number: \n";
cin >> c2;
switch (choice) {
case 1:
c3 = c1 + c2;
cout << "\nAddition is: ";
cout << c3;
break;
case 2:
c3 = c1 - c2;
cout << "\nSubtraction is: ";
cout << c3;
break;
case 3:
c3 = c1 * c2;
cout << "\nMultiplication is: ";
cout << c3;
break;
case 4:
c3 = c1 / c2;
cout << "\nDivision is: ";
cout << c3;
break;
default:
cout << "\nWrong choice.";
}
char ch;
cout << "\nDo you want to continue? (y/n): ";
cin >> ch;
if (ch == 'Y' || ch == 'y')
goto x;
return 0;
}