2021-11-16 C++使用switch结构输出月天数
·
缘由2021年某月有几天(c++)-编程语言-CSDN问答 关于#c语言#的问题,请各位专家解答!_编程语言-CSDN问答
int isLeap(int year)//判断闰年
{//缘由https://ask.csdn.net/questions/7567048?spm=1005.2025.3001.5141
return ((!(year % 4) && year % 100) || !(year % 400));
}
void 使用switch结构输出月天数(int n, int y, int& t)
{//缘由https://ask.csdn.net/questions/7568692?spm=1005.2025.3001.5141
switch (y)
{
case 2:t = isLeap(n) ? 29 : 28; break;
case 4: case 6: case 9: case 11:t = 30; break;
case 1: case 3: case 5:case 7: case 8: case 10: case 12:t = 31; break;
default: t = 0; break;
}
}
int n = 2021, y = 1, t = 0;
while (y <= 13)使用switch结构输出月天数(n, y, t),
(t ? cout << y << "月\t" << t << endl
: cout << y << "月份输入错误!\n"),
++y;
void 使用switch结构输出月天数(int n, int y, int& t)
{//缘由https://ask.csdn.net/questions/8007923
if(y && y < 13)if ((y <= 7 && y % 2) || (y > 7 && !(y % 2)))y = 7; else y = 4;
switch (y)
{
case 2:t = (((!(n % 4) && n % 100) || !(n % 400)) ? 29 : 28); break;
case 4: t = 30; break;
case 7: t = 31; break;
default: t = 0; break;
}
}
int t = 0; 使用switch结构输出月天数(2023, 10, t);
if(t)std::cout << t;else std::cout << "ERROR";

我以此施,不求人天无常,愿真正无上道成。
更多推荐
所有评论(0)