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; }
int main() { int tw; CN cn(0, 0, 0, 0, "中国计重"); cin >> tw; cn.Convert(tw); cout << cn;
EN en(0, 0, 0, 0, "英国计重"); cin >> tw; en.Convert(tw); cout << en; cn = en; cout << cn; return 0; }
|