当前位置: 首页 > news >正文

网站建设目标的技术可行性一个网站怎么做关键词搜索

网站建设目标的技术可行性,一个网站怎么做关键词搜索,深圳住房与城乡建设部网站,互联网行业数据分析文章目录 日期类的设计日期计算相关OJ题HJ73 计算日期到天数转换KY111 日期差值KY222 打印日期KY258 日期累加 在软件开发中,处理日期是一项常见的任务。为了方便地操作日期,我们可以使用C编程语言来创建一个简单的日期类。在本文中,我们将介…

文章目录

  • 日期类的设计
  • 日期计算相关OJ题
    • HJ73 计算日期到天数转换
    • KY111 日期差值
    • KY222 打印日期
    • KY258 日期累加

在软件开发中,处理日期是一项常见的任务。为了方便地操作日期,我们可以使用C++编程语言来创建一个简单的日期类。在本文中,我们将介绍如何使用C++实现一个基本的日期类,包括日期的加减、大小比较等功能。

日期类的设计

下面是日期类的基本实现代码:

#pragma once
#include<iostream>
using namespace std;class Date {
public:// 获取某年某月的天数int GetMonthDay(const int year, const int month);// 构造函数Date(int year = 1900, int month = 1, int day = 1);// 拷贝构造函数Date(const Date& d);// 析构函数~Date();// 打印日期void print()const;// 赋值运算符重载Date& operator=(const Date& d);// +=运算符重载Date& operator+=(const int day);// +运算符重载Date operator+(const int day);// -=运算符重载Date& operator-=(int day);// -运算符重载Date operator-(int day);// 计算两个日期之间的天数差int operator-(const Date& d) const;// ++前置运算符重载Date& operator++();// ++后置运算符重载Date operator++(int);// --前置运算符重载Date& operator--();// --后置运算符重载Date operator--(int);// 大于运算符重载bool operator>(const Date& d) const;// 等于运算符重载bool operator==(const Date& d) const;// 大于等于运算符重载bool operator >= (const Date& d) const;// 小于运算符重载bool operator < (const Date& d) const;// 小于等于运算符重载bool operator <= (const Date& d) const;// 不等于运算符重载bool operator != (const Date& d) const;// 地址运算符重载const Date* operator & () const;// 输出流运算符重载friend ostream& operator << (ostream& out, const Date& d);// 输入流运算符重载friend istream& operator >> (istream& in, Date& d);private:int _year;  // 年int _month;  // 月int _day;  // 日
};// 获取某年某月的天数
int Date::GetMonthDay(const int year, const int month) {int monthDay[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };if (2 == month && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))) {return 29;}return monthDay[month];
}// 构造函数
Date::Date(int year, int month, int day) {if ((year < 1) || (month < 1) || (month > 12) || (day < 1) || (day > GetMonthDay(year, month))) {cout << "非法日期:" << endl;}_year = year;_month = month;_day = day;
}// 拷贝构造函数
Date::Date(const Date& d) {_year = d._year;_month = d._month;_day = d._day;
}// 析构函数
Date::~Date() {_year = 1900;_month = 1;_day = 1;
}// 打印日期
void Date::print()const {cout << _year << "/" << _month << "/" << _day << endl;
}// 赋值运算符重载
Date& Date::operator=(const Date& d) {if (this != &d) {_day = d._day;_month = d._month;_year = d._year;}return *this;
}// +=运算符重载
Date& Date::operator+=(const int day) {_day += day;while (_day > GetMonthDay(_year, _month)) {_day -= GetMonthDay(_year, _month);++_month;if (_month == 13) {++_year;_month = 1;}}return *this;
}// +运算符重载
Date Date::operator+(const int day) {Date tmp(*this);tmp += day;return tmp;
}// -=运算符重载
Date& Date::operator-=(int day) {_day -= day;while (_day < 0) {--_month;if (_month == 0) {--_year;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}// -运算符重载
Date Date::operator-(int day) {Date tmp(*this);tmp -= day;return tmp;
}// 计算两个日期之间的天数差
int Date::operator-(const Date& d) const {Date BigDate = *this;Date SmallDate = d;if (SmallDate > BigDate) {BigDate = d;SmallDate = *this;}int count = 0;while (SmallDate != BigDate) {++SmallDate;++count;}return count;
}// ++前置运算符重载
Date& Date::operator++() {*this += 1;return *this;
}// ++后置运算符重载
Date Date::operator++(int) {Date tmp(*this);*this += 1;return tmp;
}// --前置运算符重载
Date& Date::operator--() {*this -= 1;return *this;
}// --后置运算符重载
Date Date::operator--(int) {Date tmp(*this);*this -= 1;return tmp;
}// 大于运算符重载
bool Date::operator>(const Date& d) const {if (_year > d._year || (_year == d._year && _month > d._month) || (_year == d._year && _month == d._month && _day > d._day)) {return true;}return false;
}// 等于运算符重载
bool Date::operator==(const Date& d) const {return _year == d._year && _month == d._month && _day == d._day;
}// 大于等于运算符重载
bool Date::operator >= (const Date& d) const {return (*this > d) || (*this == d);
}// 小于运算符重载
bool Date::operator < (const Date& d) const {return !(*this >= d);
}// 小于等于运算符重载
bool Date::operator <= (const Date& d) const {return !(*this > d);
}// 不等于运算符重载
bool Date::operator != (const Date& d) const {return !(*this == d);
}// 地址运算符重载
const Date* Date::operator & () const {return this;
}// 输出流运算符重载
ostream& operator << (ostream& out, const Date& d) {out << d._year << "/" << d._month << "/" << d._day;return out;
}// 输入流运算符重载
istream& operator >> (istream& in, Date& d) {in >> d._year;in >> d._month;in >> d._day;return in;
}

上面的代码实现了日期类的基本功能,包括构造函数、加减运算符重载、比较运算符重载、输入输出流运算符重载等。
下面是主函数用于测试代码功能:

int main() {// 创建日期对象并打印Date d1(2023, 11, 13);cout << "日期1:";d1.print();// 拷贝构造函数测试Date d2(d1);cout << "日期2(拷贝构造):";d2.print();// 赋值运算符重载测试Date d3 = d1;cout << "日期3(赋值运算符重载):";d3.print();// += 运算符重载测试d1 += 10;cout << "日期1(+=运算符重载后):";d1.print();// + 运算符重载测试Date d4 = d2 + 5;cout << "日期4(+运算符重载):";d4.print();// -= 运算符重载测试d2 -= 3;cout << "日期2(-=运算符重载后):";d2.print();// - 运算符重载测试Date d5 = d3 - 7;cout << "日期5(-运算符重载):";d5.print();// - 运算符重载测试int diff = d5 - d4;cout << "日期4和日期5之间的天数差:" << diff << endl;// ++ 前置运算符重载测试++d1;cout << "日期1(++前置运算符重载后):";d1.print();// ++ 后置运算符重载测试Date d6 = d2++;cout << "日期6(++后置运算符重载):";d6.print();cout << "日期2(++后置运算符重载后):";d2.print();// -- 前置运算符重载测试--d3;cout << "日期3(--前置运算符重载后):";d3.print();// -- 后置运算符重载测试Date d7 = d4--;cout << "日期7(--后置运算符重载):";d7.print();cout << "日期4(--后置运算符重载后):";d4.print();// 大于运算符重载测试cout << "日期5大于日期6吗?" << (d5 > d6 ? "是" : "否") << endl;// 等于运算符重载测试cout << "日期1等于日期2吗?" << (d1 == d2 ? "是" : "否") << endl;// 不等于运算符重载测试cout << "日期3不等于日期4吗?" << (d3 != d4 ? "是" : "否") << endl;// 输出流运算符重载测试cout << "日期1的输出流运算符重载:" << d1 << endl;// 输入流运算符重载测试Date d8;cout << "请输入一个日期(年 月 日):";cin >> d8;cout << "您输入的日期为:" << d8 << endl;return 0;
}

在这里插入图片描述
C++实现一个简单的日期类,包括日期的加减、大小比较等功能。日期类的实现可以帮助我们更方便地处理日期,提高代码的可读性和可维护性。

日期计算相关OJ题

HJ73 计算日期到天数转换

https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded?tpId=37&&tqId=21296&rp=1&ru=/activity/oj&qru=/ta/huawei/question-ranking
在这里插入图片描述

#include <iostream>
using namespace std;int main() {int month[13] = {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};int y, m, d;cin >> y >> m >> d;int day = d;if (2 < m && ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))){day += 1;}day += month[m];cout << day << endl;
}

