complex_number.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*=============================================================================
  2. Copyright (c) 2001-2003 Joel de Guzman
  3. http://spirit.sourceforge.net/
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. ///////////////////////////////////////////////////////////////////////////////
  9. //
  10. // A complex number micro parser (using subrules)
  11. //
  12. // [ JDG 5/10/2002 ]
  13. //
  14. ///////////////////////////////////////////////////////////////////////////////
  15. #include <boost/spirit/include/classic_core.hpp>
  16. #include <iostream>
  17. #include <complex>
  18. #include <string>
  19. ///////////////////////////////////////////////////////////////////////////////
  20. using namespace std;
  21. using namespace BOOST_SPIRIT_CLASSIC_NS;
  22. ///////////////////////////////////////////////////////////////////////////////
  23. //
  24. // Our complex number micro parser
  25. //
  26. ///////////////////////////////////////////////////////////////////////////////
  27. bool
  28. parse_complex(char const* str, complex<double>& c)
  29. {
  30. double rN = 0.0;
  31. double iN = 0.0;
  32. subrule<0> first;
  33. subrule<1> r;
  34. subrule<2> i;
  35. if (parse(str,
  36. // Begin grammar
  37. (
  38. first = '(' >> r >> !(',' >> i) >> ')' | r,
  39. r = real_p[assign(rN)],
  40. i = real_p[assign(iN)]
  41. )
  42. ,
  43. // End grammar
  44. space_p).full)
  45. {
  46. c = complex<double>(rN, iN);
  47. return true;
  48. }
  49. else
  50. {
  51. return false;
  52. }
  53. }
  54. ////////////////////////////////////////////////////////////////////////////
  55. //
  56. // Main program
  57. //
  58. ////////////////////////////////////////////////////////////////////////////
  59. int
  60. main()
  61. {
  62. cout << "/////////////////////////////////////////////////////////\n\n";
  63. cout << "\t\tA complex number micro parser for Spirit...\n\n";
  64. cout << "/////////////////////////////////////////////////////////\n\n";
  65. cout << "Give me a complex number of the form r or (r) or (r,i) \n";
  66. cout << "Type [q or Q] to quit\n\n";
  67. string str;
  68. while (getline(cin, str))
  69. {
  70. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  71. break;
  72. complex<double> c;
  73. if (parse_complex(str.c_str(), c))
  74. {
  75. cout << "-------------------------\n";
  76. cout << "Parsing succeeded\n";
  77. cout << str << " Parses OK: " << c << endl;
  78. cout << "-------------------------\n";
  79. }
  80. else
  81. {
  82. cout << "-------------------------\n";
  83. cout << "Parsing failed\n";
  84. cout << "-------------------------\n";
  85. }
  86. }
  87. cout << "Bye... :-) \n\n";
  88. return 0;
  89. }