fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Complex { double realPart, imaginaryPart; };
  6.  
  7. istream &operator>>(istream &input, Complex &complex)
  8. {
  9. char plus,letter;
  10. if (input >> complex.realPart >> plus) {
  11. if (plus!='+' )
  12. input.setstate(ios::failbit);
  13. else if (input >> complex.imaginaryPart>>letter) {
  14. if (letter!='i')
  15. input.setstate(ios::failbit);
  16. }
  17. }
  18. return input;
  19. }
  20.  
  21. int main() {
  22. Complex c;
  23. if (cin>>c)
  24. cout << "Succeeded: ";
  25. else cout << "Failed: ";
  26. cout <<c.realPart<<"+"<<c.imaginaryPart<<"i"<<endl;
  27. return 0;
  28. }
Success #stdin #stdout 0s 3276KB
stdin
12 + 13i
stdout
Succeeded: 12+13i