c++计算改日是本年的第几天
任务描述:
定义一个结构体变量(包括年、月、日),编写程序,要求输入年、月、日,程序能计算并输出该日在本年中是第几天。注意闰年问题。
测试输入:
2000 12 20
预期输出:
12/20 is the 355th day in 2000
程序源码:
#include <iostream> using namespace std; struct { int year; int month; int day; }date; int main() { int days; cin>>date.year>>date.month>>date.day; // 请在此添加代码 /********** Begin *********/ days=0; switch(date.month) { case 12: days=days+30; case 11: days=days+31; case 10: days=days+30; case 9: days=days+31; case 8: days=days+31; case 7: days=days+30; case 6: days=days+31; case 5: days=days+30; case 4: days=days+31; case 3: if(date.year%4==0 && date.year%100!=0 ||date.year%400==0) days=days+29; else days=days+28; case 2: days=days+31; case 1: days=days+date.day; default :break; } /********** End **********/ cout<<date.month<<"/"<<date.day<<" is the "<<days<<"th day in "<<date.year<<"."<<endl; return 0; }