【c++oj] 宠物的生长(虚函数和多态)

题目描述

需要开发一个系统,对宠物的生长状态进行管理。给出下面的基类框架:

class Pet

{ protected:

string name;//姓名

float length;//身长

float weight;//体重

CDate current;//开始记录时间

(日期类CDate包含年、月、日三个私有数据,其他方法根据需要自拟。)

public:

virtual void display(CDate day)=0;//输出目标日期时宠物的身长和体重

}

以Pet为基类,构建出Cat和Dog两个类:

Cat一天身长加0.1,体重加0.2。

Dog一天身长加0.2,体重加0.1。

生成上述类并编写主函数,要求主函数中有一个基类指针Pet *pt,用于测试子类数据。

主函数根据输入的信息,相应建立Cat类对象或Dog类对象,并给出测量日期时宠物的身长和体重。

输入

第一行为测试次数

第二行是开始记录日期

从第三行起,每个测试用例占一行,每行给出宠物的基本信息:宠物的类型(1为Cat,2为Dog)、名字、身长、体重、最后测量的日期。

输出

要求输出目标日期宠物姓名、身长和体重(结果要求保留小数点后2位)。若测量日期小于开始日期,输出”error”。

IO模式

本题IO模式为标准输入/输出(Standard IO),你需要从标准输入流中读入数据,并将答案输出至标准输出流中。

样例:

输入:

3
2019 5 5
1 tony 10 10 2018 12 30
2 jerry 5 6 2019 5 10
1 tom 3 4 2019 6 1
输出:

error
jerry after 5 day: length=6.00,weight=6.50
tom after 27 day: length=5.70,weight=9.40

AC代码:

#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;

class CDate {
	protected:
		int y, m, d;
	public:
		CDate(int yy, int mm, int dd): y(yy), m(mm), d(dd) {
		}
		friend int getday(CDate &a, CDate &b);


};

class Pet {
	protected:
		string name;
		float length;
		float weight;
		CDate current;
	public:
		virtual void display(CDate day) = 0;
		Pet(string n, float l, float w, CDate c): name(n), length(l), weight(w), current(c) {
		}
};



class Cat: public Pet {
	public:
		virtual void display(CDate day) {
			int fd = getday(day, current);
			if (fd < 0) {
				cout << "error" << endl;
			} else {
				length += 0.1 * fd;
				weight += 0.2 * fd;
				cout << name << " after " << fd << fixed << setprecision(2) << " day: length=" << length << ",weight=" << weight <<
				     endl;
			}

		}
		Cat(string n, float l, float w, CDate c): Pet(n, l, w, c) {
		}
};

class Dog: public Pet {
	public:
		virtual void display(CDate day) {
			int fd = getday(day, current);
			if (fd < 0) {
				cout << "error" << endl;
			} else {
				length += 0.2 * fd;
				weight += 0.1 * fd;
				cout << name << " after " << fd << fixed << setprecision(2) << " day: length=" << length << ",weight=" << weight <<
				     endl;
			}

		}
		Dog(string n, float l, float w, CDate c): Pet(n, l, w, c) {
		}
};

int getday(CDate &a, CDate &b) {
	if (a.y < b.y) {
		return -1;
	}
	if (a.y == b.y && a.m < b.m) {
		return -1;
	}
	if (a.y == b.y && a.m == b.m & a.d < b.d) {
		return -1;
	}
	int ans = 0;
	int i, year[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	if (a.y % 400 == 0 || a.y % 4 == 0 && a.y % 100 != 0) {
		year[1] = 28;
	} else {
		year[1] = 29;
	}
	for (i = 0; i < a.m - 1; i++) {
		ans = ans + year[i];
	}
	ans += a.d;
	if (a.y == b.y) {
		for (i = 0; i < b.m - 1; i++) {
			ans -= year[i];
		}
		ans -= b.d;
	} else {
		for (i = b.y + 1; i < a.y; i++) {
			if (i % 400 == 0 || i % 4 == 0 && i % 100 != 0) {
				ans += 366;
			} else {
				ans += 365;
			}
		}
		for (i = b.m; i < 12; i++) {
			ans += year[i];
		}
		ans = ans + year[b.m - 1] - b.d;
	}
	return ans;
}

int main() {
	int t, k, l, w, y, m, d;
	string name;
	cin >> t;
	Pet *pt;
	cin >> y >> m >> d;
	CDate c(y, m, d);
	while (t--) {
		cin >> k >> name >> l >> w >> y >> m >> d;
		CDate n(y, m, d);
		if (k == 1) {
			pt = new Cat(name, l, w, c);
			pt->display(n);
		} else {
			pt = new Dog(name, l, w, c);
			pt->display(n);
		}
	}
}

  • 11
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Shiroha Wang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值