Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- struct Complex
- {
- Complex() {}
- explicit Complex(const double real);
- Complex(const double real, const double imaginary);
- bool operator==(const Complex& rhs) const { return (re == rhs.re) && (im == rhs.im); }
- bool operator!=(const Complex& rhs) const { return !operator==(rhs); }
- Complex operator+=(const Complex& rhs);
- Complex operator+=(const double rhs) { return operator+=(Complex(rhs)); }
- Complex operator-=(const Complex& rhs);
- Complex operator-=(const double rhs) { return operator-=(Complex(rhs)); }
- Complex operator*=(const Complex& rhs);
- Complex operator*=(const double rhs);
- std::ostream& writeTo(std::ostream& ostrm) const;
- std::istream& readFrom(std::istream& istrm);
- double re{0.0};
- double im{0.0};
- static const char leftBrace{'{'};
- static const char separator{','};
- static const char rightBrace{'}'};
- };
- Complex operator+(const Complex& lhs, const Complex& rhs);
- Complex operator-(const Complex& lhs, const Complex& rhs);
- inline std::ostream& operator<<(std::ostream& ostrm, const Complex& rhs)
- {
- return rhs.writeTo(ostrm);
- }
- inline std::istream operator>>(std::istream istrm, const Complex& rhs)
- {
- return rhs.readFrom(istrm);
- }
- bool testParse(const std::string& str)
- {
- using namespace std;
- istringstream istrm(str);
- Complex z;
- istrm >> z;
- if (istrm.good())
- {
- cout << "Read success: " << str << " -> " << z << endl;
- } else {
- cout << "Read error: " << str << " -> " << z << endl;
- }
- return istrm.good();
- }
- int main() {};
- Complex::Complex(const double real) : Complex(real, 0.0) {}
- Complex::Complex(const double real, const double imaginary) : re(real), im(imaginary) {}
- Complex& Complex:: operator+=(const Complex& rhs)
- {
- re += rhs.re;
- im += rhs.im;
- return *this;
- }
- Complex& Complex:: operator-=(const Complex& rhs) // ya sdelyal'
- {
- re -= rhs.re;
- im -= rhs.im;
- return *this;
- }
- Complex operator+(const Complex& lhs, const Complex& rhs)
- {
- Complex sum(lhs);
- sum += rhs;
- return sum;
- }
- Complex operator-(const Complex& lhs, const Complex& rhs) // ya sdelyal'
- {
- Complex dif(lhs);
- sum -= rhs;
- return dif;
- }
- Complex operator*=(const double rhs)
- {
- re *= rhs;
- im *= rhs;
- return *this;
- }
- Complex operator*=(const Complex& rhs) // ya sdelyal'
- {
- Complex mul();
- mul.re = re * rhs.re - im * rhs.im;
- mul.im = re * rhs.im + im * rhs.re;
- return mul;
- }
- std::ostream& Complex::writeTo(std::ostream& ostrm) const
- {
- using namespace std;
- ostrm << leftBrace << re << separator << im << rightBrace << endl;
- return ostrm;
- }
- std::istream& Complex::readFrom(std::istream& istrm)
- {
- char leftBrace(0);
- double real(0.0);
- char comma(0);
- double imaginary(0.0);
- char rightBrace(0);
- istrm >> leftBrace >> real >> comma >> imaginary >> rightBrace;
- if (istrm.good())
- {
- if ((Complex::leftBrace == leftBrace) && (Complex::separator == comma) && (Complex::rightBrace == rightBrace))
- {
- re = real;
- im = imaginary;
- } else {
- istrm.setstate((std::ios_base::failbit));
- }
- }
- return istrm;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement