标准化计量单位 - 请帮忙 [英] Standardised unit of measure - please help

查看:75
本文介绍了标准化计量单位 - 请帮忙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在接近完成我的远程课程。在下面的代码中我

包含了许多测试相等性的重载运算符。

我还在TEST_DISTANCE驱动程序中添加了更多代码来测试代码。


我现在还有2个问题:


1.代码可以输入cm,m或km的距离

值;但是,将不同类型的值转换为b $ b(即cm和km)以便正确跟进处理并不够智能。我想在我的班级添加

的东西,它将接收三个输入中的任何一个,并将

转换为米,用于所有后续操作。

有人可以给我一些关于如何实现这个的例子。


2.我还想包括一个一元的操作员操作,这将是

返回距离值的负数。到目前为止,我所有的尝试只有减去b
。有人可以告诉我如何实施

这个。


谢谢


..h file < br $>
******************************

#ifndef DISTANCE_H


#define DISTANCE_H

#include< iostream>


using namespace std;


班级距离


{


公开:


距离(int,char); //构造函数 - 获取int和char值


距离(void); //默认 - 零


//访问会员功能


int number(void)const;


char measure(void)const;


//重载


距离& Distance :: operator =(Distance const& right_operand);


bool operator ==(距离const& rhs)const;


bool operator!=(距离const& rhs)const;


bool运算符< (距离const& rhs)const;


bool运算符< =(距离const& rhs)const;


bool运算符> ; (距离const& rhs)const;


bool运算符> =(距离const& rhs)const;


private:


int nu; //价值


char me; //度量单位(m)


};


//提供<<<"的重载便于展示


ostream&运营商LT;< (ostream&,const Distance&);


#endif

..cpp file

******* ************************


#include" Distance.h"


#include< iostream>


使用命名空间std;


/ * -------- ----------------------------------------------- * \


|成员职能的实施|


\ * ------------------------------ ------------------------- * /


//施工人员


距离::距离(int n,char m):nu(n),me(m){}


距离::距离(无效):nu(0 ),我(1){}


enum {cm,m,km};


//访问功能


int距离::数字(无效)const


{


返回nu;


}


char距离::度量(无效)const


{


返回给我;


}


//重载=运营商


距离& Distance :: operator =(距离const& right_operand)


{


nu = right_operand.nu;

me = right_operand.me;


返回* this;


}


//重载==运营商


bool Distance :: operator ==(距离const& rhs)const


{


return(nu == rhs.nu&& me == rhs.me);


}


//超载!=运算符


bool Distance :: operator!=(距离const& rhs)const


{


返回(nu!= rhs.nu&& me!= rhs.me);


}


//<重载运营商


bool距离::运营商< (距离const& rhs)const


{


return(nu< rhs.nu&& me< rhs。我);


}


//重载< =运算符


bool距离::运算符< =(距离const& rhs)const


{


return(nu< = rhs.nu& ;& me< = rhs.me);


}


//重载>运营商


bool距离::运营商> (距离const& rhs)const


{


return(nu> rhs.nu&& me> rhs。我);


}


//重载> =运算符


bool Distance :: operator> =(距离const& rhs)const


{


return(nu> = rhs.nu& ;& me> = rhs.me);


}


//提供重载<<<" ;便于展示


ostream&运营商LT;< (ostream& out,const Distance& d)


{


out<< "("<< d.number()<<""," ;;


开关(d.measure())


{


case cm:


out<<" cm";


休息;


案例m:


out<<" m";


休息;


案例公里:


out<<"" km";


休息;


}


out<<")" ;;


退出;


}


/ * ------------ ------------------------------------------- * \


|距离类的测试驱动程序|


\ * ---------------------------- --------------------------- * /


#ifdef TEST_DISTANCE // ....远程班....测试司机


int main(无效)


{


//创建测试输入


距离a =距离(6,cm);


距离b(4,km);


距离c(2,m);


距离d;


距离e(5,m );


距离z1 = a = b;


cout<< a<< endl<< b<< endl<< c<< endl<< d<< endl<< e<< endl<<

endl;


cout<< a<< endl<< endl;


cout<< 比较各种距离值的结果: << endl;


cout<< 距离a ==距离e:;


cout<< (a == e?" true":" false")<< endl;


cout<< 距离a!=距离e:;


cout<< (a!= e?" true":" false")<< endl;


cout<< 距离a<距离c:;


cout<< (a< c?" true":" false")<< endl;


cout<< 距离a< =距离c:;


cout<< (a< = c?" true":" false")<< endl;


cout<< 距离a>距离c:;


cout<< (a> c?" true":" false")<< endl;


cout<< 距离a> =距离b:" ;;


cout<< (a> = c?" true":" false")<< endl;


cin.ignore();


返回0; //正常终止


}


