C++中的混合分数可以增强有理库
Mixed fractional numbers in C++ boost rational library
是否可以在C++提升有理数库中初始化和使用混合小数?
如果你的意思是像这里: http://www.calculatorsoup.com/calculators/math/mixed-number-to-improper-fraction.php 那么肯定:
住在科里鲁
#include <boost/rational.hpp>
#include <iostream>
using R = boost::rational<int>;
int main() {
std::cout << "3 + 5/9: " << 3 + R(5,9) << "n";
}
指纹
3 + 5/9: 32/9
如果你的意思是输出格式,你可以制作自己的小型IO操纵器:
template <typename R>
struct as_mixed_wrapper {
R value;
friend std::ostream& operator<<(std::ostream& os, as_mixed_wrapper const& w) {
auto i = boost::rational_cast<typename R::int_type>(w.value);
return os << i << " " << (w.value - i);
}
};
template <typename R> as_mixed_wrapper<R> as_mixed(R const& value) {
return {value};
}
并像这样使用它:住在科里鲁
auto n = 3 + R(5,9);
std::cout << n << " would be " << as_mixed(n) << "n";
哪些打印
32/9 would be 3 5/9
奖金
还为机械手实现了快速和脏流提取:
住在科里鲁
#include <boost/rational.hpp>
#include <boost/lexical_cast.hpp>
#include <iostream>
using R = boost::rational<int>;
template <typename R>
struct as_mixed_wrapper {
R* value;
friend std::ostream& operator<<(std::ostream& os, as_mixed_wrapper const& w) {
auto i = boost::rational_cast<typename R::int_type>(*w.value);
return os << i << " " << (*w.value - i);
}
friend std::istream& operator>>(std::istream& is, as_mixed_wrapper const& w) {
typename R::int_type i, d, n;
char c;
if ((is >> i >> d >> c) && (c == '/') && (is >> n))
*w.value = i + R(d,n);
return is;
}
};
template <typename R> as_mixed_wrapper<R> as_mixed(R& value) {
return {&value};
}
int main() {
auto n = 3 + R(5,9);
std::cout << n << " would be " << as_mixed(n) << "n";
std::istringstream iss("123 7 / 13");
if (iss >> as_mixed(n))
std::cout << n << " would be " << as_mixed(n) << "n";
else
std::cout << "Parse errorn";
}
指纹
32/9 would be 3 5/9
1606/13 would be 123 7/13
相关文章:
- 无法将结构注册为增强几何体3D点
- 混合组合和继承的C++问题
- 在混合代码库中将C转换为C++时出现许多包含错误
- D3D11-将混合权重和索引传递到顶点着色器
- 增强基于 XML class_id的反序列化
- 增强精神解析器规则以检测语句中的特殊结尾
- C++ 使用增强正则表达式库时断言崩溃
- 增强精神 X3:错误:在"..."中没有名为"大小"的类型
- C++分数混合比较运算符错误
- 如何包装(撰写)增强 hana 地图并访问括号运算符(运算符 [])?
- 有没有办法在C ++中检测汉字?(使用增强)
- 如何将增强MSM与增强信号结合使用?
- 是否可以混合使用SFINAE和模板专业化?
- 如何在 Python C++ 混合库中调试非确定性分段错误?
- 使用 Boost.Spirit 解析具有混合数据类型的 OBJ 文件?
- C++11 中的混合列表初始化
- C++以迭代方式搜索混合类型地图
- C++中的混合分数可以增强有理库
- 增强:精神字符/字符串混合
- 混合增强库版本