KY111 日期差值

https://www.nowcoder.com/practice/ccb7383c76fc48d2bbc27a2a6319631c?tpId=62&&tqId=29468&rp=1&ru=/activity/oj&qru=/ta/sju-kaoyan/question-ranking
在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;// 判断是否为闰年
bool isLeapYear(int year) 
{return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}// 获取某年某月的天数
int getDaysOfMonth(int year, int month) 
{int daysOfMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};if (month == 2 && isLeapYear(year)) {return 29;}return daysOfMonth[month];
}// 计算日期距离公元0年的天数
int getDaysFromZero(int year, int month, int day) 
{int days = 0;for (int y = 1; y < year; ++y) {days += isLeapYear(y) ? 366 : 365;}for (int m = 1; m < month; ++m) {days += getDaysOfMonth(year, m);}days += day;return days;
}// 计算两个日期之间的天数差值
int getDaysDiff(const string& date1, const string& date2) 
{int year1, month1, day1, year2, month2, day2;sscanf(date1.c_str(), "%4d%2d%2d", &year1, &month1, &day1);sscanf(date2.c_str(), "%4d%2d%2d", &year2, &month2, &day2);int days1 = getDaysFromZero(year1, month1, day1);int days2 = getDaysFromZero(year2, month2, day2);return abs(days2 - days1) + 1;
}int main() 
{string date1, date2;while (cin >> date1 >> date2) {int daysDiff = getDaysDiff(date1, date2);cout << daysDiff << endl;}return 0;
}

