3.定义一个结构体变量(包括年、月、日),编写程序要求输入年月日,程序能计算并输出该日在本年中是第几天。(注意闰年问题)
#include<iostream> //3.定义一个结构体变量(包括年、月、日),编写程序要求输入年月日,程序能计算并输出该日在本年中是第几天。(注意闰年问题)
using namespace std;
struct date {
int m_year, m_mouth, m_day;
};
int main()
{
date dat1;
int dact,num=0,d=0;
cout << "输入年、月、日:" << endl;
cin >> dat1.m_year >> dat1.m_mouth >> dat1.m_day;
for(int i=0;i<dat1.m_mouth;i++)
{
switch (i)
{
case 1:case 3:case 5:case 7:case 8:case 10:case 12:d = 31; break; //通过switch选择分支结构来求得月份
case 4:case 6:case 9:case 11:d = 30; break;
case 2:d = 28; break;
}
num += d;
}
if (dat1.m_year % 400 == 0 || dat1.m_year % 100 != 0 && dat1.m_year % 4 == 0);//判断是否为闰年,是,则加一
{
num++;
}
dact = num+ dat1.m_day;
cout << dat1.m_year << "年" << dat1.m_mouth << "月" << dat1.m_day << "日";
cout << "为该日为本年的第" << dact << "天。" << endl;
return 0;
}