首页 > 其他分享 >第十七天第一个问题

第十七天第一个问题

时间:2023-05-09 21:22:39浏览次数:27  
标签:case 第一个 int month 问题 year 第十七 day cout

问题描述:

6-3 【CPP0005】定义日期类Date

定义一个日期类Date,main()函数完成对其的测试。

Date类结构说明:

 
Date类的数据成员包括:
①私有数据成员:年year(int型),月month(int型),日day(int型)。
Date类成员函数包括:
①定义有参构造函数Date(int ,int ,int )和拷贝构造函数Date(Date &),其中有参构造函数参数默认值为1,输出信息“Constructor run”,拷贝构造函数输出信息“CopyConstructor run”
②定义析构函数,析构函数输出信息“Destructor run”
③公有函数成员:void  setYear(int )和int  getYear()分别设置和返回year
④公有函数成员:void  setMonth(int )和int  getMonth()分别设置和返回month
⑤公有函数成员:void  setDay(int )和int  getday()分别设置和返回day
⑥公有函数成员:void  tomorrow( )和void  yesterday( )实现日期加/减一天操作
⑦公有函数成员:void  printMonthCalendar( )打印当月日历,格式如下(每列宽度为3位,右对齐)(以2016年3月为例)
SunMonTueWedThuFriSat
        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
⑧公有函数成员:void  chineseFormat( )和void  americaformat( )分别显示中式日期(例:2014年4月21日)和美式日期(例:Api 21, 2014),其中美式格式中1~12月分别用Jan、Feb、Mar、Api、May、Jun、Jul、Aug、Sep、Oct、Nov、Dec表示
⑨私有函数成员:int  isLeapYear( )确定当前年份是否为闰年,闰年返回1,否则返回0;
⑩公有函数成员:int  weekDay( )返回该天是星期几,0~6分别表示Sun、Mon、Tue、Wed、Thu、Fri、Sat
 

注:在以上成员函数执行过程中,若输入的日期参数值超界,则按照最接近输入参数的有效日期值对待,例如:

假定输入的month>12,则按照12对待,若输入的month<1,则按照1对待;

假定当前month为12月,则若输入的day>31,则按照31对待,若输入的day<1,则按照1对待;

提示:cout.width(int)设置输出项的宽度

