年月日时分秒(基本框架)

date.h头文件

#ifndef DATE_H
#define DATE_H
class Date{
 int year,month,day;
 public:
  Date(int Year,int Month,int Day);
  Date();
  int isLeap();
  int dayout();
  int getyear();
  int getmonth();
  int getday();
  void showtime(); 
}; 
#endif

date.cpp

#include <iostream>
using namespace std;
#include "date.h"
Date::Date():year(2000),month(1),day(1){
}
Date::Date(int Year,int Month,int Day):year(Year),month(Month),day(Day){
}
int Date::getday(){
 return day;
}
int Date::getmonth(){
 return month;
}
int Date::getyear()
{
 return year;
}
int Date::isLeap()
{
 if(year%400==0||year%4==0&&year%100!=0)
 return 1;
 else
 return 0;
}
int Date::dayout()
{
 int a=0,b=0;
 int p[]={31,28,31,30,31,30,31,31,30,31,30,31};  //先假设为平年的二月,如果不是则在加 isLeap()时加1 
 for(int i=0;i<month-1;i++)
  a+=p[i];
 if(month>2) //当month>2时才有加 isLeap()的必要 
 b=isLeap()+a+day;
 else
 b=a+day;
 return b; 
}
void Date::showtime(){
 cout<<"今天是"<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
 cout<<"今年是"<<(isLeap()==1?"闰年":"平年")<<",今天是今年的第"<<dayout()<<"天"<<endl;
}

day.h头文件

#ifndef DAY_H
#define DAY_H
#include "date.h"
class Day:public Date{
 int hour,min,s;
 public:
  Day(int H,int M,int S);
  Day(int Year,int Month,int Day,int H,int M,int S);
  Day();
  int gethour();
  int getmin();
  int gets();
  void show12time(); 
  void show24time();
}; 
#endif

day.cpp

#include <iostream>
#include <iomanip>
using namespace std;
#include "day.h"
Day::Day():Date(),hour(0),min(0),s(0){}
Day::Day(int H,int M,int S):Date(),hour(H),min(M),s(S){}
Day::Day(int Year,int Month,int Day,int H,int M,int S):Date(Year,Month,Day),hour(H),min(M),s(S){}
int Day::gethour(){
 return hour;
}
int Day::getmin(){
 return min;
}
int Day::gets(){
 return s;
}
void Day::show12time()
{
 int flag=0;
 if(hour>12)
 {flag=1;
 hour-=12;
 }
 cout<<"12小时制时间:"; 
 cout<<setfill('0')<<setw(2)<<hour<<":";
 cout<<setfill('0')<<setw(2)<<min<<":";
 cout<<setfill('0')<<setw(2)<<s;
 cout<<(flag==0?" AM":" PM")<<endl; 
 if(flag==1)
 hour+=12;
}
void Day::show24time()
{
 cout<<"24小时制时间:";
 cout<<setfill('0')<<setw(2)<<hour<<":";
 cout<<setfill('0')<<setw(2)<<min<<":";
 cout<<setfill('0')<<setw(2)<<s<<endl;
}

主函数

#include <iostream>
#include "date.h"
#include "day.h"
using namespace std;
int main(int argc, char** argv) {
 int Year,Month,D,Hour,Min,S;
 cin>>Year>>Month>>D>>Hour>>Min>>S;
 Day d(Year,Month,D,Hour,Min,S);
 d.showtime();
 d.show12time();
 d.show24time();
 return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值