分数,例如1/2 2/3等..不存储在双打或浮点数或 整数中。它们实际上是公式。你可以存储的值是0.5和 0.6666然而。 如果你想能够分数,例如 12 1/2 那么你需要解析字符串并自己做数学运算。如果我想要这样做,我会在字符串中寻找一个空格。如果我找到一个 的空间,我会把空间之前的所有内容作为整数。然后 我会找/。我会从空间到/作为 运算符。作为devisior之后的一切。然后简单地说: double高度=整个+操作员/主管;Fractions, such as 1/2 2/3 etc.. are not stored in doubles or floats orints. They are actually formulas. The values you can store are 0.5 and0.6666 however.If you want to be able to put in fractions, such as12 1/2then you will need to parse the string and do the math yourself. If Iwanted to do this I would look for a space in the string. If I found aspace I would take everything before the space as the whole number. ThenI''d look for the /. I would take everthing from the space to the / as theoperator. Everything after the / as the devisior. Then simply:double Height = Whole + operator / devisor; BTW你可以查看我的Quan库这个东西: http://quan.sourceforge.net/quan_mat...tml/index.html 在VC7.1上测试过, VC8.0和gcc4 在Quan中,代码可能如下所示: #include< quan / out / length.hpp> #include< quan / out / mass.hpp> #include< quan / fixed_quantity / io / input.hpp> int main(无效) { std :: cout<< METRIC CONVERTER << std :: endl<< std :: endl; quan :: length ::高度; quan :: mass :: lb weight; std :: cout<< 以英寸为单位输入你的身高 ; std :: cin> height.reference_numeric_value< quan :: length :: inBTW You could check my Quan library for this stuff: http://quan.sourceforge.net/quan_mat...tml/index.htmlTested on VC7.1, VC8.0 and gcc4In Quan the code might look like this:#include <quan/out/length.hpp>#include <quan/out/mass.hpp>#include <quan/fixed_quantity/io/input.hpp>int main(void){std::cout << "METRIC CONVERTER"<< std::endl << std::endl ;quan::length::in height;quan::mass::lb weight;std::cout << "Enter your height in inches " ;std::cin >height.reference_numeric_value<quan::length::in std :: cout<< 以磅为单位输入你的体重 ; std :: cin> weight.reference_numeric_value< quan :: mass :: lbstd::cout << "Enter your weight in pounds" ;std::cin >weight.reference_numeric_value<quan::mass::lb std :: cout<< std :: endl; quan :: length :: m metric_height = height; quan :: mass :: kg metric_weight = weight; std :: cout<< 你的身高是多少 << metric_height<< std :: endl; std :: cout<< 你的体重是 << metric_weight<< std :: endl; } 问候 Andy Littlestd::cout << std::endl ;quan::length::m metric_height = height;quan::mass::kg metric_weight = weight;std::cout << "Your height is "<< metric_height << std::endl;std::cout << "Your weight is "<< metric_weight << std::endl;}regardsAndy Little 这篇关于C ++中的分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-12 23:42