主题:[讨论]1个c++问题
#include <iostream.h>
int isleap(int year);
int week1(int year,int month,int day,int i);
void outputweek(int week);
int year,month,day,i;
void main()
{
cout<<"请输入年月日:";
cin>>year>>month>>day;
i=isleap(year);
static week=week1(year,month,day,i);
outputweek(week);
} //主函数
int isleap(int year)
{
if((year%4==0&&year%100!=0)||year%400==0)
return 1;
else
return 0;
} //判断是否是闰年
int week1(int year,int month,int day,int i)
{
if((month>=13||day>=32)||(i==0&&month==2&&day>=29))
{
cout<<"输入错误.";
day=0;
} /*假如输入的月大于13或日大于29,或者不是闰年的情况下输入2月29号,输出错误,同时把day值变为0*/
switch(month)
{
case 2:if(day>=30)
{
cout<<"输入错误.";
day=0;
} //假如是2月,日大于30,输出错误,day变为0
else day+=31;
break;
case 3:if(day>=31)
{
cout<<"输入错误.";
day=0;
} //假如是3月,日大于31,输出错误,day变为0
else day+=31+29;
break;
case 4:if(day>=30)
{
cout<<"输入错误.";
day=0;
} //假如是4月,日大于30,输出错误,day变为0
else day+=31+29+31;
break;
case 5:if(day>=31)
{
cout<<"输入错误.";
day=0;
} //假如是5月,日大于31,输出错误,day变为0
else day+=31+29+31+30;
break;
case 6:if(day>=30)
{
cout<<"输入错误.";
day=0;
} //假如是6月,日大于30,输出错误,day变为0
else day+=31+29+31+30+31;
break;
case 7:if(day>=31)
{
cout<<"输入错误.";
day=0;
} //假如是7月,日大于31,输出错误,day变为0
else day+=31+29+31+30+31+30;
break;
case 8:if(day>=31)
{
cout<<"输入错误.";
day=0;
} //假如是8月,日大于31,输出错误,day变为0
else day+=31+29+31+30+31+30+31;
break;
case 9:if(day>=30)
{
cout<<"输入错误.";
day=0;
} //假如是9月,日大于30,输出错误,day变为0
else day+=31+29+31+30+31+30+31+31;
break;
case 10:if(day>=31)
{
cout<<"输入错误.";
day=0;
} //假如是10月,日大于31,输出错误,day变为0
else day+=31+29+31+30+31+30+31+31+30;
break;
case 11:if(day>=30)
{
cout<<"输入错误.";
day=0;
} //假如是11月,日大于30,输出错误,day变为0
else day+=31+29+31+30+31+30+31+31+30+31;
break;
case 12:if(day>=31)
{
cout<<"输入错误.";
day=0;
} //假如是12月,日大于31,输出错误,day变为0
else day+=31+29+31+30+31+30+31+31+30+31+30;
break;
default:break;
}
if(month>=3&&i==0)
day-=1; //不是闰年,大于三月的情况下,比闰年小于1
int week;
if(day!=0) week=(year-1)+int((year-1)*0.1*10/4)-int((year-1)*0.1*10/100)+int((year-1)*0.1*10/400)+day;
else week=0;
return week;
}
void outputweek(int week)
{
cout<<year<<"年"<<month<<"月"<<day<<"日是";
int w;
if(week!=0&&day!=0) {
w=week%7;
switch(w)
{
case 1:cout<<"星期一"<<endl; break;
case 2:cout<<"星期二"<<endl; break;
case 3:cout<<"星期三"<<endl; break;
case 4:cout<<"星期四"<<endl; break;
case 5:cout<<"星期五"<<endl; break;
case 6:cout<<"星期六"<<endl; break;
case 0:cout<<"星期日"<<endl; break;
default:break;
}
}
}
不存在的天数也能算出星期几,哪地方错啦?(给但day值为什么不变化?)
int isleap(int year);
int week1(int year,int month,int day,int i);
void outputweek(int week);
int year,month,day,i;
void main()
{
cout<<"请输入年月日:";
cin>>year>>month>>day;
i=isleap(year);
static week=week1(year,month,day,i);
outputweek(week);
} //主函数
int isleap(int year)
{
if((year%4==0&&year%100!=0)||year%400==0)
return 1;
else
return 0;
} //判断是否是闰年
int week1(int year,int month,int day,int i)
{
if((month>=13||day>=32)||(i==0&&month==2&&day>=29))
{
cout<<"输入错误.";
day=0;
} /*假如输入的月大于13或日大于29,或者不是闰年的情况下输入2月29号,输出错误,同时把day值变为0*/
switch(month)
{
case 2:if(day>=30)
{
cout<<"输入错误.";
day=0;
} //假如是2月,日大于30,输出错误,day变为0
else day+=31;
break;
case 3:if(day>=31)
{
cout<<"输入错误.";
day=0;
} //假如是3月,日大于31,输出错误,day变为0
else day+=31+29;
break;
case 4:if(day>=30)
{
cout<<"输入错误.";
day=0;
} //假如是4月,日大于30,输出错误,day变为0
else day+=31+29+31;
break;
case 5:if(day>=31)
{
cout<<"输入错误.";
day=0;
} //假如是5月,日大于31,输出错误,day变为0
else day+=31+29+31+30;
break;
case 6:if(day>=30)
{
cout<<"输入错误.";
day=0;
} //假如是6月,日大于30,输出错误,day变为0
else day+=31+29+31+30+31;
break;
case 7:if(day>=31)
{
cout<<"输入错误.";
day=0;
} //假如是7月,日大于31,输出错误,day变为0
else day+=31+29+31+30+31+30;
break;
case 8:if(day>=31)
{
cout<<"输入错误.";
day=0;
} //假如是8月,日大于31,输出错误,day变为0
else day+=31+29+31+30+31+30+31;
break;
case 9:if(day>=30)
{
cout<<"输入错误.";
day=0;
} //假如是9月,日大于30,输出错误,day变为0
else day+=31+29+31+30+31+30+31+31;
break;
case 10:if(day>=31)
{
cout<<"输入错误.";
day=0;
} //假如是10月,日大于31,输出错误,day变为0
else day+=31+29+31+30+31+30+31+31+30;
break;
case 11:if(day>=30)
{
cout<<"输入错误.";
day=0;
} //假如是11月,日大于30,输出错误,day变为0
else day+=31+29+31+30+31+30+31+31+30+31;
break;
case 12:if(day>=31)
{
cout<<"输入错误.";
day=0;
} //假如是12月,日大于31,输出错误,day变为0
else day+=31+29+31+30+31+30+31+31+30+31+30;
break;
default:break;
}
if(month>=3&&i==0)
day-=1; //不是闰年,大于三月的情况下,比闰年小于1
int week;
if(day!=0) week=(year-1)+int((year-1)*0.1*10/4)-int((year-1)*0.1*10/100)+int((year-1)*0.1*10/400)+day;
else week=0;
return week;
}
void outputweek(int week)
{
cout<<year<<"年"<<month<<"月"<<day<<"日是";
int w;
if(week!=0&&day!=0) {
w=week%7;
switch(w)
{
case 1:cout<<"星期一"<<endl; break;
case 2:cout<<"星期二"<<endl; break;
case 3:cout<<"星期三"<<endl; break;
case 4:cout<<"星期四"<<endl; break;
case 5:cout<<"星期五"<<endl; break;
case 6:cout<<"星期六"<<endl; break;
case 0:cout<<"星期日"<<endl; break;
default:break;
}
}
}
不存在的天数也能算出星期几,哪地方错啦?(给但day值为什么不变化?)