#endif


I''m now getting close to finishing my Distance class. In the code below I
have included a number of overload operators that test for equality etc.
I''ve also added more code in the TEST_DISTANCE driver to test the code.

I now have 2 remaining problems:

1. The code will allow for the input of distances in either cm, m or km
values; however, it isn''t smart enough to convert values of different types
(ie cm and km) for correct follow on processing. I would like to add
something to my class that will take any of the three inputs and convert
them to metres for all subsequent operations.

Could someone please give me some examples on how I can achive this.

2. I would also like to include a unary operator- operation which will
return the negative of a Distance value. All my attempts thus far have only
subtracted values. Could someone please advise me on how I must implement
this.

Thanks

..h file
******************************
#ifndef DISTANCE_H

#define DISTANCE_H

#include <iostream>

using namespace std;

class Distance

{

public :

Distance (int, char) ; // constructor - takes int and char values

Distance (void) ; // default - zero

//access member functions

int number (void) const;

char measure (void) const;

//overloads

Distance & Distance::operator= (Distance const & right_operand);

bool operator == ( Distance const &rhs ) const;

bool operator != ( Distance const &rhs ) const;

bool operator < ( Distance const &rhs ) const;

bool operator <= ( Distance const &rhs ) const;

bool operator > ( Distance const &rhs ) const;

bool operator >= ( Distance const &rhs ) const;

private :

int nu ; // the value

char me ; // the unit of measure (m)

} ;

// provide an overload of "<<" for easy display

ostream& operator<< (ostream&, const Distance&);

#endif
..cpp file
*******************************

#include "Distance.h"

#include <iostream>

using namespace std;

/*-------------------------------------------------------*\

| implementation of member functions |

\*-------------------------------------------------------*/

// constructors

Distance :: Distance (int n, char m) : nu(n), me(m) {}

Distance :: Distance (void) : nu(0), me(1) {}

enum {cm, m, km};

// access functions

int Distance :: number (void) const

{

return nu;

}

char Distance :: measure (void) const

{

return me;

}

// Overload of the "=" operator

Distance & Distance::operator= (Distance const & right_operand)

{

nu = right_operand.nu;

me = right_operand.me;

return *this;

}

// Overload of the "==" operator

bool Distance::operator == ( Distance const & rhs ) const

{

return ( nu == rhs.nu && me == rhs.me );

}

//Overload of the != operator

bool Distance::operator != ( Distance const & rhs ) const

{

return ( nu != rhs.nu && me != rhs.me );

}

//Overload of the < operator

bool Distance::operator < (Distance const & rhs) const

{

return ( nu < rhs.nu && me < rhs.me );

}

//Overload of the <= operator

bool Distance::operator <= (Distance const & rhs) const

{

return (nu <= rhs.nu && me <= rhs.me);

}

//Overload of the > operator

bool Distance::operator > (Distance const & rhs) const

{

return (nu > rhs.nu && me > rhs.me);

}

//Overload of the >= operator

bool Distance::operator >= (Distance const & rhs) const

{

return (nu >= rhs.nu && me >= rhs.me);

}

// provide an overload of "<<" for easy display

ostream& operator<< (ostream& out, const Distance& d)

{

out << "(" << d.number() << ", ";

switch (d.measure())

{

case cm:

out << "cm";

break;

case m:

out << "m";

break;

case km:

out << "km";

break;

}

out << ")";

return out;

}

/*-------------------------------------------------------*\

| test driver for the Distance class |

\*-------------------------------------------------------*/

#ifdef TEST_DISTANCE // .... Distance class .... test driver

int main (void)

