Advertisement
Guest User

complex_example

a guest
Sep 19th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3.  
  4. struct Complex
  5. {
  6.     Complex() {}
  7.     explicit Complex(const double real);
  8.     Complex(const double real, const double imaginary);
  9.     bool operator==(const Complex& rhs) const { return (re == rhs.re) && (im == rhs.im); }
  10.     bool operator!=(const Complex& rhs) const { return !operator==(rhs); }
  11.     Complex operator+=(const Complex& rhs);
  12.     Complex operator+=(const double rhs) { return operator+=(Complex(rhs)); }
  13.     Complex operator-=(const Complex& rhs);
  14.     Complex operator-=(const double rhs) { return operator-=(Complex(rhs)); }
  15.     Complex operator*=(const Complex& rhs);
  16.     Complex operator*=(const double rhs);
  17.     std::ostream& writeTo(std::ostream& ostrm) const;
  18.     std::istream& readFrom(std::istream& istrm);
  19.  
  20.  
  21.     double re{0.0};
  22.     double im{0.0};
  23.  
  24.     static const char leftBrace{'{'};
  25.     static const char separator{','};
  26.     static const char rightBrace{'}'};
  27. };
  28.  
  29. Complex operator+(const Complex& lhs, const Complex& rhs);
  30. Complex operator-(const Complex& lhs, const Complex& rhs);
  31.  
  32. inline std::ostream& operator<<(std::ostream& ostrm, const Complex& rhs)
  33. {
  34.     return rhs.writeTo(ostrm);
  35. }
  36.  
  37. inline std::istream operator>>(std::istream istrm, const Complex& rhs)
  38. {
  39.     return rhs.readFrom(istrm);
  40. }
  41.  
  42. bool testParse(const std::string& str)
  43. {
  44.     using namespace std;
  45.     istringstream istrm(str);
  46.     Complex z;
  47.     istrm >> z;
  48.     if (istrm.good())
  49.     {
  50.         cout << "Read success: " << str << " -> " << z << endl;
  51.     } else {
  52.         cout << "Read error: " << str << " -> " << z << endl;
  53.     }
  54.     return istrm.good();
  55. }
  56.  
  57. int main() {};
  58.  
  59. Complex::Complex(const double real) : Complex(real, 0.0) {}
  60.  
  61. Complex::Complex(const double real, const double imaginary) : re(real), im(imaginary) {}
  62.  
  63. Complex& Complex:: operator+=(const Complex& rhs)
  64. {
  65.     re += rhs.re;
  66.     im += rhs.im;
  67.     return *this;
  68. }
  69.  
  70. Complex& Complex:: operator-=(const Complex& rhs) // ya sdelyal'
  71. {
  72.     re -= rhs.re;
  73.     im -= rhs.im;
  74.     return *this;
  75. }
  76.  
  77. Complex operator+(const Complex& lhs, const Complex& rhs)
  78. {
  79.     Complex sum(lhs);
  80.     sum += rhs;
  81.     return sum;
  82. }
  83.  
  84. Complex operator-(const Complex& lhs, const Complex& rhs) // ya sdelyal'
  85. {
  86.     Complex dif(lhs);
  87.     sum -= rhs;
  88.     return dif;
  89. }
  90. Complex operator*=(const double rhs)
  91. {
  92.     re *= rhs;
  93.     im *= rhs;
  94.     return *this;
  95. }
  96.  
  97. Complex operator*=(const Complex& rhs) // ya sdelyal'
  98. {
  99.     Complex mul();
  100.     mul.re = re * rhs.re - im * rhs.im;
  101.     mul.im = re * rhs.im + im * rhs.re;
  102.     return mul;
  103. }
  104.  
  105. std::ostream& Complex::writeTo(std::ostream& ostrm) const
  106. {
  107.     using namespace std;
  108.     ostrm << leftBrace << re << separator << im << rightBrace << endl;
  109.     return ostrm;
  110. }
  111.  
  112. std::istream& Complex::readFrom(std::istream& istrm)
  113. {
  114.     char leftBrace(0);
  115.     double real(0.0);
  116.     char comma(0);
  117.     double imaginary(0.0);
  118.     char rightBrace(0);
  119.     istrm >> leftBrace >> real >> comma >> imaginary >> rightBrace;
  120.     if (istrm.good())
  121.     {
  122.         if ((Complex::leftBrace == leftBrace) && (Complex::separator == comma) && (Complex::rightBrace == rightBrace))
  123.         {
  124.             re = real;
  125.             im = imaginary;
  126.         } else {
  127.             istrm.setstate((std::ios_base::failbit));
  128.         }
  129.     }
  130.     return istrm;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement