-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmplx.cpp
162 lines (132 loc) · 2.18 KB
/
cmplx.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <iostream>
#include <string>
#include <sstream>
#include "cmplx.h"
using namespace std;
Complex::Complex(double r, double i)
{
this->re=r;
this->im=i;
}
void Complex::Clear()
{
this->re=0;
this->im=0;
}
void Complex::StrToComp(string inp)
{
string input=inp.c_str();
string temp, re_arr, im_arr;
int x,o,n,pm=0,ro=0,mal=0;
for(x=0; input[x]!='\0'; x++)
{
if((input[x]=='+') || (input[x]=='-')) ro=pm=x;
if(input[x]=='*') mal=x;
}
if(pm==0)
{
temp=input;
re=from_string<double>(temp);
}
else
{
for(n=mal+1, o=0; n<x; n++, o++);
re_arr.resize(pm);
im_arr.resize(o);
pm--;
for(; pm>=0; pm--)
re_arr[pm]=input[pm];
for(; n>mal; --o,--n)
im_arr[o]=input[n];
temp=re_arr;
this->re=from_string<double>(temp);
temp=im_arr;
this->im=from_string<double>(temp);
if(input[ro]=='-')
this->im=this->im*(-1);
}
}
int Complex::CompLen()
{
int len=1;
if(this->im)
{
len+=strlen(to_string(this->im).c_str());
len+=3;
}
if(this->re)
len+=strlen(to_string(this->re).c_str());
else
len+=3;
return(len);
}
double Complex::GetRe()
{
return(this->re);
}
double Complex::GetIm()
{
return(this->im);
}
void Complex::SetRe(double r)
{
this->re=r;
}
void Complex::SetIm(double i)
{
this->im=i;
}
void Complex::StoreComp(Complex C)
{
this->re=C.re;
this->im=C.im;
}
Complex Complex::operator +(Complex C2)
{
Complex Sum;
Sum.SetRe(this->re+C2.GetRe());
Sum.SetIm(this->im+C2.GetIm());
return (Sum);
}
Complex Complex::operator -(Complex C2)
{
Complex Diff;
Diff.SetRe(this->re-C2.GetRe());
Diff.SetIm(this->im-C2.GetIm());
return (Diff);
}
Complex Complex::operator *(Complex C2)
{
Complex Prod;
Prod.re+=this->re*C2.re-this->im*C2.im;
Prod.im+=this->re*C2.im+C2.re*this->im;
return (Prod);
}
Complex Complex::operator =(Complex C2)
{
this->re=C2.re;
this->im=C2.im;
return (*this);
}
Complex Complex::operator +=(Complex C2)
{
this->re=C2.re+this->re;
this->im=C2.im+this->im;
return (*this);
}
ostream &operator <<(ostream &out, Complex &C)
{
if(C.im==0)
out << C.re;
else
{
if(C.im<0)
{
C.im=-C.im;
out << C.re << "-j*" << C.im;
}
else
out << C.re<< "+j*" << C.im;
}
return out;
}