runtime_unit.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Boost.Units - A C++ library for zero-overhead dimensional analysis and
  2. // unit/quantity manipulation and conversion
  3. //
  4. // Copyright (C) 2003-2008 Matthias Christian Schabel
  5. // Copyright (C) 2008 Steven Watanabe
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See
  8. // accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #include <map>
  11. #include <iostream>
  12. #include <boost/lexical_cast.hpp>
  13. #include <boost/units/quantity.hpp>
  14. #include <boost/units/cmath.hpp>
  15. #include <boost/units/systems/si/length.hpp>
  16. #include <boost/units/base_units/imperial/foot.hpp>
  17. //[runtime_unit_snippet_1
  18. namespace {
  19. using namespace boost::units;
  20. using imperial::foot_base_unit;
  21. std::map<std::string, quantity<si::length> > known_units;
  22. }
  23. quantity<si::length> calculate(const quantity<si::length>& t)
  24. {
  25. return(boost::units::hypot(t, 2.0 * si::meters));
  26. }
  27. int main()
  28. {
  29. known_units["meter"] = 1.0 * si::meters;
  30. known_units["centimeter"] = .01 * si::meters;
  31. known_units["foot"] =
  32. conversion_factor(foot_base_unit::unit_type(), si::meter) * si::meter;
  33. std::string output_type("meter");
  34. std::string input;
  35. while((std::cout << "> ") && (std::cin >> input))
  36. {
  37. if(!input.empty() && input[0] == '#')
  38. {
  39. std::getline(std::cin, input);
  40. }
  41. else if(input == "exit")
  42. {
  43. break;
  44. }
  45. else if(input == "help")
  46. {
  47. std::cout << "type \"exit\" to exit\n"
  48. "type \"return 'unit'\" to set the return units\n"
  49. "type \"'number' 'unit'\" to do a simple calculation"
  50. << std::endl;
  51. }
  52. else if(input == "return")
  53. {
  54. if(std::cin >> input)
  55. {
  56. if(known_units.find(input) != known_units.end())
  57. {
  58. output_type = input;
  59. std::cout << "Done." << std::endl;
  60. }
  61. else
  62. {
  63. std::cout << "Unknown unit \"" << input << "\""
  64. << std::endl;
  65. }
  66. }
  67. else
  68. {
  69. break;
  70. }
  71. }
  72. else
  73. {
  74. try
  75. {
  76. double value = boost::lexical_cast<double>(input);
  77. if(std::cin >> input)
  78. {
  79. if(known_units.find(input) != known_units.end())
  80. {
  81. std::cout << static_cast<double>(
  82. calculate(value * known_units[input]) /
  83. known_units[output_type])
  84. << ' ' << output_type << std::endl;
  85. }
  86. else
  87. {
  88. std::cout << "Unknown unit \"" << input << "\""
  89. << std::endl;
  90. }
  91. }
  92. else
  93. {
  94. break;
  95. }
  96. }
  97. catch(...)
  98. {
  99. std::cout << "Input error" << std::endl;
  100. }
  101. }
  102. }
  103. }
  104. //]