-
Notifications
You must be signed in to change notification settings - Fork 0
/
COMPLEX NO.
127 lines (117 loc) · 2.42 KB
/
COMPLEX NO.
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include<iostream>
using namespace std;
class complex
{
float a,b;
public : complex()
{
a=0;
b=0;
}
void display();
complex(float real,float img)
{
a=real;
b=img;
}
friend istream & operator >>(istream &din,complex &c1);
friend ostream & operator <<(ostream &dout,complex &c1);
complex operator+(complex);
complex operator-(complex);
complex operator*(complex);
complex operator/(complex);
};
istream & operator >>(istream &din,complex &c) ////input overloading
{
din>>c.a;
din>>c.b;
return din;
}
ostream & operator <<(ostream &dout,complex &c1) ////output overloading
{
dout<<c1.a;
dout<<c1.b;
return dout;
}
complex complex::operator+(complex x) ////addition overloading
{
complex temp;
temp.a=a+x.a;
temp.b=b+x.b;
return temp;
}
complex complex::operator-(complex x) ////subtraction overloading
{
complex temp;
temp.a=a-x.a;
temp.b=b-x.b;
return temp;
}
complex complex::operator*(complex x) ////multiplication overloading
{
complex temp;
temp.a=((a*x.a)-(b*x.b));
temp.b=((a*x.b)+(b*x.a));;
return temp;
}
complex complex::operator/(complex x) ////division overloading
{
complex temp;
temp.a=((a*x.a)-(b*x.b))/((x.a*x.a)-(x.b*x.b));
temp.b=((a*x.b)+(b*x.a))/((x.a*x.a)-(x.b*x.b));
return temp;
}
void complex::display() //////display
{
cout<<a<<"+"<<b<<"i";
}
int main()
{
complex c1,c2,c3;
int c;
char x;
do
{
cout<<"\n\t MENU: "; /////menu
cout<<"\n\t1. Addition: ";
cout<<"\n\t2. Subtraction: ";
cout<<"\n\t3. Multiplication: ";
cout<<"\n\t4. Division: ";
cout<<"\n\t Enter your choice ";
cin>>c;
cout<<"enter first complex no ";
cin>>c1;
cout<<"enter second complex no ";
cin>>c2;
cout<<"\nFirst complex no= ";
c1.display();
cout<<"\nSecond complex no= ";
c2.display();
switch(c)
{
case 1:
c3=c1+c2;
cout<<"\nAddition = ";
c3.display();
break;
case 2:
c3=c1-c2;
cout<<"\nSubtraction = ";
c3.display();
break;
case 3:
c3=c1*c2;
cout<<"\nMultiplication = ";
c3.display();
break;
case 4:
c3=c1/c2;
cout<<"\nDivision = ";
c3.display();
break;
}
cout<<"\ndo you want to continue?(y/n)";
cin>>x;
}
while(x=='y'||x=='Y');
}