2021-11-15 C++下一个生日天数
缘由c++今日日期到生日天数-编程语言-CSDN问答
int isLeap(int year)//判断闰年
{//缘由https://ask.csdn.net/questions/7567048?spm=1005.2025.3001.5141return ((!(year % 4) && year % 100) || !(year % 400));
}
int daysInMonth(int year, int month)//返回月天数
{if (month == 2){ if (isLeap(year)) return 29; else return 28; }else if ((month <= 7 && month % 2) || (month > 7 && !(month % 2)))return 31; else return 30;
}
int isLeglal(int date[])//判断合法
{return (daysInMonth(date[0], date[1]) >= date[2]);
}
void dateInput(int date[])//输入合法日期
{do{cout << (date[0] ? "重新" : "") << "输入:年 月 日:" << endl;cin >> date[0] >> date[1] >> date[2];} while (isLeglal(date) == 0);
}
void 下一个生日天数()
{int datea[3]{0}, dates[3]{0}, ta = 0, y = 1, n = 0;dateInput(datea); dateInput(dates);while (datea[0] < dates[0])dateInput(datea);//2021 11 15 2021 2 1 15+31+31+2=79while(!n)if (datea[0] >= dates[0] && datea[1] >= dates[1] && datea[2] >= dates[2]){ta = daysInMonth(datea[0], datea[1]) - datea[2];y = datea[1] + 1;while (!n || y != dates[1])ta += daysInMonth(datea[0] + n, y), (++y > 12 ? y = 1, ++n : 0);ta += dates[2];}elsedateInput(datea);cout << "还有" << ta << "天" << endl;
}