计重转换

1.题目描述

目前国际计重最基本的单位是克。在古代各个国家的计重单位是不同的。

中国使用斤、两、钱来表示重量,其中1斤=10两,1两=10钱

中国计重单位与克的关系为:1斤=500克,1两=50克,1钱=5克

英国使用磅、盎司、打兰来表示重量,其中1磅=16盎司,1盎司=16打兰

英国计重单位与克的关系为:1磅=512克,1盎司=32克,1打兰=2克

以下参考代码包含了抽象类Weight,中国计重和英国计重都继承了抽象类。

中国计重类新增了斤、两、钱三个属性,并新增了一个操作:计重转换Convert。

Convert能够把输入的克数转成中国计重,例如1234克转成2斤4两6钱4克,并且把数值放入斤、两、钱、克四个属性中

英国计重类新增了磅、盎司、打兰三个属性,并新增了两个操作:

1、计重转换Convert,功能与上述类似,例如2345克转成4磅9盎司4打兰1克,并且把数值放入对应的四个属性中

2、计重等价,重载类型转换运算符,实现将英国计重类的对象转换成中国计重类的对象,例如英国计重类对象en(2磅2盎司11打兰1克)等价于(转换成)中国计重类对象cn(2斤2两2钱1克)。

2.代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;

class CN; //提前声明
class EN; //提前声明

// 抽象类
class Weight
{
protected:
char kind[20]; //计重类型
int gram; // 克
public:
Weight(const char tk[] = "no name", int tg = 0)
{
strcpy(kind, tk);
gram = tg;
}
virtual void print(ostream& out) = 0; // 输出不同类型的计重信息
};

// 中国计重
class CN : public Weight
{
private:
int jin;
int liang;
int qian;
public:
CN(int j, int l, int q, int g, const char* k) :jin(j), liang(l), qian(q), Weight(k, g) {}
void Convert(int g)
{
jin = g / 500;
g -= jin * 500;
liang = g / 50;
g -= liang * 50;
qian = g / 5;
g -= qian * 5;
gram = g;
}
void print(ostream& out)
{
out << *this;
}
friend ostream& operator<<(ostream& os, const CN& res);

};

// 英国计重
class EN : public Weight
{
protected:
int bang;
int si;
int lan;
public:
EN(int b, int s, int l, int g, const char* k) :bang(b), si(s), lan(l), Weight(k, g) {}
void Convert(int g)
{
bang = g / 512;
g -= bang * 512;
si = g / 32;
g -= si * 32;
lan = g / 2;
g -= lan * 2;
gram = g;
}
operator CN() const {
int totalGrams = gram;
totalGrams += lan * 2;
totalGrams += si * 32;
totalGrams += bang * 512;

int jin = totalGrams / 500;
totalGrams %= 500;
int liang = totalGrams / 50;
totalGrams %= 50;
int qian = totalGrams / 5;
totalGrams %= 5;

return CN(jin, liang, qian, totalGrams, "中国计重");
}
void print(ostream& out)
{
out << *this;
}
friend ostream& operator<<(ostream& os, const EN& res);
};
ostream& operator<<(ostream& os, const CN& res)
{
os << "中国计重:" << res.jin << "斤" << res.liang << "两" << res.qian << "钱" << res.gram << "克" << endl;
return os;
}
ostream& operator<<(ostream& os, const EN& res)
{
os << "英国计重:" << res.bang << "磅" << res.si << "盎司" << res.lan << "打兰" << res.gram << "克" << endl;
return os;
}
// 以全局函数方式重载输出运算符,代码3-5行....自行编写
// 重载函数包含两个参数:ostream流对象、Weight类对象,参数可以是对象或对象引用
// 重载函数必须调用参数Weight对象的print方法

// 主函数
int main()
{
int tw;
// 创建一个中国计重类对象cn
// 构造参数对应斤、两、钱、克、类型,其中克和类型是对应基类属性gram和kind
CN cn(0, 0, 0, 0, "中国计重");
cin >> tw;
cn.Convert(tw); // 把输入的克数转成中国计重
cout << cn;

// 创建英国计重类对象en
// 构造参数对应磅、盎司、打兰、克、类型,其中克和类型是对应基类属性gram和kind
EN en(0, 0, 0, 0, "英国计重");
cin >> tw;
en.Convert(tw); // 把输入的克数转成英国计重
cout << en;

// 使用类型转换运算符 自动调用 EN 的类型转换运算符
cn = en; // 把英国计重转成中国计重
cout << cn;
return 0;
}

类型转换运算符的应用