当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Date.h 9.11 KB
一键复制 编辑 原始数据 按行查看 历史
Blacol 提交于 2020-07-04 18:13 . 7/4 test
#pragma once
#include<iostream>
#include<fstream>
using namespace std;
//第一种日期:以年/月/日(2020/6/27)的格式存储日期,可以进行加减法运算,但是减法不能出现负数,可以使用<<和>>实现文件操作。
class Date_1{
int year;
int month;
int day;
public:
Date_1(){ year = 0; month = 0; day = 0; }
Date_1(int y,int m,int d){
year=y;month=m;day=d;
}
//设置日期
void Set(int y, int m, int d) {
year = y; month = m; day = d;
}
friend Date_1 operator+(Date_1& a,Date_1&b){
int ny, nm, nd;
ny = a.year + b.year;
nm = a.month + b.month;
nd = a.day + b.day;
if (ny % 4 == 0 && ny % 100 != 0 || ny % 400 != 0) {
if (nm-1 == 1 || nm-1 == 3 || nm-1 == 5 || nm-1 == 7 || nm-1 == 8 || nm-1 == 10 || nm-1 == 12) {
if (nd > 31) {
nm++;
nd = nd - 31;
}
}
else if (nm-1 == 2) {
if (nd > 29) {
nm++;
nd = nd - 29;
}
}
else {
if (nd > 30) {
nm++;
nd = nd - 30;
}
}
}
else {
if (nm-1 == 1 || nm-1 == 3 || nm-1 == 5 || nm-1 == 7 || nm-1 == 8 || nm-1 == 10 || nm-1 == 12) {
if (nd > 31) {
nm++;
nd = nd - 31;
}
}
else if (nm-1 == 2) {
if (nd > 28) {
nm++;
nd = nd - 28;
}
}
else {
if (nd > 30) {
nm++;
nd = nd - 30;
}
}
}
if (nm > 12) {
ny++;
nm = nm - 12;
}
return Date_1(ny,nm,nd);
}
friend Date_1 operator-(Date_1& a,Date_1&b){
if(a.day<b.day||a.month<b.month||a.day<b.day){
cout<<"运算错误:结果为负,请确认两个操作数的差不是负数!"<<endl;
cout<<"Error:The result is negative.Please make sure the difference between the two operands is not negative!"<<endl;
cout<<"Ошибка операции:результат отрицательный.Убедитесь,что разница между двумя операндами не является отрицательной!"<<endl;
return Date_1(0,0,0);
}else{
return Date_1(a.day-b.day,a.month-b.month,a.day-b.day);
}
}
friend ostream& operator<<(ostream &a,Date_1 &d) {
a << d.year << "/" << d.month << "/" << d.day;
return a;
}
friend istream& operator>>(istream &a,Date_1 &d) {
a >> d.year;
a.get();
a >> d.month;
a.get();
a >> d.day;
return a;
}
friend ofstream& operator<<(ofstream& a, Date_1& d) {
a << d.year << noskipws << " " << d.month << noskipws << " " << d.day;
return a;
}
friend ifstream& operator>>(ifstream& a, Date_1& d) {
a >> d.year;
a.get();
a >> d.month;
a.get();
a >> d.day;
return a;
}
friend fstream& operator<<(fstream& a, Date_1& d) {
a << d.year << noskipws << " " << d.month << noskipws << " " << d.day;
return a;
}
friend fstream& operator>>(fstream& a, Date_1& d) {
a >> d.year;
a.get();
a >> d.month;
a.get();
a >> d.day;
return a;
}
};
//第二种日期:以月/日/年(6/27/2020)的格式存储日期,可以进行加减法运算,但是减法不能出现负数,不可以使用<<和>>实现文件操作。
class Date_2{
int year;
int month;
int day;
public:
Date_2() { year = 0; month = 0; day = 0; }
Date_2(int y,int m,int d){
year=y;month=m;day=d;
}
friend Date_2 operator+(Date_2& a,Date_2&b){
int ny, nm, nd;
ny = a.year + b.year;
nm = a.month + b.month;
nd = a.day + b.day;
if (ny % 4 == 0 && ny % 100 != 0 || ny % 400 != 0) {
if (nm - 1 == 1 || nm - 1 == 3 || nm - 1 == 5 || nm - 1 == 7 || nm - 1 == 8 || nm - 1 == 10 || nm - 1 == 12) {
if (nd > 31) {
nm++;
nd = nd - 31;
}
}
else if (nm - 1 == 2) {
if (nd > 29) {
nm++;
nd = nd - 29;
}
}
else {
if (nd > 30) {
nm++;
nd = nd - 30;
}
}
}
else {
if (nm - 1 == 1 || nm - 1 == 3 || nm - 1 == 5 || nm - 1 == 7 || nm - 1 == 8 || nm - 1 == 10 || nm - 1 == 12) {
if (nd > 31) {
nm++;
nd = nd - 31;
}
}
else if (nm - 1 == 2) {
if (nd > 28) {
nm++;
nd = nd - 28;
}
}
else {
if (nd > 30) {
nm++;
nd = nd - 30;
}
}
}
if (nm > 12) {
ny++;
nm = nm - 12;
}
return Date_2(ny, nm, nd);
}
friend Date_2 operator-(Date_2& a,Date_2&b){
if(a.day<b.day||a.month<b.month||a.day<b.day){
cout<<"运算错误:结果为负,请确认两个操作数的差不是负数!"<<endl;
cout<<"Error:The result is negative.Please make sure the difference between the two operands is not negative!"<<endl;
cout<<"Ошибка операции:результат отрицательный.Убедитесь,что разница между двумя операндами не является отрицательной!"<<endl;
return Date_2(0,0,0);
}else{
return Date_2(a.day-b.day,a.month-b.month,a.day-b.day);
}
}
friend ostream& operator<<(ostream &a,Date_2 &d) {
a << d.month << "/" << d.day << "/" << d.year;
return a;
}
friend istream& operator>>(istream& a, Date_2& d) {
a >> d.month >> d.day >> d.year;
return a;
}
void Set(int y, int m, int d) {
year = y; month = m; day = d;
}
};
//第三种日期:以月(英文) 日 年(Jun. 27 2020)的格式存储日期,可以进行加减法运算,但是减法不能出现负数,不可以使用<<和>>实现文件操作。
class Date_3{
int year;
int month;
int day;
public:
Date_3() { year = 0; month = 0; day = 0; }
Date_3(int y,int m,int d){
year=y;month=m;day=d;
}
friend Date_3 operator+(Date_3& a,Date_3&b){
int ny, nm, nd;
ny = a.year + b.year;
nm = a.month + b.month;
nd = a.day + b.day;
if (ny % 4 == 0 && ny % 100 != 0 || ny % 400 != 0) {
if (nm - 1 == 1 || nm - 1 == 3 || nm - 1 == 5 || nm - 1 == 7 || nm - 1 == 8 || nm - 1 == 10 || nm - 1 == 12) {
if (nd > 31) {
nm++;
nd = nd - 31;
}
}
else if (nm - 1 == 2) {
if (nd > 29) {
nm++;
nd = nd - 29;
}
}
else {
if (nd > 30) {
nm++;
nd = nd - 30;
}
}
}
else {
if (nm - 1 == 1 || nm - 1 == 3 || nm - 1 == 5 || nm - 1 == 7 || nm - 1 == 8 || nm - 1 == 10 || nm - 1 == 12) {
if (nd > 31) {
nm++;
nd = nd - 31;
}
}
else if (nm - 1 == 2) {
if (nd > 28) {
nm++;
nd = nd - 28;
}
}
else {
if (nd > 30) {
nm++;
nd = nd - 30;
}
}
}
if (nm > 12) {
ny++;
nm = nm - 12;
}
return Date_3(ny, nm, nd);
}
friend Date_3 operator-(Date_3& a,Date_3&b){
if(a.day<b.day||a.month<b.month||a.day<b.day){
cout<<"运算错误:结果为负,请确认两个操作数的差不是负数!"<<endl;
cout<<"Error:The result is negative.Please make sure the difference between the two operands is not negative!"<<endl;
cout<<"Ошибка операции:результат отрицательный.Убедитесь,что разница между двумя операндами не является отрицательной!"<<endl;
return Date_3(0,0,0);
}else{
return Date_3(a.day-b.day,a.month-b.month,a.day-b.day);
}
}
friend ostream& operator<<(ostream &a,Date_3 &d) {
string month[12] = { "Jan.","Feb.","Mar.","Apr.","May","Jun.","Jul.","Aug.","Sep.","Oct.","Nov.","Dec." };
a << month[d.month - 1] << noskipws << " " << d.day << noskipws << " " << d.year;
return a;
}
friend istream& operator>>(istream &a,Date_3 &d) {
a >> d.month >> d.day >> d.year;
return a;
}
void Set(int y, int m, int d) {
year = y; month = m; day = d;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化