thousand_separated.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*=============================================================================
  2. Copyright (c) 2002-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 parser for a real number parser that parses thousands separated numbers
  11. // with at most two decimal places and no exponent. This is discussed in the
  12. // "Numerics" chapter in the Spirit User's Guide.
  13. //
  14. // [ JDG 12/16/2003 ]
  15. //
  16. ///////////////////////////////////////////////////////////////////////////////
  17. #include <boost/spirit/include/classic_core.hpp>
  18. #include <boost/spirit/include/classic_assign_actor.hpp>
  19. #include <iostream>
  20. #include <string>
  21. ///////////////////////////////////////////////////////////////////////////////
  22. using namespace std;
  23. using namespace BOOST_SPIRIT_CLASSIC_NS;
  24. template <typename T>
  25. struct ts_real_parser_policies : public ureal_parser_policies<T>
  26. {
  27. // These policies can be used to parse thousand separated
  28. // numbers with at most 2 decimal digits after the decimal
  29. // point. e.g. 123,456,789.01
  30. typedef uint_parser<int, 10, 1, 2> uint2_t;
  31. typedef uint_parser<T, 10, 1, -1> uint_parser_t;
  32. typedef int_parser<int, 10, 1, -1> int_parser_t;
  33. ////////////////////////////////// 2 decimal places Max
  34. template <typename ScannerT>
  35. static typename parser_result<uint2_t, ScannerT>::type
  36. parse_frac_n(ScannerT& scan)
  37. { return uint2_t().parse(scan); }
  38. ////////////////////////////////// No exponent
  39. template <typename ScannerT>
  40. static typename parser_result<chlit<>, ScannerT>::type
  41. parse_exp(ScannerT& scan)
  42. { return scan.no_match(); }
  43. ////////////////////////////////// No exponent
  44. template <typename ScannerT>
  45. static typename parser_result<int_parser_t, ScannerT>::type
  46. parse_exp_n(ScannerT& scan)
  47. { return scan.no_match(); }
  48. ////////////////////////////////// Thousands separated numbers
  49. template <typename ScannerT>
  50. static typename parser_result<uint_parser_t, ScannerT>::type
  51. parse_n(ScannerT& scan)
  52. {
  53. typedef typename parser_result<uint_parser_t, ScannerT>::type RT;
  54. static uint_parser<unsigned, 10, 1, 3> uint3_p;
  55. static uint_parser<unsigned, 10, 3, 3> uint3_3_p;
  56. if (RT hit = uint3_p.parse(scan))
  57. {
  58. T n;
  59. typedef typename ScannerT::iterator_t iterator_t;
  60. iterator_t save = scan.first;
  61. while (match<> next = (',' >> uint3_3_p[assign_a(n)]).parse(scan))
  62. {
  63. hit.value((hit.value() * 1000) + n);
  64. scan.concat_match(hit, next);
  65. save = scan.first;
  66. }
  67. scan.first = save;
  68. return hit;
  69. // Note: On erroneous input such as "123,45", the result should
  70. // be a partial match "123". 'save' is used to makes sure that
  71. // the scanner position is placed at the last *valid* parse
  72. // position.
  73. }
  74. return scan.no_match();
  75. }
  76. };
  77. real_parser<double, ts_real_parser_policies<double> > const
  78. ts_real_p = real_parser<double, ts_real_parser_policies<double> >();
  79. ////////////////////////////////////////////////////////////////////////////
  80. //
  81. // Main program
  82. //
  83. ////////////////////////////////////////////////////////////////////////////
  84. int
  85. main()
  86. {
  87. cout << "/////////////////////////////////////////////////////////\n\n";
  88. cout << "\t\tA real number parser that parses thousands separated\n";
  89. cout << "\t\tnumbers with at most two decimal places and no exponent...\n\n";
  90. cout << "/////////////////////////////////////////////////////////\n\n";
  91. cout << "Give me a number.\n";
  92. cout << "Type [q or Q] to quit\n\n";
  93. string str;
  94. double n;
  95. while (getline(cin, str))
  96. {
  97. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  98. break;
  99. if (parse(str.c_str(), ts_real_p[assign_a(n)]).full)
  100. {
  101. cout << "-------------------------\n";
  102. cout << "Parsing succeeded\n";
  103. cout << str << " Parses OK: " << endl;
  104. cout << "n=" << n << endl;
  105. cout << "-------------------------\n";
  106. }
  107. else
  108. {
  109. cout << "-------------------------\n";
  110. cout << "Parsing failed\n";
  111. cout << "-------------------------\n";
  112. }
  113. }
  114. cout << "Bye... :-) \n\n";
  115. return 0;
  116. }