C+实践参考——日期时间类


【项目】日期时间类

定义一个日期类Date,数据成员包括年、月、日,SetDate(int y,int m,int d)和PrintDate()函数分别用于设置日期和显示日期;再定义一个时间类Time,数据成员包括时、分、秒,SetTime(int h,int m,int s)和PrintTime()函数分别用于设置时间和显示时间,在此基础上再定义一个日期时间类TimeDate,充分利用已有的两个类中提供的方法,实现日期和时间的设置和显示。请实现类TimeDate,下面是用于测试的主函数及参考运行结果。
int main()
{
    TimeDate dt_a,dt_b(2010,4,16,9,30,0);
    cout<<"dt_a: ";
    dt_a.PrintDate_Time();
    cout<<endl;
    cout<<"dt_b: ";
    dt_b.PrintDate_Time();
    dt_a.SetTime(20,00,00);
    dt_a.SetDate(2008,8,7);
    cout<<endl;
    cout<<"dt_after uptate: ";
    dt_a.PrintDate_Time();
    return 0;
}

[参考解答]
#include<iostream>
using namespace std;
class Date
{
public:
    void SetDate(int y,int m,int d)
    {
        Year=y;
        Month=m;
        Day=d;
    }
    void PrintDate()
    {
        cout<<Year<<"/"<<Month<<"/"<<Day;
    }
    Date():Year(0),Month(0),Day(0) {}
    Date(int y,int m,int d):Year(y),Month(m),Day(d) {}

private:
    int Year,Month,Day;
};
class Time
{
public:
    void SetTime(int h,int m,int s)
    {
        Houre=h;
        Minutes=m;
        Seconds=s;
    }
    void PrintTime()
    {
        cout<<Houre<<":"<<Minutes<<":"<<Seconds;
    }
    Time():Houre(0),Minutes(0),Seconds(0) {}
    Time(int h,int m,int s):Houre(h),Minutes(m),Seconds(s) {}

private:
    int Houre,Minutes,Seconds;
};
class TimeDate: public Date,public Time
{
public:
    TimeDate():Date(),Time() {};
    TimeDate(int y,int mo,int d,int h,int mi,int s):
        Date(y,mo,d),Time(h,mi,s) {}
    void PrintDate_Time()
    {
        PrintDate();
        cout<<" ";
        PrintTime();
    }
};
int main()
{
    TimeDate dt_a,dt_b(2010,4,16,9,30,0);
    cout<<"dt_a: ";
    dt_a.PrintDate_Time();
    cout<<endl;
    cout<<"dt_b: ";
    dt_b.PrintDate_Time();
    dt_a.SetTime(20,00,00);
    dt_a.SetDate(2008,8,7);
    cout<<endl;
    cout<<"dt_after uptate: ";
    dt_a.PrintDate_Time();
    return 0;
}



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include <iostream> using namespace std; #include "MyDate.h" #include <iomanip> bool MYDATE::dowFlag = false; void MYDATE::Input() { int year, month, day; char c1, c2; while(!(cin >> year >> c1 >> month >> c2 >> day) || ! IsValid(year, month, day) || c1 != '-' || c2 != '-') { cout << "不正确的日期! 请重新输入: "; if(! cin.good()) cin.clear(); while(cin.get() != '\n') ; } this->year = year; this->month = month; this->day = day; } void MYDATE::Output() { char *adow[] = {"日", "一", "二", "三", "四", "五", "六"}; int w = Dow(); cout << setfill('0') << setw(2) << year << '-' << setw(2) << month << '-' << setw(2) << day << setfill(' '); if(dowFlag) { cout << "(星期" << adow[w] << ")"; } } void MYDATE::Set(int year, int month, int day) { if(IsValid(year, month, day)) { this->year = year; this->month = month; this->day = day; } else { cout << "不正确的时间,设置失败!\n"; } } void MYDATE::Get(int &year, int &month, int &day) { year = this->year; month = this->month; day = this->day; } int MYDATE::Year() { return year; } void MYDATE::Year(int year) { if(year > 0) { this->year = year; int n = Dom(year, month); if(day > n) day = n; } } int MYDATE::Month() { return month; } void MYDATE::Month(int month) { if(month > 0 && month <= 12) { this->month = month; int n = Dom(year, month); if(day > n) day = n; } } int MYDATE::Day() { return day; } void MYDATE::Day(int day) { int n = Dom(year, month); if(day <= 0) day = 1; else if(day > n) day = n; this->day = day; } MYDATE MYDATE::Add(int x) { int y = 1,i = 1,m = 1; MYDATE c; double s = this->Tod() + x; if(s >= 0) { y = 1; while(s >= 365 + IsLeap(y)) { s = s - (365 + IsLeap(y++)); i ++; } c.year = i; while(s >= Dom(c.year,m)) { s = s - Dom(c.year,m); m ++; } c.month = m; if(s == 0) c.day = 1; else c.day = s; } else cout << "Error!\n"; return c; } MYDATE MYDATE::Sub(int x) { MYDATE c; c.Set(year, month, day); c = c.Add(-x); return c; } bool MYDATE::IsValid(int year, int month, int day) { return year > 0 && month > 0 && month <= 12 && day > 0 && day <= Dom(year, month); } int MYDATE::Dom(int year, int month) { int n = 0; const static char adom[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if(month > 0 && month <= 12) { n = adom[month - 1]; if(month == 2 && IsLeap(year)) n ++; } return n; } int MYDATE::Doy() { const static short adoy[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; int n = adoy[month - 1] + day; if(month > 2 && IsLeap(year)) n ++; return n; } bool MYDATE::IsLeap(int year) { return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; } int MYDATE::Nol(int year) { return year / 4 - year / 100 + year / 400; } int MYDATE::Tod() { int t = year - 1; return t * 365 + Nol(t) + Doy(); } int MYDATE::Dow() { return Tod() % 7; } int MYDATE::Sub(MYDATE &x) { return this->Tod() - x.Tod(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值