-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.cpp
More file actions
85 lines (72 loc) · 2.12 KB
/
class.cpp
File metadata and controls
85 lines (72 loc) · 2.12 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
/*
Ramamurthy Sundar
class.cpp
This file is the implementation of the complex class.
The first 6 functions are operator overloading functions.
The next 5 functions will be important for being able to
work witth the complex number class.
*/
#include "class.h"
//friend functions
std::ostream& operator<<(std::ostream& out, const Complex& theComplex) {
out << theComplex.real << " + " << theComplex.imag << 'i';
return out;
}
std::istream& operator>>(std::istream& in, Complex& theComplex) {
// read obj from stream
in >> theComplex.real >> theComplex.imag;
return in;
}
Complex operator+(const Complex& lhs, const Complex& rhs) {
Complex c;
c.real = (lhs.real + rhs.real);
c.imag = (lhs.imag + rhs.imag);
return c;
}
Complex operator-(const Complex& lhs, const Complex& rhs) {
Complex c;
c.real = (lhs.real - rhs.real);
c.imag = (lhs.imag - rhs.imag);
return c;
}
Complex operator*(const Complex& lhs, const Complex& rhs) {
Complex c;
c.real = (lhs.real*rhs.real - lhs.imag*rhs.imag);
c.imag = (lhs.imag - rhs.imag);
return c;
}
Complex operator/(const Complex& lhs, const Complex& rhs){
Complex c;
c.real = (lhs.real*rhs.real + lhs.imag*rhs.imag) / (rhs.real*rhs.real + rhs.imag*rhs.imag);
c.imag = (lhs.imag*rhs.real - lhs.real*rhs.imag) / (rhs.real*rhs.real + rhs.imag*rhs.imag);
return c;
}
//nonfriend functions
//returns real
double Complex::getReal(void) const {
return real;
}
//returns imaginary
double Complex::getImaginary(void) const {
return imag;
}
// sets the real part
void Complex::setReal(double re) {
real = re;
}
// sets the imaginary part
void Complex::setImaginary(double im) {
imag = im;
}
void Complex::convertStringToComplex(const std::string & complexString){
int sz = complexString.size();
real = atof(complexString.c_str());
std::string imag_substring;
for (int i = 0; i < sz; i++) {
//parsers with selection
if (complexString.at(i) == '+') {
imag_substring.assign(complexString, 4, 3); //.assign(string str, int index, int length) from string library
imag = atof(imag_substring.c_str());
}
}
}