forked from ccgcv/Cplus-plus-for-hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComplex.cpp
79 lines (69 loc) · 2.71 KB
/
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
#include <iostream>
using namespace std;
struct complex {
double real_part=0; //double to store the real part of the complex number
short isDivision=0; //toggling multiplication and division print statements
double imaginary_part=0; //double to store the imaginary part part of the complex number
void getsetnumbers(){
cout<<"number\n";
cout<<"give real part of number- ";
cin>>real_part;
cout<<"give imaginary part of number- ";
cin>>imaginary_part;
}
double operator +(complex x){ //add complex numbers and print it
double real_part_addition, imaginary_part_addition;
real_part_addition=real_part+x.real_part;
imaginary_part_addition=imaginary_part+x.imaginary_part;
cout<<"Addition is== "<<real_part_addition<<"+"<<imaginary_part_addition<<"i\n";
}
double operator -(complex x){
double real_part_subtraction, imaginary_part_subtraction;
real_part_subtraction=real_part-x.real_part;
imaginary_part_subtraction=imaginary_part-x.imaginary_part;
cout<<"Subtraction is== "<<real_part_subtraction<<"+"<<imaginary_part_subtraction<<"i\n";
}
friend double operator *(complex x, complex y); //create friend operators for operator overloading
friend double operator /(complex x, complex y);
friend double operator |(complex x, complex y);
};
double operator |(complex g, complex k){ //copy two complex numbers
g.real_part=k.real_part;
g.imaginary_part=k.imaginary_part;
g.isDivision=k.isDivision;
}
double operator *(complex o, complex p){
double real_part_multiplication, imaginary_part_multiplication;
real_part_multiplication=p.real_part*o.real_part- p.imaginary_part*o.imaginary_part;
imaginary_part_multiplication=p.imaginary_part*o.real_part+ p.real_part*o.imaginary_part;
if (o.isDivision== 0){ //multiplication case
cout<<"product is== "<<real_part_multiplication<<"+("<<imaginary_part_multiplication<<")i\n";
}
else{ //division case
o.real_part=real_part_multiplication;
o.imaginary_part=imaginary_part_multiplication;
}
}
double operator /(complex h, complex k){ //division
complex g;
k.isDivision=1;
g|k; //make a copy
k.imaginary_part=-k.imaginary_part;
k*h;
g*h;
k.real_part=k.real_part/g.real_part;
k.imaginary_part=k.imaginary_part/g.real_part;
cout<<"division is== "<<k.real_part<<"+("<<k.imaginary_part<<")i\n";
}
int main()
{ double a,b;
complex z;
z.getsetnumbers();
complex y;
y.getsetnumbers();
y+(z);
y-z;
y*(z);
y/z;
return 0;
}