complex_number.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright (c) 2001-2010 Hartmut Kaiser
  2. // Copyright (c) 2001-2010 Joel de Guzman
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ///////////////////////////////////////////////////////////////////////////////
  7. //
  8. // A complex number micro generator.
  9. //
  10. // [ HK July 7, 2009 ] spirit2
  11. //
  12. ///////////////////////////////////////////////////////////////////////////////
  13. #include <boost/config/warning_disable.hpp>
  14. #include <boost/spirit/include/qi.hpp>
  15. #include <boost/spirit/include/karma.hpp>
  16. #include <boost/spirit/include/phoenix_core.hpp>
  17. #include <boost/spirit/include/phoenix_operator.hpp>
  18. #include <boost/fusion/include/std_pair.hpp>
  19. #include <iostream>
  20. #include <string>
  21. #include <complex>
  22. namespace client
  23. {
  24. ///////////////////////////////////////////////////////////////////////////
  25. // Our complex number parser/compiler (that's just a copy of the complex
  26. // number example from Qi (see examples/qi/complex_number.cpp)
  27. ///////////////////////////////////////////////////////////////////////////
  28. template <typename Iterator>
  29. bool parse_complex(Iterator first, Iterator last, std::complex<double>& c)
  30. {
  31. using boost::spirit::qi::double_;
  32. using boost::spirit::qi::_1;
  33. using boost::spirit::qi::phrase_parse;
  34. using boost::spirit::ascii::space;
  35. using boost::phoenix::ref;
  36. double rN = 0.0;
  37. double iN = 0.0;
  38. bool r = phrase_parse(first, last,
  39. (
  40. '(' >> double_[ref(rN) = _1]
  41. >> -(',' >> double_[ref(iN) = _1]) >> ')'
  42. | double_[ref(rN) = _1]
  43. ),
  44. space);
  45. if (!r || first != last) // fail if we did not get a full match
  46. return false;
  47. c = std::complex<double>(rN, iN);
  48. return r;
  49. }
  50. ///////////////////////////////////////////////////////////////////////////
  51. // Our complex number generator
  52. ///////////////////////////////////////////////////////////////////////////
  53. //[tutorial_karma_complex_number
  54. template <typename OutputIterator>
  55. bool generate_complex(OutputIterator sink, std::complex<double> const& c)
  56. {
  57. using boost::spirit::karma::eps;
  58. using boost::spirit::karma::double_;
  59. using boost::spirit::karma::_1;
  60. using boost::spirit::karma::generate;
  61. return generate(sink,
  62. // Begin grammar
  63. (
  64. eps(c.imag() != 0) <<
  65. '(' << double_[_1 = c.real()] << ", " << double_[_1 = c.imag()] << ')'
  66. | double_[_1 = c.real()]
  67. )
  68. // End grammar
  69. );
  70. }
  71. //]
  72. }
  73. ///////////////////////////////////////////////////////////////////////////////
  74. // Main program
  75. ///////////////////////////////////////////////////////////////////////////////
  76. int main()
  77. {
  78. std::cout << "/////////////////////////////////////////////////////////\n\n";
  79. std::cout << "\t\tA complex number micro generator for Spirit...\n\n";
  80. std::cout << "/////////////////////////////////////////////////////////\n\n";
  81. std::cout << "Give me a complex number of the form r or (r) or (r,i) \n";
  82. std::cout << "Type [q or Q] to quit\n\n";
  83. std::string str;
  84. while (getline(std::cin, str))
  85. {
  86. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  87. break;
  88. std::complex<double> c;
  89. if (client::parse_complex(str.begin(), str.end(), c))
  90. {
  91. std::cout << "-------------------------\n";
  92. std::string generated;
  93. std::back_insert_iterator<std::string> sink(generated);
  94. if (!client::generate_complex(sink, c))
  95. {
  96. std::cout << "-------------------------\n";
  97. std::cout << "Generating failed\n";
  98. std::cout << "-------------------------\n";
  99. }
  100. else
  101. {
  102. std::cout << "-------------------------\n";
  103. std::cout << "Generated: " << generated << "\n";
  104. std::cout << "-------------------------\n";
  105. }
  106. }
  107. else
  108. {
  109. std::cout << "-------------------------\n";
  110. std::cout << "Parsing failed\n";
  111. std::cout << "-------------------------\n";
  112. }
  113. }
  114. std::cout << "Bye... :-) \n\n";
  115. return 0;
  116. }