期中

1.日期类

#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
#include "date.h"
#include "utils.h" 
#include <iostream>
using std::cout;
using std::endl;
int Month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
Date::Date():year(1970),month(1),day(1){} // 默认构造函数,将日期初始化为1970年1月1日 
Date::Date(int y, int m, int d):year(y),month(m),day(d){} // 带有形参的构造函数,用形参y,m,d初始化年、月、日 
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 Day=0;
    int a;
    for(a=0;a<month;a++){
        Day+=Month[a];
    }
    Day-=(Month[month-1]-day);
    if(isLeap(year))
        Day+=1;
    return Day;

}// 返回这是一年中的第多少天

// 补足程序,实现Date类中定义的成员函数 
date.app
// 工具包头文件,用于存放函数声明
 
// 函数声明 
bool isLeap(int);
utils.h
// 功能描述: 
// 判断year是否是闰年, 如果是,返回true; 否则,返回false 
 
bool isLeap(int year) {
    if( (year % 4 == 0  &&  year % 100 !=0) || (year % 400 == 0) )
        return true;
    else
        return false;
}
utils.app
#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.app

 

 

2.博客文件管理

 

#include"utils.h"
#include"article.h"
#include<iostream>
#include<string>
#include<string>
#include<cstdlib>
using namespace std;
int main(){
	Article a;
	a.print();
	a.changetitle();
	a.changecontent();
	a.print();
	return 0;
}

  

#ifndef ARTICLE_H
#define ARTICLE_H
#include<string>
#include<cstring>
using namespace std;
class Article{
	//文章标题(title), 文章内容(content),发布时间(publicTime), 最后一次更新时间lastUpdateTime
public:
	Article();
	void changetitle();
	void changecontent();
	void print();
private:
	string title;
	string content;
	string publictime;
	string lastupdatetime;
}
#endif

  

#include"utils.h"
#include"article.h"
#include<iostream>
#include<string>
#include<string>
using namespace std;
Article::Article(){
		string t,c,p,l;
	cout<<"输入标题"<<endl;
	getline(cin,t);
	title=t;
	cout<<"输入内容"<<endl;
	getline(cin,c);
	content=c;
	 publictime=lastupdatetime=getCurrentTime();
};
void Article::changetitle(){
	cout<<"输入新标题"<<endl;
	string t;
	getline(cin,t);
	title=t;
	lastupdatetime=getCurrentTime();
}
void Article::changecontent(){
		cout<<"输入新内容"<<endl;
	string c;
	getline(cin,c);
	content=c;
	lastupdatetime=getCurrentTime();
}
void Article::print(){
	cout<<"=============文章信息==============="<<endl;
	cout<<"标题"<<endl;
	cout<<title<<endl;
	cout<<"内容"<<endl;
	cout<<content<<endl;
	cout<<"发布时间"<<endl;
	cout<<publictime<<endl;
	cout<<"最后一次更新时间"<<endl;
	cout<<lastupdatetime<<endl;
}

  

3.预约信息显示

posted @ 2019-04-30 15:07  无名的路人  阅读(139)  评论(0编辑  收藏  举报