裁判测试程序样例:

 
#include<iostream>
using namespace std;
int const monthDay[12]={31,28,31,30,31,30,31,31,30,31,30,31};
char* const weekName[7]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
char* const monthName[12]={"Jan","Feb","Mar","Api","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};

/*请在这里填写答案*/

int main(){
    int year,month,day;
    Date d1;
    Date d2(d1);
    cin>>year>>month>>day;
    d1.setYear(year);
    d1.setMonth(month);
    d1.setDay(day);
    d1.yesterday();
    d1.chineseFormat();
    cin>>year>>month>>day;
    d2.setYear(year);
    d2.setMonth(month);
    d2.setDay(day);
    d2.tomorrow();
    d2.americaformat();
    d2.printMonthCalendar();
    return 0;
}
 

输入样例:

2019 3 27
2016 2 31
 

输出样例:

Constructor run
CopyConstructor run
2019年3月26日
Mar 1,2016
SunMonTueWedThuFriSat
        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
Destructor run
Destructor run
代码:

#include<iomanip>
class Date
{
public:
Date(int a=1,int b=1,int c=1)
{
year=a;
month=b;
day=c;
cout<<"Constructor run"<<endl;
}
Date(Date &d)
{
year=d.year;
month=d.month;
day=d.day;
cout<<"CopyConstructor run"<<endl;
}
~Date()
{
cout<<"Destructor run"<<endl;
}
void setYear(int a)
{
year=a;
}
int getYear()
{
return year;
}
void setMonth(int b)
{
if(b<1)
b=1;
if(b>12)
b=12;
month=b;
}
int getMonth()
{
return month;
}
void setDay(int c)
{
{
if(c<1)
c=1;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)//根据不同的月份,来修正错误输入
{if(c>31) c=31;}
else
if(month==4||month==6||month==9||month==11)
{if(c>30) c=30;}
else
{if(isLeapYear(year)) {if(c>29) c=29;} else {if(c>28) c=28;}}
}
day=c;
}
int getDay()
{
return day;
}
void tomorrow( )
{
day++;
switch(month)
{
case 1:if(day>31) {month++,day=1;};break;
case 2:if(isLeapYear(year)){if(day>29){month++,day=1;}} else {if(day>28){month++,day=1;}}break;
case 3:if(day>31) {month++,day=1;};break;
case 4:if(day>30) {month++,day=1;};break;
case 5:if(day>31) {month++,day=1;};break;
case 6:if(day>30) {month++,day=1;};break;
case 7:if(day>31) {month++,day=1;};break;
case 8:if(day>31) {month++,day=1;};break;
case 9:if(day>30) {month++,day=1;};break;
case 10:if(day>31) {month++,day=1;};break;
case 11:if(day>30) {month++,day=1;};break;
case 12:if(day>31) {year++,month=1,day=1;};break;
}
}
void yesterday( )
{
day--;
switch(month)//如果day--后day小于1,则说明yesterday是上月的最后一天
{
case 1:if(day<1) {year--,month=12,day=31;};break;
case 2:if(day<1) {month--,day=31;};break;
case 3:if(isLeapYear(year)){if(day<1){month--,day=29;}} else {if(day<1){month--,day=28;}};break;
case 4:if(day<1) {month--,day=31;};break;
case 5:if(day<1) {month--,day=30;};break;
case 6:if(day<1) {month--,day=31;};break;
case 7:if(day<1) {month--,day=30;};break;
case 8:if(day<1) {month--,day=31;};break;
case 9:if(day<1) {month--,day=31;};break;
case 10:if(day<1) {month--,day=30;};break;
case 11:if(day<1) {month--,day=31;};break;
case 12:if(day<1) {month--,day=30;};break;
}
}
void printMonthCalendar( )
{
int i,j,k;
int const rili[31]={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};
k=weekDay();
j=weekDay();
cout<<"SunMonTueWedThuFriSat"<<endl;
while(k>=1&&k<=6)
{
cout<<" ";
k--;
}
if(j!=0)
{
for(i=0;i<28;i++)
{
if(i%7==7-j)
cout<<endl;
cout<<setw(3)<<rili[i];
}
if(month==14&&isLeapYear(year+1)==1)
{
if(i%7==7-j)
{cout<<endl;}
cout<<setw(3)<<rili[28];
}
if(month==13||month==3||month==5||month==7||month==8||month==10||month==12)//打印天数为31天的月份的结尾
{
for( ;i<31;i++)
{
if(i%7==7-j)
cout<<endl;
cout<<setw(3)<<rili[i];
}
}
if(month==4||month==6||month==9||month==11)
{
for( ;i<30;i++)
{
if(i%7==7-j)
cout<<endl;
cout<<setw(3)<<rili[i];
}
}
cout<<endl;
}
else
{
for(i=0;i<28;i++)
{
cout<<setw(3)<<rili[i];
if(i%7==6&&i!=27)
cout<<endl;
}
if(month==14&&isLeapYear(year+1)==1)
{
cout<<endl;
cout<<setw(3)<<rili[28];
}

if(month==13||month==3||month==5||month==7||month==8||month==10||month==12)
{
for( ;i<31;i++)
{
if(i%7==0)
cout<<endl;
cout<<setw(3)<<rili[i];
}
}
if(month==4||month==6||month==9||month==11)
{
for( ;i<30;i++)
{
if(i%7==0)
cout<<endl;
cout<<setw(3)<<rili[i];
}
}
cout<<endl;
}
}
void chineseFormat( )
{
cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
}
void americaformat( )
{
cout<<monthName[month-1]<<' '<<day<<','<<year<<endl;
}
int weekDay( )
{
int i;
if(month==1)
{
year--;
month=13;
}
if(month==2)
{
year--;
month=14;
}
i=(day+2*month+3*(month+1)/5+year+year/4-year/100+year/400)%7;
switch(i)
{
case 0:return 1;break;
case 1:return 2;break;
case 2:return 3;break;
case 3:return 4;break;
case 4:return 5;break;
case 5:return 6;break;
case 6:return 0;break;
}
}
private:
int year;
int month;
int day;
int isLeapYear(int year)
{
if(((year%4)==0&&(year%100)!=0)||(year%400)==0)
return 1;
else
return 0;
}
};

 

