forked from vishnuparikh/vmp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4 ComplexNumbers.cpp
More file actions
141 lines (110 loc) · 2.55 KB
/
Copy path4 ComplexNumbers.cpp
File metadata and controls
141 lines (110 loc) · 2.55 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
NINAD DESHPANDE
Roll No: 10
SE Computer II
Batch: S1
Question:
Implement a class Complex which represents a Complex number data type,
Implement the following operations:
1. Constructor(including a default constructor which creates a 0+0i)
2. Overloaded operator + to add two complex numbers.
3. overloaded operator * to multiply two complex numbers.
4. Overloaded << and >> to print and read complex numbers.
*/
#include<iostream>
using namespace std;
class complex
{
float a,b;
public:
complex()
{
a=0.00;b=0.00;
};
complex operator+(complex ob)
{
complex temp;
temp.a=a+ob.a;
temp.b=b+ob.b;
return temp;
};
complex operator-(complex ob)
{
complex temp;
temp.a=a-ob.a;
temp.b=b-ob.b;
return temp;
};
complex operator*(complex ob)
{
complex temp;
temp.a= (a*ob.a)-(b*ob.b);
temp.b= (a*ob.b)+(b*ob.a);
return temp;
};
complex operator/(complex ob)
{
complex temp;
temp.a= (a*ob.a)-(b*(-ob.b));
temp.b= (a*(-ob.b))+(b*ob.a);
temp.a=temp.a/((ob.a*ob.a)+(ob.b*ob.b));
temp.b=temp.b/((ob.a*ob.a)+(ob.b*ob.b));
return temp;
};
friend ostream &operator<<(ostream &out, complex &c);
friend istream &operator>>(istream &in, complex &c);
};
ostream &operator<<(ostream &out, complex &c)
{
out<<c.a<<" + i("<<c.b<<")";
return out;
}
istream &operator>>(istream &in, complex &c)
{
in>>c.a>>c.b;
return in;
}
int main()
{
char out,cont;
out='N'; cont='N';
int op;
complex c1,c2,c3;
do
{
cout<<"Enter real and imaginary parts of first complex number:\n";
cin>>c1;
cout<<"Enter real and imaginary parts of second complex number:\n";
cin>>c2;
do
{
cout<<"\nSelect operation to perform:\n";
cout<<"1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n";
cout<<"Operation Code: ";
cin>>op;
switch(op)
{
case 1:
c3= c1+c2;
cout<<c3<<endl;
break;
case 2:
c3= c1-c2;
cout<<c3<<endl;
break;
case 3:
c3= c1*c2;
cout<<c3<<endl;
break;
case 4:
c3=c1/c2;
cout<<c3<<endl;
break;
}
cout<<"Select another operation? (Y/N)\n";
cin>>cont;
}while(cont=='Y' || cont=='y');
cout<<"Input different complex numbers? (Y/N)\n";
cin>>out;
}while(out=='Y' || out=='y');
}