{

// create test input

Distance a = Distance (6, cm);

Distance b (4, km);

Distance c (2, m);

Distance d;

Distance e (5, m);

Distance z1 = a = b;

cout << a << endl << b << endl << c << endl << d << endl << e << endl <<
endl;

cout << a <<endl << endl;

cout << "The results of comparing various Distance values :" << endl;

cout << "Distance a == Distance e : ";

cout << (a == e ? "true" : "false") << endl;

cout << "Distance a != Distance e : ";

cout << (a != e ? "true" : "false") << endl;

cout << "Distance a < Distance c : ";

cout << (a < c ? "true" : "false") << endl;

cout << "Distance a <= Distance c : ";

cout << (a <= c ? "true" : "false") << endl;

cout << "Distance a > Distance c : ";

cout << (a > c ? "true" : "false") << endl;

cout << "Distance a >= Distance b : ";

cout << (a >= c ? "true" : "false") << endl;

cin.ignore();

return 0; // normal termination

}

#endif


推荐答案

*" Chiller" < ... @ ...> schriebt:
* "Chiller" <...@...> schriebt:
我现在接近完成我的距离课程。在下面的代码中,我已经包含了许多测试相等性的重载运算符。
我还在TEST_DISTANCE驱动程序中添加了更多代码来测试代码。
我现在还有2个问题:

1.代码将允许输入cm,m或km
值的距离;然而,转换不同类型的值(即cm和km)以便正确跟进处理并不够智能。我想在课堂上添加一些内容,它将接收三个输入中的任何一个并将它们转换为米以用于所有后续操作。

有人可以给我一些例子吗关于如何实现这一点。

2.我还想包括一个一元的操作员操作,它将返回一个距离值的负数。到目前为止,我所有的尝试都只有减去值。有人可以告诉我如何实施这个。

谢谢

.h文件
********** ********************
#ifndef DISTANCE_H

#define DISTANCE_H

#include<的iostream>


不要在头文件中包含任何你不需要的东西。 <&的iostream GT;这是一个很重的东西,使用你的课程的程序可能不需要。

大概你认为你需要为你的重载运算符<<<,但是( 1)

你不需要超载,只需提供一个toString函数,(2)如果

你发现你毕竟需要那个重载,提供它通过单独的

头文件(标准库应该为例如字符串完成)和

(3)如果你甚至不能容忍单独的头文件,至少考虑

使用转发标头而不是完整的< iostream>标题。

使用命名空间std;
I''m now getting close to finishing my Distance class. In the code below I
have included a number of overload operators that test for equality etc.
I''ve also added more code in the TEST_DISTANCE driver to test the code.

I now have 2 remaining problems:

1. The code will allow for the input of distances in either cm, m or km
values; however, it isn''t smart enough to convert values of different types
(ie cm and km) for correct follow on processing. I would like to add
something to my class that will take any of the three inputs and convert
them to metres for all subsequent operations.

Could someone please give me some examples on how I can achive this.

2. I would also like to include a unary operator- operation which will
return the negative of a Distance value. All my attempts thus far have only
subtracted values. Could someone please advise me on how I must implement
this.

Thanks

.h file
******************************
#ifndef DISTANCE_H

#define DISTANCE_H

#include <iostream>
Don''t include anything you don''t need in a header file. <iostream> is a very
heavy thing, which a program using your class will perhaps not need.
Presumably you think you need that for your overloaded operator <<, but (1)
you don''t need that overload, simply provide a toString function, and (2) if
you find that you need that overload after all, provide it via a separate
header file (which the standard library should have done for e.g. string), and
(3) if you cannot even tolerate a separate header file, at least consider
using the forwarding header instead of the full <iostream> header.
using namespace std;




哇!停在那儿!不要,我再说一遍,不要,把使用命名空间

std;在头文件中。这将导致客户端的各种问题

代码,例如使用自定义类字符串的客户端代码。


嗯,其余的你代码基于不合理的设计理念。


请考虑

类AbstractDistance ...

class Meter:public AbstractDistance .. 。

class KiloMeter:public AbstractDistance ...

class CentiMeter:public AbstractDistance ...

其中AbstractDistance提供常用的措施,例如以米为单位,但

未知客户代码。


-

答:因为它弄乱了人们的订单通常阅读文字。

问:为什么顶级发布这么糟糕的事情?

A:热门发布。

问:什么是usenet和电子邮件中最烦人的事情是?



Whoa! Stop right there! DO NOT, I repeat, DO NOT, put "using namespace
std;" in a header file. That will cause all sorts of problems for client
code, for example client code using a custom class "string".

