[YTU]_2428(C语言习题 计算该日在本年中是第几天)
题目描述
定义一个结构体变量(包括年、月、日)。编写一个函数days,由主函数将年、月、日传递给函数days,计算出该日在本年中是第几天并将结果传回主函数输出。
输入
年月日
输出
当年第几天
样例输入
2000 12 31
样例输出
366#include <iostream> using namespace std; struct y_m_d { int year,month,day; }; int days(y_m_d date) { int m; if((date.year%4==0&&date.year%100!=0)||(date.year%400==0)) switch(date.month) { case 1:m=date.day;break; case 2:m=31+date.day;break; case 3:m=31+29+date.day;break; case 4:m=31+29+31+date.day;break; case 5:m=31+29+31+30+date.day;break; case 6:m=31+29+31+30+31+date.day;break; case 7:m=31+29+31+30+31+30+date.day;break; case 8:m=31+29+31+30+31+30+31+date.day;break; case 9:m=31+29+31+30+31+30+31+31+date.day;break; case 10:m=31+29+31+30+31+30+31+31+30+date.day;break; case 11:m=31+29+31+30+31+30+31+31+30+31+date.day;break; case 12:m=31+29+31+30+31+30+31+31+30+31+30+date.day;break; } else switch(date.month) { case 1:m=date.day;break; case 2:m=31+date.day;break; case 3:m=31+28+date.day;break; case 4:m=31+28+31+date.day;break; case 5:m=31+28+31+30+date.day;break; case 6:m=31+28+31+30+31+date.day;break; case 7:m=31+28+31+30+31+30+date.day;break; case 8:m=31+28+31+30+31+30+31+date.day;break; case 9:m=31+28+31+30+31+30+31+31+date.day;break; case 10:m=31+28+31+30+31+30+31+31+30+date.day;break; case 11:m=31+28+31+30+31+30+31+31+30+31+date.day;break; case 12:m=31+28+31+30+31+30+31+31+30+31+30+date.day;break; } return m; } int main() { y_m_d date; int days(y_m_d); int day_sum; cin>>date.year>>date.month>>date.day; day_sum=days(date); cout<<day_sum<<endl; return 0; }