YXTS122 于 2016.02.19 18:22 提问

5个回答

caozhy      2016.02.20 11:15

`````` #include <iostream>
using namespace std;

class Stonewt
{
private:
int stone;
double pounds;
public:
Stonewt()
{
stone = 0;
pounds = 0.0;
}
Stonewt(double lbs)
{
stone = (int)lbs / 14;
pounds = lbs - ((int)lbs / 14 * 14);
}
Stonewt(int stn, double lbs)
{
stone = stn + (int)lbs / 14;
pounds = lbs - ((int)lbs / 14 * 14);
}
void show()
{
cout << stone << "stn " << pounds << "lbs." << endl;
}
bool operator > (const Stonewt &s)
{
if (stone == s.stone)
return pounds > s.pounds;
else
return stone > s.stone;
}
bool operator < (const Stonewt &s)
{
if (stone == s.stone)
return pounds < s.pounds;
else
return stone < s.stone;
}
bool operator == (const Stonewt &s)
{
if (stone == s.stone)
return pounds == s.pounds;
else
return false;
}
bool operator >= (const Stonewt &s)
{
return *this > s || *this == s;
}
bool operator <= (const Stonewt &s)
{
return *this < s || *this == s;
}
bool operator != (const Stonewt &s)
{
return !(*this == s);
}
};

int main()
{
Stonewt arr[6] = { Stonewt(28.5), Stonewt(50.1), Stonewt(1, 2.1) };
arr[3] = Stonewt(1, 20.0);
arr[4] = Stonewt(12, 0.4);
arr[5] = Stonewt(12, 0.6);
Stonewt max = arr[0];
Stonewt min = arr[0];
int n = 0;
Stonewt c = Stonewt(11, 0.0);
for (int i = 0; i < 6; i++)
{
arr[i].show();
if (arr[i] > max) max = arr[i];
if (arr[i] < min) min = arr[i];
if (arr[i] >= c) n++;
}
cout << "max "; max.show();
cout << "mix "; min.show();
cout << "> 11 stone: " << n << endl;
return 0;
}
``````
caozhy      2016.02.19 22:37

stn和lbs分别是什么，Stonewt对象判断的依据是什么，这个题目无非就是要求你重载> >= < <= == !=6个运算符。

caozhy 回复YXTS122: 刚醒来，我知道了，稍等

YXTS122 回复caozhy:这道题要怎么编啊？

YXTS122 回复caozhy:stn就是英石，pounds就是磅,比如15磅就是1英石+1磅，就是些单位转换

caozhy      2016.02.20 11:15

2stn 0.5lbs.
3stn 8.1lbs.
1stn 2.1lbs.
2stn 6lbs.
12stn 0.4lbs.
12stn 0.6lbs.
max 12stn 0.6lbs.
mix 1stn 2.1lbs.

11 stone: 2
Press any key to continue

qq_33971941   2016.02.19 23:06

mengyin521   2016.02.20 23:40

Stonew类 内部重载需要用到的 运算符 运算符