-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathc4_complex_lib.cpp
146 lines (123 loc) · 3.89 KB
/
c4_complex_lib.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
# include <cstdlib>
# include <iostream>
# include <cmath>
using namespace std;
# include "c4_complex_lib.hpp"
/********************************************************
* c = c / c -- c4_complex division *
* *
* Parameters *
* oper1, oper2 -- two operands of the divide *
* *
* Returns *
* result of the divide *
********************************************************/
c4_complex operator / ( const c4_complex &oper1, const c4_complex &oper2 )
{
// Denominator of the result
float den = fabs(oper2.real()) + fabs(oper2.imaginary());
// Real part of the oper1 factor
float oper1_real_den = oper1.real() / den;
// Imaginary part of the oper1 factor
float oper1_imag_den = oper1.imaginary() / den;
// Real part of the oper2 factor
float oper2_real_den = oper2.real() / den;
// Imaginary part of the oper2 factor
float oper2_imag_den = oper2.imaginary() / den;
// Normalization factor
float normalization = oper2_real_den * oper2_real_den +
oper2_imag_den * oper2_imag_den;
return c4_complex((oper1_real_den * oper2_real_den +
oper1_imag_den * oper2_imag_den) / normalization,
(oper1_imag_den * oper2_real_den -
oper1_real_den * oper2_imag_den) / normalization);
}
/********************************************************
* c /= c -- c4_complex divide by *
* *
* Parameters *
* oper2 -- operator to divide by *
* *
* Returns *
* Reference to the result of the divide *
********************************************************/
c4_complex& c4_complex::operator /= (const c4_complex& oper2)
{
// Denominator of the result
float den = fabs(oper2.real()) + fabs(oper2.imaginary());
// Denominator -- operator 1 real part
float oper1_real_den = real_part / den;
// Denominator -- operator 1 imaginary part
float oper1_imag_den = imaginary_part / den;
// Denominator -- operator 2 real part
float oper2_real_den = oper2.real() / den;
// Denominator -- operator 2 imaginary part
float oper2_imag_den = oper2.imaginary() / den;
// Normalization factor
float normalization = oper2_real_den * oper2_real_den +
oper2_imag_den * oper2_imag_den;
real_part = (oper1_real_den * oper2_real_den +
oper1_imag_den * oper2_imag_den) / normalization;
imaginary_part = (oper1_imag_den * oper2_real_den -
oper1_real_den * oper2_imag_den) / normalization;
return (*this);
}
/********************************************************
* istream >> c4_complex -- read a c4_complex *
* *
* Parameters *
* in_file -- file to read *
* number -- place to put the number *
* *
* Returns *
* reference to the input file *
********************************************************/
istream &operator >> ( istream &in_file, c4_complex &number )
{
float real, imaginary; // Real and imaginary part.
char ch; // Random character used to verify input
number.set ( 0.0, 0.0 ); // Initialize the number (just in case)
//
// ERROR: NO MATCHING FUNCTION FOR CALL TO IPFX...
//
// in_file.ipfx(1); // Tell the i/o system we are reading formatted
in_file >> ws; // Skip whitespace
if (in_file.bad())
{
return (in_file);
}
in_file >> ch; // Get character after whitespace
if (ch != '(')
{
// in_file.setf ( ios::failbit ); // We have an error
return (in_file);
}
in_file >> real;
if (in_file.bad())
{
return (in_file);
}
in_file >> ws >> ch; // Get first character after number
if (in_file.bad())
{
return (in_file);
}
if (ch != ',')
{
// in_file.setf(ios::failbit);
return (in_file);
}
in_file >> imaginary;
in_file >> ws >> ch;
if (in_file.bad())
{
return (in_file);
}
if (ch != ')')
{
// in_file.setf(ios::failbit);
return (in_file);
}
number.set(real, imaginary);
return (in_file);
}