Advertisement
Guest User

Untitled

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