adjusting format

This commit is contained in:
Jidong Xiao
2023-10-15 21:10:03 -04:00
parent ccdd6794fd
commit ae1af2e2a8

View File

@@ -4,37 +4,31 @@
## 14.1 Complex Numbers — A Brief Review ## 14.1 Complex Numbers — A Brief Review
- Complex numbers take the form z = a + bi, where i = - Complex numbers take the form z = a + bi, where i = √1 and a and b are real. a is called the real part, b is called the imaginary part.
1 and a and b are real. a is called the real part, b is
called the imaginary part.
- If w = c + di, then - If w = c + di, then
w + z = (a + c) + (b + d)i, w + z = (a + c) + (b + d)i,
w z = (a c) + (b d)i, and w z = (a c) + (b d)i, and
w × z = (ac bd) + (ad + bc)i w × z = (ac bd) + (ad + bc)i
- The magnitude of a complex number is - The magnitude of a complex number is &radic;a<sup>2</sup> + b<sup>2</sup>;
a
2 + b
2
## 14.2 Complex Class declaration (complex.h) ## 14.2 Complex Class declaration (complex.h)
```cpp ```cpp
class Complex { class Complex {
public: public:
Complex(double x=0, double y=0) : real_(x), imag_(y) {} // default constructor Complex(double x=0, double y=0) : real_(x), imag_(y) {} // default constructor
Complex(Complex const& old) : real_(old.real_), imag_(old.imag_) {} // copy constructor Complex(Complex const& old) : real_(old.real_), imag_(old.imag_) {} // copy constructor
Complex& operator= (Complex const& rhs); // Assignment operator Complex& operator= (Complex const& rhs); // Assignment operator
double Real() const { return real_; } double Real() const { return real_; }
void SetReal(double x) { real_ = x; } void SetReal(double x) { real_ = x; }
double Imaginary() const { return imag_; } double Imaginary() const { return imag_; }
void SetImaginary(double y) { imag_ = y; } void SetImaginary(double y) { imag_ = y; }
double Magnitude() const { return sqrt(real_*real_ + imag_*imag_); } double Magnitude() const { return sqrt(real_*real_ + imag_*imag_); }
Complex operator+ (Complex const& rhs) const; Complex operator+ (Complex const& rhs) const;
Complex operator- () const; // unary operator- negates a complex number Complex operator- () const; // unary operator- negates a complex number
friend istream& operator>> (istream& istr, Complex& c); friend istream& operator>> (istream& istr, Complex& c);
private: private:
double real_, imag_; double real_, imag_;
}; };
Complex operator- (Complex const& left, Complex const& right); // non-member function Complex operator- (Complex const& left, Complex const& right); // non-member function
ostream& operator<< (ostream& ostr, Complex const& c); // non-member function ostream& operator<< (ostream& ostr, Complex const& c); // non-member function
@@ -45,34 +39,34 @@ ostream& operator<< (ostream& ostr, Complex const& c); // non-member function
```cpp ```cpp
// Assignment operator // Assignment operator
Complex& Complex::operator= (Complex const& rhs) { Complex& Complex::operator= (Complex const& rhs) {
real_ = rhs.real_; real_ = rhs.real_;
imag_ = rhs.imag_; imag_ = rhs.imag_;
return *this; return *this;
} }
// Addition operator as a member function. // Addition operator as a member function.
Complex Complex::operator+ (Complex const& rhs) const { Complex Complex::operator+ (Complex const& rhs) const {
double re = real_ + rhs.real_; double re = real_ + rhs.real_;
double im = imag_ + rhs.imag_; double im = imag_ + rhs.imag_;
return Complex(re, im); return Complex(re, im);
} }
// Subtraction operator as a non-member function. // Subtraction operator as a non-member function.
Complex operator- (Complex const& lhs, Complex const& rhs) { Complex operator- (Complex const& lhs, Complex const& rhs) {
return Complex(lhs.Real()-rhs.Real(), lhs.Imaginary()-rhs.Imaginary()); return Complex(lhs.Real()-rhs.Real(), lhs.Imaginary()-rhs.Imaginary());
} }
// Unary negation operator. Note that there are no arguments. // Unary negation operator. Note that there are no arguments.
Complex Complex::operator- () const { Complex Complex::operator- () const {
return Complex(-real_, -imag_); return Complex(-real_, -imag_);
} }
// Input stream operator as a friend function // Input stream operator as a friend function
istream& operator>> (istream & istr, Complex & c) { istream& operator>> (istream & istr, Complex & c) {
istr >> c.real_ >> c.imag_; istr >> c.real_ >> c.imag_;
return istr; return istr;
} }
// Output stream operator as an ordinary non-member function // Output stream operator as an ordinary non-member function
ostream& operator<< (ostream & ostr, Complex const& c) { ostream& operator<< (ostream & ostr, Complex const& c) {
if (c.Imaginary() < 0) ostr << c.Real() << " - " << -c.Imaginary() << " i "; if (c.Imaginary() < 0) ostr << c.Real() << " - " << -c.Imaginary() << " i ";
else ostr << c.Real() << " + " << c.Imaginary() << " i "; else ostr << c.Real() << " + " << c.Imaginary() << " i ";
return ostr; return ostr;
} }
``` ```
@@ -140,8 +134,8 @@ area of the declaration of Foo.
```cpp ```cpp
class Foo { class Foo {
public: public:
friend class Bar; friend class Bar;
... ...
}; };
``` ```
This allows member functions in class Bar to access all of the private member functions and variables of a Foo This allows member functions in class Bar to access all of the private member functions and variables of a Foo