C++期中考试
2024-08-31 04:43:24
第一题1. 补足日期类实现,使得能够根据日期获取这是一年中第多少天。(12分)
date.h
#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
utils.h
// 工具包头文件,用于存放函数声明 // 函数声明
bool isLeap(int);
date.cpp
#include "date.h"
#include "utils.h"
#include <iostream>
using std::cout;
using std::endl; /*
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; */
// 补足程序,实现Date类中定义的成员函数
Date::Date ():year(),month(),day()
{
}
Date::Date(int y,int m,int d):year(y),month(m),day(d)
{ }
void Date::display()
{
cout<<year<<'-'<<month<<'-'<<day<<endl;
}
int Date::getDay()const
{
return day;
}
int Date::getMonth()const
{
return month;
}
int Date::getYear()const
{
return year;
}
int Date::dayOfYear()
{
int sum=;
int month1[]={,,,,,,,,,,,};
if(isLeap(year))
month1[]=;
for(int i=;i<month-;i++)
sum+=month1[i];
sum+=day;
return sum;
}
main.cpp
#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(,,); today.display();
cout << "是" <<today.getYear()<<"年第"<< today.dayOfYear() << "天.\n\n" ; Date tomorrow(,,);
tomorrow.display();
cout << "是" <<tomorrow.getYear()<<"年第"<< tomorrow.dayOfYear() << "天.\n\n"; system("pause");
return ;
}
utils.cpp
// 功能描述:
// 判断year是否是闰年, 如果是,返回true; 否则,返回false bool isLeap(int year) {
if( (year % == && year % !=) || (year % == ) )
return true;
else
return false;
}
最新文章
- get github
- 对TabControl的简单优化
- OneThink学习笔记02----数据字典(即OneThink项目数据库里的表及其字段)
- Mybatis中模糊查询的各种写法
- Android 学习笔记之Volley(六)实现获取服务器的字符串响应...
- CCNA 6.5
- unite
- 安卓模拟器Android SDK Manager 无法获取SDK列表的解决办法
- 如何新建XCode项目
- java8新特性,使用流遍历集合
- oracle 角色
- Java IO流 思维导图
- oracle02
- UE4游戏开发基础命令
- POJ 1256
- gdb常用命令记录
- linux之cp和scp的使用
- 【转载】Selenium WebDriver的简单操作说明
- Linux 文件管理权限
- Java多线程-----匿名内部类创建线程
热门文章
- ArcGis教程
- Day2-VIM(五):复制
- df 命令-显示目前磁盘剩余的磁盘空间
- HDU 1863 畅通工程(Prim,Kruskal,邻接表模板)
- Regexp:教程
- delphi 在线程中运行控制台命令(console)
- 初识python notes
- spring与struts有什么区别?
- Shell编程进阶 1.2 shell结构及执行
- xcode 编译报错“Cannot create __weak reference in file using manual reference counting”解决办法<;转>;