C ++中的分数 [英] Fractions in C++

查看:60
本文介绍了C ++中的分数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有这个问题,我不知道我的

计划有什么问题。我试图输入我的两个变量高度和重量作为

分数。我将它们声明为float并且也是double,但是当我输入一个小数而不是十进制/整数

数时,

程序就会中止。有人能告诉我问题出在哪里,以及我需要什么才能更正我的代码?非常感谢Farah。

#include< iostream>

使用命名空间std;


int main(void )

{

const double INCHES_PER_METER = 39.37;

const double POUNDS_PER_KG = 2.24;


长双倍高度;

长双倍重量;


cout<< METRIC CONVERTER << endl<< endl;

cout<< 以英寸为单位输入你的身高 ;

cin>身高;


cout<< 以磅为单位输入你的体重 ;

cin>重量;

cout<<结束;


double metric_height = height * INCHES_PER_METER;


double metric_weight = weight / POUNDS_PER_KG;

cout< ;< 你的身高是多少 << metric_height<< "米"。 <<

endl;

cout<< 你的体重是 << metric_weight<< "公斤"。 <<

endl;


返回0;

}

Hi everyone, I have this problem and I don''t know what''s wrong with my
program. I am trying to enter my two variables height and weight as
fraction numbers. I declared them as float and also as double, but the
program aborts when I input a fraction instead of decimal/integer
number. Could someone tell me where the problem is, and what I need to
do to correct my code? Thanks a lot in advance, Farah.
#include <iostream>
using namespace std;

int main(void)
{
const double INCHES_PER_METER = 39.37;
const double POUNDS_PER_KG = 2.24;

long double height;
long double weight;

cout << "METRIC CONVERTER" << endl << endl ;
cout << "Enter your height in inches " ;
cin >height;

cout << "Enter your weight in pounds" ;
cin >weight;
cout << endl ;

double metric_height = height*INCHES_PER_METER;

double metric_weight = weight/POUNDS_PER_KG;
cout << "Your height is " << metric_height << " meters." <<
endl;
cout << "Your weight is " << metric_weight << " kilograms." <<
endl;

return 0;
}

推荐答案

2006年9月28日20:32:56 -0700 in comp.lang.c ++,

" fa ******** **@gmail.com" < fa ********** @ gmail.comwrote,
On 28 Sep 2006 20:32:56 -0700 in comp.lang.c++,
"fa**********@gmail.com" <fa**********@gmail.comwrote,

我试图将我的两个变量高度和重量输入为
分数。我将它们声明为float并且也是double,但是当我输入一个小数而不是十进制/整数
时,
程序会中止。
I am trying to enter my two variables height and weight as
fraction numbers. I declared them as float and also as double, but the
program aborts when I input a fraction instead of decimal/integer
number.



请告诉我们哪本书骗你并告诉你,你可以

输入数字作为分数?


< fa ********** @ gmail.comwrote in message

news:11 ******** **************@i42g2000cwa.googlegr oups.com ...
<fa**********@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...

大家好,我有这个问题我不喜欢我不知道我的

计划有什么问题。我试图输入我的两个变量高度和重量作为

分数。我将它们声明为float并且也是double,但是当我输入一个小数而不是十进制/整数

数时,

程序就会中止。有人能告诉我问题出在哪里,以及我需要什么才能更正我的代码?非常感谢Farah。


#include< iostream>

使用命名空间std;


int main(void)

{

const double INCHES_PER_METER = 39.37;

const double POUNDS_PER_KG = 2.24;


长双倍高度;

长双倍重量;


cout<< METRIC CONVERTER << endl<< endl;

cout<< 以英寸为单位输入你的身高 ;

cin>身高;


cout<< 以磅为单位输入你的体重 ;

cin>重量;

cout<<结束;


double metric_height = height * INCHES_PER_METER;


double metric_weight = weight / POUNDS_PER_KG;


cout<< 你的身高是多少 << metric_height<< "米"。 <<

endl;

cout<< 你的体重是 << metric_weight<< "公斤"。 <<

endl;


返回0;

}
Hi everyone, I have this problem and I don''t know what''s wrong with my
program. I am trying to enter my two variables height and weight as
fraction numbers. I declared them as float and also as double, but the
program aborts when I input a fraction instead of decimal/integer
number. Could someone tell me where the problem is, and what I need to
do to correct my code? Thanks a lot in advance, Farah.
#include <iostream>
using namespace std;

