通过使用两个int变量浮点到int转换来表示float变量 [英] Float to int conversion by using two int variables for representation of the float variable
问题描述
各位大家好,我想请任何人帮我理解
应该完全解决这个问题。到目前为止,我已经提供了我所拥有的
,虽然它不太准确。
创建一个以美元操作的类。调用新类Money。
此类中所有数据组件的美元和美分都是整数
变量(这是我感到困惑的地方,不知道如何实际上
做的工作)。包括成员函数和构造函数以初始化
Money为0并将其初始化为固定值。会员功能
应以适当的格式显示。包括
允许双向数据转换的成员函数:
- 从实际(浮动)价值(4.5f美元)到美元/美分(4美元,50美元) $>
美分)
- 从美元/美分(4美元,50美分)到实际(浮动)价值(4.5f
美元)>
这是我到目前为止所做的事情虽然我不确定
如果我的方法是正确的话。所以,如果有人可以帮我这个,我会很感激,这很有意思,但我一直在尝试
很长一段时间我只能''弄明白!
感谢提前任何人!
#include< iostream>
使用namespace std;
class Money {
private:int dollars; int cents;
public:Money(){dollars = 0;美分= 0; } $ / $
Money(int dol,int cnt){dollar = dol;美分= cnt; } $ / $
Money(float fl_dollars)// one-arg构造函数
{
cout<< fl_dollars值是: << fl_dollars<< endl;
float flt_dollars = fl_dollars;
dollar = int(flt_dollars);
cout<< fl_dollars值是: << flt_dollars<< endl<< "并且
美元是: << levs<< endl;
float cents_part = float(fl_dollars - dollar);
cents = cents_part;
cout<< 转换后的分数: <<美分<< endl;
cout<< 美分值是: <<美分<< endl;
cout<< \ n 1-arg构造函数已激活\ n;
}
void ShowMoney()
{
cout<<" \ nDistObject =" <<美元<< "美元" <<"和<<分
<< "美分" << endl<<结束;
}
};
void main()
{
//从基本转换为用户定义>>> one-arg构造函数
资金d3 = 10.89;
d3.ShowMoney();
}
Hello everybody, I would like to ask anybody to help me understand what
should be done exactly with this Problem. I have provided what I have
up to now, although it is not quite accurate.
Create a class to operate on the US currency. Call the new class Money.
All the data components in this class for dollars and cents are integer
variables (this is where I get confused and don''t know how to actually
do the job). Include member functions and constructors to initialize
Money to 0 and to initialize it to fixed values. A member function
should display it in appropriate format. Include member functions that
permit data conversion in both directions:
- from real (float) value (4.5f US) to dollars/cents ( 4 dollar, 50
cents)
- from dollars/cents (4 dollar, 50 cents) to real (float) value (4.5f
US)
Here it is what I have managed to do up to now although I am not sure
if I am on the right way even. So if anybody could help me with this I
would really appreciate, it is interesting, but I''ve been trying for
quite some time and I just can''t figure it out!
Thanks to anyone in advance!
#include <iostream>
using namespace std;
class Money {
private: int dollars; int cents;
public: Money() { dollars = 0; cents = 0; }
Money (int dol, int cnt) { dollars = dol; cents = cnt; }
Money (float fl_dollars) // one-arg constructor
{
cout << "fl_dollars value is: " << fl_dollars << endl;
float flt_dollars = fl_dollars;
dollars = int(flt_dollars);
cout << "fl_dollars value is: " << flt_dollars << endl << " And
dollars are: " << levs << endl;
float cents_part = float(fl_dollars - dollars);
cents = cents_part;
cout << "Cents after conversion: " << cents << endl;
cout << "cents value is: " << cents<< endl;
cout << "\n 1-arg Constructor activated\n";
}
void ShowMoney()
{
cout <<"\nDistObject= " << dollars << " dollars" <<" and "<< cents
<< " cents" << endl << endl;
}
};
void main ()
{
// Conversion from Basic to User Defined >>> one-arg Constructor
Money d3 = 10.89;
d3.ShowMoney();
}
推荐答案
k3n3dy写道:
大家好,我想请任何人帮我理解
应该用这个问题做些什么。我已经提供了我现在所拥有的东西,虽然它不太准确。
[...]
class Money {
私人:美元; int cents;
public:Money(){dollars = 0;美分= 0; Money(int dol,int cnt){dollar = dol;美分= cnt; Money(float fl_dollars)//一个arg构造函数
{
cout<< fl_dollars值是: << fl_dollars<< endl;
float flt_dollars = fl_dollars;
dollar = int(flt_dollars);
cout<< fl_dollars值是: << flt_dollars<< endl<< "并且美元是: << levs<< ENDL;
我猜这里''levs''应该是''美元''这里......
float cents_part = float(fl_dollars - dollar);
美分=美分_部分;
如果''cents_part''是0.89(美元金额的一小部分),那多少美元是多少美分?你是怎么得到那个答案的?
cout<< 转换后的分数: <<美分<< endl;
cout<< 美分值是: <<美分<< endl;
cout<< " \\\
1-arg构造函数已激活\ n";
}
void ShowMoney()
{
cout<<" \ nDistObject =" <<美元<< "美元" <<"和<<分
<< "美分" << endl<< endl;
}
};
void main()
{
//从基本转换为用户定义>>> one-arg构造函数
钱d3 = 10.89;
d3.ShowMoney();
}
Hello everybody, I would like to ask anybody to help me understand
what should be done exactly with this Problem. I have provided what I
have up to now, although it is not quite accurate.
[..]
class Money {
private: int dollars; int cents;
public: Money() { dollars = 0; cents = 0; }
Money (int dol, int cnt) { dollars = dol; cents = cnt; }
Money (float fl_dollars) // one-arg constructor
{
cout << "fl_dollars value is: " << fl_dollars << endl;
float flt_dollars = fl_dollars;
dollars = int(flt_dollars);
cout << "fl_dollars value is: " << flt_dollars << endl << " And
dollars are: " << levs << endl;
I am guessing ''levs'' should be ''dollars'' here...
float cents_part = float(fl_dollars - dollars);
cents = cents_part;
If ''cents_part'' is 0.89 (the fraction of the dollar amount), how many
full cents is it? How did you get that answer?
cout << "Cents after conversion: " << cents << endl;
cout << "cents value is: " << cents<< endl;
cout << "\n 1-arg Constructor activated\n";
}
void ShowMoney()
{
cout <<"\nDistObject= " << dollars << " dollars" <<" and "<< cents
<< " cents" << endl << endl;
}
};
void main ()
{
// Conversion from Basic to User Defined >>> one-arg Constructor
Money d3 = 10.89;
d3.ShowMoney();
}
V
-
请在邮寄回复时从我的地址中删除资金
V
--
Please remove capital As from my address when replying by mail
是的我的坏它应该是美元而不是levs,我已经忘了用美元来改变它。
yes my bad it should have been dollars instead of levs, i have
forgotten to change it with dollars.
k3n3dy写道:
是的我不好它应该是美元而不是levs,我已经忘了用美元来改变它。
yes my bad it should have been dollars instead of levs, i have
forgotten to change it with dollars.
既然它是我回复了你原来的帖子,我知道你在说什么。然而,对于其他人来说,这可能是一个完全神秘的事。
请引用你回复的帖子的相关部分。
那么其他我的笔记?你明白了吗?
V
-
请在回复e时删除资金'A'邮件
我没有回复最热门的回复,请不要问
Since it was I who replied to your original post, I know what you''re
talking about. However, for anybody else it can be a total mystery.
Please quote relevant portion of the post you''re replying to.
And what about other notes of mine? Have you figured it out?
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
这篇关于通过使用两个int变量浮点到int转换来表示float变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!