【id:128】【20分】E. 五、计重转换(期末模拟)

注意!!!本AC代码由 AI ChatGpt 生成,仅供参考!!!

题目描述

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

中国使用斤、两、钱来表示重量,其中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克)。

程序要求如下

1、参考代码框架不能做任何修改,在要求的地方添加代码

2、主函数不能有任何修改

以上数据纯粹为题目设计,方便计算,实际换算数据是不同的。

上述所有类属性都不是public,用面向对象思想和C++语言实现上述要求

----参考代码----

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
{
	// ....类定义自行编写
};

// 英国计重
class EN : public Weight
{
	// ....类定义自行编写
};

// 以全局函数方式重载输出运算符,代码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;
	cn = en; // 把英国计重转成中国计重
	cout << cn;
	return 0;
}

输入

第一行输入一个克数,调用中国计重转换,把克数转成中国计重

第二行输入一个克数,调用英国计重转换,把克数转成英国计重,并调用计重等价把英国计重转成中国计重

输出

根据主函数运行输出

输入样例1

1234
2345

输出样例1

中国计重:2斤4两6钱4克
英国计重:4磅9盎司4打兰1克
中国计重:4斤6两9钱0克


A C 代 码

//注:VS中运行需在第一行加上#define _CRT_SECURE_NO_WARNINGS
//注:提交oj系统则无需加上,删去即可
#include <iostream>
#include <cstring>
using namespace std;

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) const = 0;
};

class CN : public Weight
{
    int jin, liang, qian;
public:
    CN(int j = 0, int l = 0, int q = 0, int g = 0, const char tk[] = "中国计重") : Weight(tk, g)
    {
        jin = j;
        liang = l;
        qian = q;
    }

    void print(ostream& out) const override
    {
        out << kind << ":" << jin << "斤" << liang << "两" << qian << "钱" << gram << "克" << endl;
    }

    void Convert(int grams)
    {
        jin = grams / 500;
        grams %= 500;
        liang = grams / 50;
        grams %= 50;
        qian = grams / 5;
        gram = grams % 5;
    }
};

class EN : public Weight
{
    int pound, ounce, drachm;
public:
    EN(int p = 0, int o = 0, int d = 0, int g = 0, const char tk[] = "英国计重") : Weight(tk, g)
    {
        pound = p;
        ounce = o;
        drachm = d;
    }

    void print(ostream& out) const override
    {
        out << kind << ":" << pound << "磅" << ounce << "盎司" << drachm << "打兰" << gram << "克" << endl;
    }

    void Convert(int grams)
    {
        pound = grams / 512;
        grams %= 512;
        ounce = grams / 32;
        grams %= 32;
        drachm = grams / 2;
        gram = grams % 2;
    }

    operator CN() const
    {
        int totalGrams = pound * 512 + ounce * 32 + drachm * 2 + gram;
        CN cn;
        cn.Convert(totalGrams);
        return cn;
    }
};
ostream& operator<<(ostream& out, const Weight& obj)
{
    obj.print(out);
    return out;
}

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值