标签:case,第一个,int,month,问题,year,第十七,day,cout
From: https://www.cnblogs.com/czfznb/p/17386339.html

相关文章

  • 粘包/拆包问题一直都存在,只是到TCP就拆不动了。
    OSIopen-system-InterconnectionTCP/IP5层协议栈应用层和操作系统的边界是系统调用,对应到网络编程是socketapiTCP/UDP概况TCP粘包问题TCP/IP报头深思OSI开放系统互联定义了网络框架,以层为单位实现协议,同时控制权逐层传递。OSI实际并没有落地,TCP/IP5层协议栈......
  • 使用vue的keep-alive缓存组件,三级菜单组件无法缓存问题解决
    使用vue做后台管理系统,需求是所有的菜单打开之后,下次点击的时候的使用缓存,这里很简单的做法就是用来包裹住;但是一级菜单和二级菜单都没有问题,三级菜单就会出现无法缓存的问题,网上找资料说是vue中keep-alive本身存在的缺陷,需要在路由守卫中将matched属性做一下优化,具体如下//......
  • 【问题解决】Kafka报错 Bootstrap broker x.x.x.x:9092 (id: -1 rack: null) disconne
    问题复现近日针对某一客户需求开发了一个需要使用Kafka的功能,功能是什么暂且不论,在本地虚机的Kafka连接一切正常遂放到测试服务器上验证功能,以下是监听topic成功和警告报错:2023-05-0910:22:23[localhost-startStop-1]INFOorg.apache.kafka.clients.consumer.ConsumerConfig......
  • EasyPlayer播放H.265视频,播放器快照时间显示的问题优化
    我们的EasyPlayer流媒体播放器可支持H.264与H.265,能支持RTSP、RTMP、HLS、FLV、WebRTC等格式的视频流播放。TSINGSEE的视频平台均集成了EasyPlayer的播放器,实现了无插件网页直播,性能稳定。有用户反馈,EasyPlayer在播放H.265视频的通道时,播放器快照时间显示的是时间戳,并不是日期。......
  • eclipse Maven Dependencies显示不出来的问题
    在eclipse新建了java工程,添加maven的pom.xml,但是MavenDependencies以及依赖的jar包并没有预期地显示出来。  即便我将工程配置成mavenproject,updateprojec...t都不行。 解决办法:找到工程下的.classpath文件,添加如下,重启eclipse就出来了<classpathentry......
  • node使用fs.rename重命名文件的时候,如果有该名称的文件会被替换的问题
    再项目中使用fs.rename给快捷方式重命名的时候,因为有两个版本,在开发人员电脑上都安装两个版本的时候,发现每次都只有快捷方式,然后在重新查看代码的时候。发现代码中使用到了fs.rename去重命名文件。于是做了以下处理letnewLnkPath=path.join(桌面地址,`{appName}.lnk`)if(exi......
  • ant-select数据会发生卡顿问题解决
    <a-selectv-model="form.region"show-searchplaceholder="请选择"option-filter-prop="children"@search="handleSearch"@popupScroll="handlePopu......
  • RDS的PG数据库删除数据后表空间不释放问题处理
    清理表空间(pg_repack)RDSPostgreSQL支持通过插件pg_repack在线清理表空间,有效解决因对全表大量更新等操作引起的表膨胀问题。pg_repack无需获取排它锁,相比CLUSTER或VACUUMFULL更加轻量化。前提条件RDS实例需要满足以下条件:实例大版本内核小版本PostgreSQL10、11、1......
  • 《nodejs跨栏》问题篇
    报错python.EXE参考链接:https://blog.csdn.net/qq_43753724/article/details/122241983报错内容如下:gypERRstackError:Commandfailed:D:\python\python.EXE-cimportsys;print解决方法:设置低版本python//设置confignpmconfigsetpythonD:\Python27......
  • xshell7 免费版 关闭 弹窗问题解决
    原博客地址:https://www.hao.kim/1175.html使用二进制编辑器winhex进行编辑绿色版下载地址:https://mikemhm.lanzoul.com/i6boy0v2a6pa使用winhex打开xshell.exe文件xshell.exe默认目录"C:\ProgramFiles(x86)\NetSarang\Xshell7\Xshell.exe"查找16进制数值74116A006A0......