前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >生日打折(复合类构造)

生日打折(复合类构造)

作者头像
叶茂林
发布2023-07-30 11:08:41
1470
发布2023-07-30 11:08:41
举报
文章被收录于专栏:叶子的开发者社区

题目描述

定义一个日期类Date,包含数据成员year\month\day,还包含构造函数及其他函数(根据需要自己添加)

定义一个会员类VIP,包含数据成员id和birth,其中id是整数表示会员编号;birth是Date类型表示生日。

类VIP包含构造函数和其他函数(根据需要自己添加),还包含一个折扣函数Discount。函数Discount返回结果为浮点数表示折扣,函数包含1个参数为日期类型,函数功能是判断参数日期是否会员生日,是则折扣为0.5,不是则折扣为0.95

编写程序实现上述类功能并实现输入输出的要求

输入

第一行输入年、月、日,表示今天日期

第二行输入t表示有t个会员

第三行输入第1个会员的ID、生日的年、月、日

第四行输入第1个会员的消费金额

依次类推输入下一个会员的两行数据.....

输出

根据会员的消费金额,调用Discount函数判断今天是否会员生日并得到折扣,然后计算会员打完折的消费金额

每一行先输出会员编号,再输出会员打完折的消费金额,消费金额只需要输出整数部分

提示把浮点数转整数

double x = 123.456

cout<<int(x)<<endl;

输入样例1

2017 4 20 2 1111 2000 4 20 136 2222 2000 3 30 125

输出样例1

1111's consumption is 68 2222's consumption is 118

AC代码

代码语言:javascript
复制
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
class Date{
	int year,month,day;
	public:
		void datain(){cin>>year>>month>>day;}
		int isbirth(int month,int day){
			if(month==this->month&&day==this->day)
			return 1;
			return 0;
		}
};
class VIP{
	int id,consumption;
	Date date;
	public:
	void Discount(int month,int day){
		cin>>id;
		date.datain();
		cin>>consumption;
		if(date.isbirth(month,day)){
			cout<<id<<"'s consumption is "<<int(0.5*consumption)<<endl;
		}else{
			cout<<id<<"'s consumption is "<<int(0.95*consumption)<<endl;
		}
	}
};
int main() {
	int t,year,month,day;
	cin >>year>>month>>day>> t;
	while (t--) {
		VIP vip;
		vip.Discount(month,day);
	}
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-09-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目描述
  • AC代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档