Uhm, the rest of your code is based on unsound design idea.

Consider instead
class AbstractDistance ...
class Meter: public AbstractDistance ...
class KiloMeter: public AbstractDistance ...
class CentiMeter: public AbstractDistance ...
where AbstractDistance provides the common measure e.g. in meters, but
unknown for client code.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


*" Chiller" < ... @ ...> schriebt:
* "Chiller" <...@...> schriebt:
我现在接近完成我的距离课程。在下面的代码中,我已经包含了许多测试相等性的重载运算符。
我还在TEST_DISTANCE驱动程序中添加了更多代码来测试代码。
我现在还有2个问题:

1.代码将允许输入cm,m或km
值的距离;然而,转换不同类型的值(即cm和km)以便正确跟进处理并不够智能。我想在课堂上添加一些内容,它将接收三个输入中的任何一个并将它们转换为米以用于所有后续操作。

有人可以给我一些例子吗关于如何实现这一点。

2.我还想包括一个一元的操作员操作,它将返回一个距离值的负数。到目前为止,我所有的尝试都只有减去值。有人可以告诉我如何实施这个。

谢谢

.h文件
********** ********************
#ifndef DISTANCE_H

#define DISTANCE_H

#include<的iostream>


不要在头文件中包含任何你不需要的东西。 <&的iostream GT;这是一个很重的东西,使用你的课程的程序可能不需要。

大概你认为你需要为你的重载运算符<<<,但是( 1)

你不需要超载,只需提供一个toString函数,(2)如果

你发现你毕竟需要那个重载,提供它通过单独的

头文件(标准库应该为例如字符串完成)和

(3)如果你甚至不能容忍单独的头文件,至少考虑

使用转发标头而不是完整的< iostream>标题。

使用命名空间std;
I''m now getting close to finishing my Distance class. In the code below I
have included a number of overload operators that test for equality etc.
I''ve also added more code in the TEST_DISTANCE driver to test the code.

I now have 2 remaining problems:

1. The code will allow for the input of distances in either cm, m or km
values; however, it isn''t smart enough to convert values of different types
(ie cm and km) for correct follow on processing. I would like to add
something to my class that will take any of the three inputs and convert
them to metres for all subsequent operations.

Could someone please give me some examples on how I can achive this.

2. I would also like to include a unary operator- operation which will
return the negative of a Distance value. All my attempts thus far have only
subtracted values. Could someone please advise me on how I must implement
this.

Thanks

.h file
******************************
#ifndef DISTANCE_H

#define DISTANCE_H

#include <iostream>
Don''t include anything you don''t need in a header file. <iostream> is a very
heavy thing, which a program using your class will perhaps not need.
Presumably you think you need that for your overloaded operator <<, but (1)
you don''t need that overload, simply provide a toString function, and (2) if
you find that you need that overload after all, provide it via a separate
header file (which the standard library should have done for e.g. string), and
(3) if you cannot even tolerate a separate header file, at least consider
using the forwarding header instead of the full <iostream> header.
using namespace std;




哇!停在那儿!不要,我再说一遍,不要,把使用命名空间

std;在头文件中。这将导致客户端的各种问题

代码,例如使用自定义类字符串的客户端代码。


嗯,其余的你代码基于不合理的设计理念。


请考虑

类AbstractDistance ...

class Meter:public AbstractDistance .. 。

class KiloMeter:public AbstractDistance ...

class CentiMeter:public AbstractDistance ...

其中AbstractDistance提供常用的措施,例如以米为单位,但

未知客户代码。


-

答:因为它弄乱了人们的订单通常阅读文字。

问:为什么顶级发布这么糟糕的事情?

A:热门发布。

问:什么是usenet和电子邮件中最令人讨厌的事情是什么?



Whoa! Stop right there! DO NOT, I repeat, DO NOT, put "using namespace
std;" in a header file. That will cause all sorts of problems for client
code, for example client code using a custom class "string".

Uhm, the rest of your code is based on unsound design idea.

Consider instead
class AbstractDistance ...
class Meter: public AbstractDistance ...
class KiloMeter: public AbstractDistance ...
class CentiMeter: public AbstractDistance ...
where AbstractDistance provides the common measure e.g. in meters, but
unknown for client code.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?




" Chiller" < ... @ ...>在消息中写道

新闻:f5 ****************************** @ news.teranew s。 com ...

