【CppProject】#001 成立日
2026-10-28 18:47:04
发布于:广东
#include <iostream>
#include <ctime>
using namespace std;
bool isLeap(int y) {
return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
}
int daysInMonth(int m, int y) {
int d[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
if (m == 2 && isLeap(y)) return 29;
return d[m];
}
int toDays(int y, int m, int d) {
int total = 0;
for (int i = 1; i < y; i++)
total += isLeap(i) ? 366 : 365;
for (int i = 1; i < m; i++)
total += daysInMonth(i, y);
total += d;
return total;
}
int main() {
// 成立日:2026年10月28日
int found_y = 2026, found_m = 10, found_d = 28;
// 自动获取今天日期
time_t now = time(0);
tm *lt = localtime(&now);
int today_y = lt->tm_year + 1900;
int today_m = lt->tm_mon + 1;
int today_d = lt->tm_mday;
int diff = toDays(today_y, today_m, today_d) - toDays(found_y, found_m, found_d);
if (diff < 0) diff = -diff;
cout << "CppProject-Ver 已运行 " << diff << " 天" << endl;
return 0;
}
全部评论 1
CppProject首个C++开源项目,可在Windows上编译运行(推荐Microsoft的Code::Blocks编译器)
1周前 来自 广东
0














有帮助,赞一个