期中

 

#include "date.h"
#include "utils.h" 
#include <iostream>
using std::cout;
using std::endl;

// 补足程序,实现Date类中定义的成员函数 
Date::Date(int y,int m,int d):year(y),month(m),day(d)
{
}

Date::Date()
{
    year=1970;
    month=1;
    day=1;
}

void Date::display()
{
    cout<<year<<"-"<<month<<"-"<<day<<endl;
}

int Date::getYear() const
{
    return year;
}

int Date::getMonth() const 
{
    return month;
}

int Date::getDay() const 
{
    return day;
}

int Date::dayOfYear ()
{
    int n=day,a=0;
    if(isLeap(year))
        a=1;
    switch(month-1)
    {
    case 0:n=n;break;
    case 1:n+=31;break;
    case 2:n=n+59+a;break;
    case 3:n+=90;break;
    case 4:n+=120;break;
    case 5:n+=151;break;
    case 6:n+=181;break;
    case 7:n+=212;break;
    case 8:n+=243;break;
    case 9:n+=273;break;
    case 10:n+=304;break;
    case 11:n+=334;break;
    }
    return n;
}
date.cpp
#ifndef DATE_H
#define DATE_H

class Date {
    public:
        Date(); // 默认构造函数,将日期初始化为1970年1月1日 
        Date(int y, int m, int d); // 带有形参的构造函数,用形参y,m,d初始化年、月、日 
        void display(); // 显示日期 
        int getYear() const;  // 返回日期中的年份 
        int getMonth() const; // 返回日期中的月份 
        int getDay() const; // 返回日期中的日字 
        int dayOfYear(); // 返回这是一年中的第多少天

    private:
        int year;
        int month;
        int day;  
};

#endif
date.h

 

// 功能描述: 
// 判断year是否是闰年, 如果是,返回true; 否则,返回false 
 
bool isLeap(int year) {
    if( (year % 4 == 0  &&  year % 100 !=0) || (year % 400 == 0) )
        return true;
    else
        return false;
}
utils.cpp
// 工具包头文件,用于存放函数声明
 
// 函数声明 
bool isLeap(int);
utils.h
include "utils.h"
#include "date.h"

#include <iostream>
using namespace std;
int main() {
    
    Date epochDate;
    epochDate.display();
    cout << "" <<epochDate.getYear()<<"年第"<< epochDate.dayOfYear() << "天.\n\n" ;
    
    Date today(2019,4,30);
    
    today.display();
    cout << "" <<today.getYear()<<"年第"<< today.dayOfYear() << "天.\n\n" ;
    
    Date tomorrow(2019,5,1);
    tomorrow.display();
    cout << "" <<tomorrow.getYear()<<"年第"<< tomorrow.dayOfYear() << "天.\n\n";
    
    system("pause");
    return 0;
}
main.cpp

 以上为编程题1

 

编程题2

string getCurrentTime() {
    
    time_t now = time(0);  // 获取当前系统日历时间
    
    struct tm *local_time = localtime(&now);  // 把系统日历时间转换为当地时间
    
    char date[SIZE];

    strftime(date, SIZE, "%Y-%m-%d %H:%M", local_time);
    
    return (string(date));
} 
Aticle.cpp
#pragma once
using namespace std;
class Article
{
public:
    Article(string t,string c,string p,string l);
    string getlasttime();
    string getCurrentTime();
    void rtitle();//更新文章标题
    void rcontent();//更新文章内容
    void show();
    ~Article(void);
private:
    string title;
    string content;
    string puttime;
    string lasttime;
};
Article.h
#include<iostream>
#include<string>
#include"Article.h"
using namespace std;
int main()
{
    Article a;
    a.show();
mian.cpp

 

#include "Article.h"
#include<ctime>
#include<string>
#include<iostream>
using namespace std;

const int SIZE=20;

string getCurrentTime() 
{
    time_t now = time(0);  // 获取当前系统日历时间
    struct tm *local_time = localtime(&now);  // 把系统日历时间转换为当地时间
    char date[SIZE];
    strftime(date, SIZE, "%Y-%m-%d %H:%M", local_time);
    return (string(date));
} 

Article::Article(string t,string c,string p,string l):title(t),content(c),puttime(p),lasttime(l)
{
    getline(cin,t);
    getline(cin,c);
    p=getCurrentTime();
    l=getCurrentTime();
}

void Article::rtitle()
{
    getline(cin,title);
    lasttime=getCurrentTime();
}

void Article::rcontent ()
{
    getline(cin,content);
    lasttime=getCurrentTime();
}

void Article::show()
{
    cout<<"===============文章==============";
    cout<<"标题:              ";
    cout<<title<<endl;
    cout<<"内容:"<<endl;
    cout<<content<<endl;
    cout<<"发布时间:           ";
    cout<<puttime<<endl;
    cout<<"最后一次更新时间:  ";
    cout<<lasttime<<endl;
}

Article::~Article(void)
{
}
Article实现
#include "Article.h"
#include<ctime>
#include<string>
#include<iostream>
using namespace std;

const int SIZE=20;

string getCurrentTime() 
{
    time_t now = time(0);  // 获取当前系统日历时间
    struct tm *local_time = localtime(&now);  // 把系统日历时间转换为当地时间
    char date[SIZE];
    strftime(date, SIZE, "%Y-%m-%d %H:%M", local_time);
    return (string(date));
} 

Article::Article(string t,string c,string p,string l):title(t),content(c),puttime(p),lasttime(l)
{
    cout<<"输入标题:"<<endl;
    getline(cin,t);cout<<endl;
    cout<<"输入内容:"<<endl;
    getline(cin,c);
    cout<<endl;
    p=getCurrentTime();
    l=getCurrentTime();
}

void Article::rtitle()
{
    getline(cin,title);
    lasttime=getCurrentTime();
}

void Article::rcontent ()
{
    getline(cin,content);
    lasttime=getCurrentTime();
}

void Article::show()
{
    cout<<"===============文章==============";
    cout<<"标题:              ";
    cout<<title<<endl;
    cout<<"内容:"<<endl;
    cout<<content<<endl;
    cout<<"发布时间:           ";
    cout<<puttime<<endl;
    cout<<"最后一次更新时间:  ";
    cout<<lasttime<<endl;
}

Article::~Article(void)
{
}
Article更新

 

转载于:https://www.cnblogs.com/lovecpp/p/10795834.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值