"Chiller" <...@...> wrote in message
news:f5******************************@news.teranew s.com...
我现在接近完成我的距离课程。在下面的代码中,我已经包含了许多测试相等性的重载运算符。
我还在TEST_DISTANCE驱动程序中添加了更多代码来测试代码。

您在下面的代码中有一些错误。事实上*所有*比较

运算符都是错误的。


考虑以下程序


int main( )

{

距离one_km(1,km);

距离one_thousand_m(1000,m);

if(one_km == one_thousand_m)

cout<< 1km等于1000m \ n

其他

cout<< 1km不等于1000m \ n

}


使用你的代码,这打印错误''1km不等于1000m''。你需要

重写你的算子==所以它可以处理不同的单位。对于其他比较运算符,所有

也是如此。编写简单的程序非常容易,而b $ b表明他们计算错误的东西。我认为问题的一部分是

,你要求这个小组提供太多帮助,而不是想你自己足够好。有时人们会给你答案,而不是真正理解你想要实现的目标而你只是盲目地接受它们。

我现在有了剩下的2个问题:

1.代码将允许输入cm,m或km
值的距离;然而,转换不同的
类型(即cm和km)的值以进行正确的后续处理并不够聪明。我想在我的课程中添加一些内容,它将采用三个输入中的任何一个,并将它们转换为米,用于所有后续操作。


是的,你应该为运营商==和所有其他运营商做这件事。


但是这真的有多难?你知道如何从

公里和厘米转换成米,什么阻止你编写代码?


int Distance :: to_metres()const

{

//你的代码在这里

}


当你写完这个函数时,你可以在运营商中使用它==等。

就像我上面解释过的那样。

有人可以给我一些关于如何实现这一点的例子。


我认为你要求太多帮助而不学习任何东西。为了您自己的利益,尝试找出所需的非常简单的代码

以上是



2.我还想包括一元算子操作,它将返回距离值的负值。到目前为止我所有的尝试都只有
减去的值。有人可以告诉我如何实施这个。
I''m now getting close to finishing my Distance class. In the code below I
have included a number of overload operators that test for equality etc.
I''ve also added more code in the TEST_DISTANCE driver to test the code.

You have some mistakes in the code below. In fact *all* the comparison
operators are wrong.

Consider the following program

int main()
{
Distance one_km(1, km);
Distance one_thousand_m(1000, m);
if (one_km == one_thousand_m)
cout << "1km equals 1000m\n"
else
cout << "1km does not equal 1000m\n"
}

With your code this prints wrongly ''1km does not equal 1000m''. You need to
rewrite your operator== so it can handle different units. Same goes for all
the other comparison operators. It''s very easy to write simple programs that
show that they calculate the wrong things. I think part of the problem is
that you are asking for too much help from this group and not thinking for
yourself enough. Some times people give you answers without really
understanding what you are trying to achieve and you are just blindly
accepting them.
I now have 2 remaining problems:

1. The code will allow for the input of distances in either cm, m or km
values; however, it isn''t smart enough to convert values of different types (ie cm and km) for correct follow on processing. I would like to add
something to my class that will take any of the three inputs and convert
them to metres for all subsequent operations.
Yes, you should do this for the operator== and all the other operators.

But seriously how hard is this? You know how to convert to meters from
kilometres and centimetres, what''s stopping you writing the code?

int Distance::to_metres() const
{
// your code here
}

When you have written this function, you can then use it in operator== etc.
like I explained above.

Could someone please give me some examples on how I can achive this.
I think you are asking for too much help and not learning anything. It is
for your own benefit to try and work out the very simple code required
above.

2. I would also like to include a unary operator- operation which will
return the negative of a Distance value. All my attempts thus far have only subtracted values. Could someone please advise me on how I must implement
this.




听起来像是在写这个


距离距离:: operator-(const Distance& rhs)const


当你应该写这个


距离距离:: operator-( )const


第一个是二元运算符 - 第二个是一元运算符 - 。但是真的

最好的办法就是向你展示你写的代码。


john



Sounds like you are writing this

Distance Distance::operator-(const Distance& rhs) const

when you should be writing this

Distance Distance::operator-() const

The first is a binary operator-, the second is a unary operator-. But really
the best thing is for you to show us the code you have written.

john


这篇关于标准化计量单位 - 请帮忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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