KY222 打印日期

https://www.nowcoder.com/practice/b1f7a77416194fd3abd63737cdfcf82b?tpId=69&&tqId=29669&rp=1&ru=/activity/oj&qru=/ta/hust-kaoyan/question-ranking
在这里插入图片描述

#include <iostream>
using namespace std;bool isLeapYear(int year)
{return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}int getMonthDay(int year, int month)
{int daysOfMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};if (month == 2 && isLeapYear(year)) {return 29;}return daysOfMonth[month];
}int main() {int year, day;while (cin >> year >> day) { int month = 1;while (day > getMonthDay(year, month)){day -= getMonthDay(year, month);++month;}printf("%4d-%02d-%02d\n",  year, month, day);}
}

KY258 日期累加

https://www.nowcoder.com/practice/eebb2983b7bf40408a1360efb33f9e5d?tpId=40&&tqId=31013&rp=1&ru=/activity/oj&qru=/ta/kaoyan/question-ranking
在这里插入图片描述

#include <iostream>
using namespace std;bool isLeapYear(int year)
{return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}int getMonthDay(int year, int month)
{int daysOfMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};if (month == 2 && isLeapYear(year)) {return 29;}return daysOfMonth[month];
}
int main() {int t, year, month, day, add;cin >> t;while (t--) { // 注意 while 处理多个 casecin >> year >> month >> day >> add;day += add;while (day > getMonthDay(year, month)){day -= getMonthDay(year, month);++month;if (13 == month){month = 1;++year;}}printf("%4d-%02d-%02d\n",  year, month, day);}
}
http://www.yayakq.cn/news/530251/

相关文章:

  • 小米路由器做网站广告公司名字大全免费
  • 移动网站建站视频教程zimg wordpress
  • 怎么快速做网站排名山西手动网站建设推荐平台
  • 黄金网站app软件下载安装免费网站集约化建设
  • 网站怎么做平台太原论坛建站模板
  • 潮州seo网站推广大连模板网站制作
  • 网站后台管理界面代码快刷网站
  • 新手做网站优化怎么做做驾校题目用什么网站好
  • 时尚大气网站设计学技术的培训机构
  • 怎么制作网站软件下载人才网站怎么建设
  • asp.net做网站的流程263企业邮箱登录登录入口电脑版
  • 庆阳做网站公司08影院 WordPress模板
  • 哪个网站域名更新快教育网站开发
  • 电影网站建设策划书2023全国企业公司大黄页
  • 做外贸如何建立网站网站的主题是什么
  • 学校定制网站建设公司山西建设执业资格注册管理中心网站
  • 公明做网站科技网站建设方案
  • 做网站是百度包年快照扬中网站建设公司
  • gif图标网站电商公司名字大全
  • 网站到期怎么续费网站打开是建设中
  • 太平洋建设 网站中信云 做网站
  • 车身做网站宣传图深圳制作外贸网站
  • 网站如何被收录情况平面设计网上培训机构
  • 做网站维护学什么编程语言尚易企业邮箱登录入口
  • 长沙做网站最好的公司有哪些坪山网站建设价位
  • 网站开发岗位内容和要求建设通好用吗
  • wordpress显示注册用户重庆seo研究中心
  • 校园官方网站建设的书籍wordpress 不显示图片
  • 做网站在线建设网站需要哪些设备
  • 网站建站无锡手机端网站建设的注意事项