complex_number.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*=============================================================================
  2. Copyright (c) 2002-2010 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. =============================================================================*/
  6. ///////////////////////////////////////////////////////////////////////////////
  7. //
  8. // A complex number micro parser.
  9. //
  10. // [ JDG May 10, 2002 ] spirit1
  11. // [ JDG May 9, 2007 ] spirit2
  12. //
  13. ///////////////////////////////////////////////////////////////////////////////
  14. #include <boost/config/warning_disable.hpp>
  15. #include <boost/spirit/include/qi.hpp>
  16. #include <boost/spirit/include/phoenix_core.hpp>
  17. #include <boost/spirit/include/phoenix_operator.hpp>
  18. #include <iostream>
  19. #include <string>
  20. #include <complex>
  21. ///////////////////////////////////////////////////////////////////////////////
  22. // Our complex number parser/compiler
  23. ///////////////////////////////////////////////////////////////////////////////
  24. //[tutorial_complex_number
  25. namespace client
  26. {
  27. template <typename Iterator>
  28. bool parse_complex(Iterator first, Iterator last, std::complex<double>& c)
  29. {
  30. using boost::spirit::qi::double_;
  31. using boost::spirit::qi::_1;
  32. using boost::spirit::qi::phrase_parse;
  33. using boost::spirit::ascii::space;
  34. using boost::phoenix::ref;
  35. double rN = 0.0;
  36. double iN = 0.0;
  37. bool r = phrase_parse(first, last,
  38. // Begin grammar
  39. (
  40. '(' >> double_[ref(rN) = _1]
  41. >> -(',' >> double_[ref(iN) = _1]) >> ')'
  42. | double_[ref(rN) = _1]
  43. ),
  44. // End grammar
  45. space);
  46. if (!r || first != last) // fail if we did not get a full match
  47. return false;
  48. c = std::complex<double>(rN, iN);
  49. return r;
  50. }
  51. }
  52. //]
  53. ////////////////////////////////////////////////////////////////////////////
  54. // Main program
  55. ////////////////////////////////////////////////////////////////////////////
  56. int
  57. main()
  58. {
  59. std::cout << "/////////////////////////////////////////////////////////\n\n";
  60. std::cout << "\t\tA complex number micro parser for Spirit...\n\n";
  61. std::cout << "/////////////////////////////////////////////////////////\n\n";
  62. std::cout << "Give me a complex number of the form r or (r) or (r,i) \n";
  63. std::cout << "Type [q or Q] to quit\n\n";
  64. std::string str;
  65. while (getline(std::cin, str))
  66. {
  67. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  68. break;
  69. std::complex<double> c;
  70. if (client::parse_complex(str.begin(), str.end(), c))
  71. {
  72. std::cout << "-------------------------\n";
  73. std::cout << "Parsing succeeded\n";
  74. std::cout << "got: " << c << std::endl;
  75. std::cout << "\n-------------------------\n";
  76. }
  77. else
  78. {
  79. std::cout << "-------------------------\n";
  80. std::cout << "Parsing failed\n";
  81. std::cout << "-------------------------\n";
  82. }
  83. }
  84. std::cout << "Bye... :-) \n\n";
  85. return 0;
  86. }