-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13.16_Complex.cpp
More file actions
71 lines (59 loc) · 1.59 KB
/
13.16_Complex.cpp
File metadata and controls
71 lines (59 loc) · 1.59 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
// 12191706 ±èÁ¤Áø
// Chapter.13_13.16
// Overloaded stream insertion and stream extraction operators
// for class Complex.
#include <iomanip>
#include "Complex.h"
using namespace std;
// Overloaded stream insertion operaotr; cannot be a member function
// if we would like to invoke it with cout << somecomplex;
ostream& operator<< (ostream& output, const Complex& complex) {
output << "Real: " << complex.real
<< "\nImaginary: " << complex.imaginary << "i"
<< "\nComplex: " << complex.real << " + " << complex.imaginary << "i\n";
return output; // enables cout << a << b << c;
}
// Overloaded stream extraction operator; cannot be a member function
// if we would like to invoke it with cin >> somePhoneNumber;
istream& operator>>(istream& input, Complex& complex)
{
int number;
int multiplier;
char temp; // temporary variable used to store input
input >> number; // get input
if (input.peek() == ' ') {
complex.real = number;
input >> temp;
multiplier = (temp == '+') ? 1 : -1;
if (input.peek() != ' ')
input.clear(ios::failbit);
else {
if (input.peek() == ' ') {
input >> number;
complex.imaginary = number;
complex.imaginary *= multiplier;
input >> temp;
if (temp != 'i')
input.clear(ios::failbit);
}
else
input.clear(ios::failbit);
}
}
else if (input.peek() == 'i') {
input >> temp;
if (temp == 'i') {
complex.real = 0;
complex.imaginary = number;
}
else
input.clear(ios::failbit);
}
else if (input.peek() == '\n') {
complex.real = number;
complex.imaginary = 0;
}
else
input.clear(ios::failbit);
return input;
}