• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #include <map>
12 #include <iostream>
13 #include <boost/lexical_cast.hpp>
14 #include <boost/units/quantity.hpp>
15 #include <boost/units/cmath.hpp>
16 #include <boost/units/systems/si/length.hpp>
17 #include <boost/units/base_units/imperial/foot.hpp>
18 
19 //[runtime_unit_snippet_1
20 
21 namespace {
22 
23 using namespace boost::units;
24 using imperial::foot_base_unit;
25 
26 std::map<std::string, quantity<si::length> > known_units;
27 
28 }
29 
calculate(const quantity<si::length> & t)30 quantity<si::length> calculate(const quantity<si::length>& t)
31 {
32     return(boost::units::hypot(t, 2.0 * si::meters));
33 }
34 
main()35 int main()
36 {
37     known_units["meter"] = 1.0 * si::meters;
38     known_units["centimeter"] = .01 * si::meters;
39     known_units["foot"] =
40         conversion_factor(foot_base_unit::unit_type(), si::meter) * si::meter;
41 
42     std::string output_type("meter");
43     std::string input;
44 
45     while((std::cout << "> ") && (std::cin >> input))
46     {
47         if(!input.empty() && input[0] == '#')
48         {
49             std::getline(std::cin, input);
50         }
51         else if(input == "exit")
52         {
53             break;
54         }
55         else if(input == "help")
56         {
57             std::cout << "type \"exit\" to exit\n"
58                 "type \"return 'unit'\" to set the return units\n"
59                 "type \"'number' 'unit'\" to do a simple calculation"
60                 << std::endl;
61         }
62         else if(input == "return")
63         {
64             if(std::cin >> input)
65             {
66                 if(known_units.find(input) != known_units.end())
67                 {
68                     output_type = input;
69                     std::cout << "Done." << std::endl;
70                 }
71                 else
72                 {
73                     std::cout << "Unknown unit \"" << input << "\""
74                          << std::endl;
75                 }
76             }
77             else
78             {
79                 break;
80             }
81         }
82         else
83         {
84             try
85             {
86                 double value = boost::lexical_cast<double>(input);
87 
88                 if(std::cin >> input)
89                 {
90                     if(known_units.find(input) != known_units.end())
91                     {
92                         std::cout << static_cast<double>(
93                             calculate(value * known_units[input]) /
94                             known_units[output_type])
95                             << ' ' << output_type << std::endl;
96                     }
97                     else
98                     {
99                         std::cout << "Unknown unit \"" << input << "\""
100                             << std::endl;
101                     }
102                 }
103                 else
104                 {
105                     break;
106                 }
107             }
108             catch(...)
109             {
110                 std::cout << "Input error" << std::endl;
111             }
112         }
113     }
114 }
115 
116 //]
117