【问题标题】:boost units parsing string stream提升单元解析字符串流
【发布时间】:2014-11-30 13:34:52
【问题描述】:

boost 单位库提供有用的编译时“度量单位”类型检查。它还提供流 io 操作来序列化单元。但是,我在字符串解析位上苦苦挣扎。 例如以下几行:

boost::units::quantity<boost::units::si::force> f(2.0 * boost::units::si::newton);
std::cout << "Force = " << f << std::endl;

产生输出:

Force = 2.0 N

有人能指出一个将这些标准序列化解析回升压单元的示例吗?

// f.parse_string("2.0 N");  or using stream operators??

谢谢!

【问题讨论】:

    标签: c++ string parsing boost units-of-measurement


    【解决方案1】:

    库不直接支持。

    有一个例子可以提供一些启发:http://www.boost.org/doc/libs/1_56_0/doc/html/boost_units/Examples.html#boost_units.Examples.RuntimeUnits

    这个例子展示了如何实现一个允许不同的接口 运行时的单元,同时仍然保持内部的类型安全 计算。

    namespace {
    
    using namespace boost::units;
    using imperial::foot_base_unit;
    
    std::map<std::string, quantity<si::length> > known_units;
    
    }
    
    quantity<si::length> calculate(const quantity<si::length>& t)
    {
        return(boost::units::hypot(t, 2.0 * si::meters));
    }
    
    int main()
    {
        known_units["meter"] = 1.0 * si::meters;
        known_units["centimeter"] = .01 * si::meters;
        known_units["foot"] =
            conversion_factor(foot_base_unit::unit_type(), si::meter) * si::meter;
    
        std::string output_type("meter");
        std::string input;
    
        while((std::cout << "> ") && (std::cin >> input))
        {
            if(!input.empty() && input[0] == '#')
            {
                std::getline(std::cin, input);
            }
            else if(input == "exit")
            {
                break;
            }
            else if(input == "help")
            {
                std::cout << "type \"exit\" to exit\n"
                    "type \"return 'unit'\" to set the return units\n"
                    "type \"'number' 'unit'\" to do a simple calculation"
                    << std::endl;
            }
            else if(input == "return")
            {
                if(std::cin >> input)
                {
                    if(known_units.find(input) != known_units.end())
                    {
                        output_type = input;
                        std::cout << "Done." << std::endl;
                    }
                    else
                    {
                        std::cout << "Unknown unit \"" << input << "\""
                             << std::endl;
                    }
                }
                else
                {
                    break;
                }
            }
            else
            {
                try
                {
                    double value = boost::lexical_cast<double>(input);
    
                    if(std::cin >> input)
                    {
                        if(known_units.find(input) != known_units.end())
                        {
                            std::cout << static_cast<double>(
                                calculate(value * known_units[input]) /
                                known_units[output_type])
                                << ' ' << output_type << std::endl;
                        }
                        else
                        {
                            std::cout << "Unknown unit \"" << input << "\""
                                << std::endl;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                catch(...)
                {
                    std::cout << "Input error" << std::endl;
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-22
      • 2019-09-11
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      相关资源
      最近更新 更多