#include

// 判断是否为闰年 bool isLeapYear(int year) { if (year % 400 == 0) return true; if (year % 100 == 0) return false; if (year % 4 == 0) return true; return false; }

// 获取某个月份的天数 int getDaysInMonth(int year, int month) { switch (month) { case 2: return isLeapYear(year) ? 29 : 28; case 4: case 6: case 9: case 11: return 30; default: return 31; } }

// 计算该月还剩下多少天 int getRemainingDays(int year, int month, int day) { int totalDays = getDaysInMonth(year, month); return totalDays - day; }

int main() { int year, month, day;

std::cout << "请输入年份:";
std::cin >> year;

std::cout << "请输入月份:";
std::cin >> month;

std::cout << "请输入日期:";
std::cin >> day;

int remainingDays = getRemainingDays(year, month, day);

std::cout << "该月还剩下 ' << remainingDays << ' 天" << std::endl;

return 0;

}

您可以将上述代码保存为一个.cpp文件,然后使用C++编译器编译并运行该程序。程序将要求您输入年、月、日,并输出该月还剩下多少天的结果。

C++ 代码:计算给定年月日该月剩余天数

原文地址: https://www.cveoy.top/t/topic/bXNf 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录