int main(void)
{
const double INCHES_PER_METER = 39.37;
const double POUNDS_PER_KG = 2.24;

long double height;
long double weight;

cout << "METRIC CONVERTER" << endl << endl ;
cout << "Enter your height in inches " ;
cin >height;

cout << "Enter your weight in pounds" ;
cin >weight;
cout << endl ;

double metric_height = height*INCHES_PER_METER;

double metric_weight = weight/POUNDS_PER_KG;
cout << "Your height is " << metric_height << " meters." <<
endl;
cout << "Your weight is " << metric_weight << " kilograms." <<
endl;

return 0;
}



分数,例如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 or
ints. They are actually formulas. The values you can store are 0.5 and
0.6666 however.

If you want to be able to put in fractions, such as
12 1/2
then you will need to parse the string and do the math yourself. If I
wanted to do this I would look for a space in the string. If I found a
space I would take everything before the space as the whole number. Then
I''d look for the /. I would take everthing from the space to the / as the
operator. Everything after the / as the devisior. Then simply:

double Height = Whole + operator / devisor;



fa ********** @ gmail.com 写道:

大家好,我有这个问题,我不知道我的

计划有什么问题。我试图输入我的两个变量高度和重量作为

分数。我将它们声明为float并且也是double,但是当我输入一个小数而不是十进制/整数

数时,

程序就会中止。有人能告诉我问题出在哪里,以及我需要什么才能更正我的代码?非常感谢Farah。


#include< iostream>

使用命名空间std;


int main(void)

{

const double INCHES_PER_METER = 39.37;

const double POUNDS_PER_KG = 2.24;


长双倍高度;

长双倍重量;


cout<< METRIC CONVERTER << endl<< endl;

cout<< 以英寸为单位输入你的身高 ;

cin>身高;


cout<< 以磅为单位输入你的体重 ;

cin>重量;

cout<<结束;


double metric_height = height * INCHES_PER_METER;


double metric_weight = weight / POUNDS_PER_KG;


cout<< 你的身高是多少 << metric_height<< "米"。 <<

endl;

cout<< 你的体重是 << metric_weight<< "公斤"。 <<

endl;


返回0;

}
Hi everyone, I have this problem and I don''t know what''s wrong with my
program. I am trying to enter my two variables height and weight as
fraction numbers. I declared them as float and also as double, but the
program aborts when I input a fraction instead of decimal/integer
number. Could someone tell me where the problem is, and what I need to
do to correct my code? Thanks a lot in advance, Farah.
#include <iostream>
using namespace std;

int main(void)
{
const double INCHES_PER_METER = 39.37;
const double POUNDS_PER_KG = 2.24;

long double height;
long double weight;

cout << "METRIC CONVERTER" << endl << endl ;
cout << "Enter your height in inches " ;
cin >height;

cout << "Enter your weight in pounds" ;
cin >weight;
cout << endl ;

double metric_height = height*INCHES_PER_METER;

double metric_weight = weight/POUNDS_PER_KG;
cout << "Your height is " << metric_height << " meters." <<
endl;
cout << "Your weight is " << metric_weight << " kilograms." <<
endl;

return 0;
}



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 :: in

BTW You could check my Quan library for this stuff:

http://quan.sourceforge.net/quan_mat...tml/index.html

Tested on VC7.1, VC8.0 and gcc4

In 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 :: lb

std::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 Little

std::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;
}

regards
Andy Little


这篇关于C ++中的分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