C++计算日期两个经典算法:1.返回年总天数及年天数,2.返回月的天数.
int 返回月的天数(int 年, int 月) {//缘由https://bbs.csdn.net/topics/395074486 return (月 == 2 ? ((((!(年 % 4) && 年 % 100) || !(年 % 400)) ? 1 : 0) ? 29 : 28) : (((月 <= 7 && 月 % 2) || (月>7 && !(月 % 2))) ? 31 : 30)); } int 返回年总天数(int 年) { return ((年) ? 365 * 年 + ((年 / 4) - (年 / 100) + (年 / 400)) : 0); } int a = 24; cout << "该年天数" << (返回年总天数(a) - 返回年总天数(a - 1)) << "\t总天数" << 返回年总天数(a-1) <<"\t周几"<< (返回年总天数(a-1)+1) % 7 << endl; int n = a, y = 1, r = 1, nn = n; while (n) { while (nn)if (--y)r += 返回月的天数(nn, y); else --nn, y = 13; cout << n << "总天数" << r << "年1月1日是星期" << r % 7 << endl;//if (r % 7 == 1) nn = --n, y = 1, r = 1; }
原创文章,作者:xianfajushi的博客,如若转载,请注明出处:https://www.zengqueling.com/c%e8%ae%a1%e7%ae%97%e6%97%a5%e6%9c%9f%e4%b8%a4%e4%b8%aa%e7%bb%8f%e5%85%b8%e7%ae%97%e6%b3%951-%e8%bf%94%e5%9b%9e%e5%b9%b4%e6%80%bb%e5%a4%a9%e6%95%b0%e5%8f%8a%e5%b9%b4%e5%a4%a9%e6%95%b02-%e8%bf%94/