C++判断日期为一年中第几天
我刚学C++,遇到一道题目:输入年月日,输出该日期为这一年的第几天?涉及到闰年。自己写了代码,编译没问题,但输入日期后运行出错(dubge error),烦请高手帮忙,先谢谢了。
代码如下:
//===============================================================================================
//date.cpp
//===============================================================================================
#include <iostream>
using namespace std;
//-----------------------------------------------------------------------------------------------
int main ()
{
int year,month,day,a,b,i;
cout<<"Please input a date as 'yyyy mm dd'"<<"After input every pate please press 'Enter'"<<endl;
cin>>year>>month>>day;
//-------------------------------------------------
{
if((year%4==0)&&(year%100!=0)||(year%400==0))
a=1;
else
a=0;
}
//-------------------------------------------------
if (!((year>1980) && (month<13) && (day<32)))
cout<<"The date don't comform to routines"<<endl;
else
{
//-------------------------------------------
for(i=0;i<month;i++)
{
if (i=2)
b=b+28+a;
else
if (((i%2==1)&&(i<8))||((i%2==0)&&(i>7)))
b=b+31;
else
b=b+30;
}
//--------------------------------------------
cout<<"The day is the "<<b<<"of the year"<<endl;
}
}
//==============================================================================================
代码如下:
//===============================================================================================
//date.cpp
//===============================================================================================
#include <iostream>
using namespace std;
//-----------------------------------------------------------------------------------------------
int main ()
{
int year,month,day,a,b,i;
cout<<"Please input a date as 'yyyy mm dd'"<<"After input every pate please press 'Enter'"<<endl;
cin>>year>>month>>day;
//-------------------------------------------------
{
if((year%4==0)&&(year%100!=0)||(year%400==0))
a=1;
else
a=0;
}
//-------------------------------------------------
if (!((year>1980) && (month<13) && (day<32)))
cout<<"The date don't comform to routines"<<endl;
else
{
//-------------------------------------------
for(i=0;i<month;i++)
{
if (i=2)
b=b+28+a;
else
if (((i%2==1)&&(i<8))||((i%2==0)&&(i>7)))
b=b+31;
else
b=b+30;
}
//--------------------------------------------
cout<<"The day is the "<<b<<"of the year"<<endl;
}
}
//==============================================================================================
...全文


