-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathcomplesNumber.cpp
101 lines (76 loc) · 1.66 KB
/
complesNumber.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
#include <bits/stdc++.h>
using namespace std;
class complexNumber{
private :
int real;
int imaginary;
public:
complexNumber()
{
real=0;
imaginary=0;
}
complexNumber(int r,int i)
{
real=r;
imaginary=i;
}
void getComplexNumber()
{
cout<<"Enter Real Number";
cin>>real;
cout<<endl<<"Enter Imaginary Number";
cin>>imaginary;
}
void showComplexNumber()
{
cout<<endl<<"Real Part "<<real;
cout<<endl<<"Imaginary Part "<<imaginary;
}
complexNumber operator +(complexNumber)const;
void operator *=(complexNumber);
operator float()const;
};
complexNumber :: operator float()const
{
float finalFloatValue=0;
finalFloatValue+=real;
int temp=imaginary;
int count=0;
while(temp>0)
{
temp=temp/10;
count++;
}
finalFloatValue+=static_cast<float>(imaginary/pow(10,count));
return finalFloatValue;
}
void complexNumber :: operator *=(complexNumber c2)
{
int temp=real;
real=real*c2.real-imaginary*c2.imaginary;
imaginary=temp*c2.imaginary+imaginary*c2.real;
// complexNumber(r,i);
}
complexNumber complexNumber::operator+(complexNumber c2)const
{
int r=real+c2.real;
int i=imaginary+c2.imaginary;
return complexNumber(r,i);
}
int main()
{
complexNumber c1,c3,c4;
c1.getComplexNumber();
complexNumber c2(10,5);
c4=c1+c2;
float x;
c1.showComplexNumber();
c2.showComplexNumber();
c4.showComplexNumber();//sum
c1*=c2;
c1.showComplexNumber();//multiplication
x=c2;//typecasting
cout<<endl<<x;
return 0;
}