救命的C++编程题

来源:百度知道 编辑:UC知道 时间:2024/06/08 19:26:52
给定某个年月日的值,例如,1998年4月7日,计算出这一天属于该年的第几天,星期几.
要求写出计算闰年的函数和计算日期的函数.

#include <iostream>
#include <math.h>
#include <cstdio>
using namespace std;
int smonth(int);
int firstday(int);
int year,month,day,sum(0);
int main()
{
cin>>year>>month>>day;
for(int i=1;i<month;++i)
sum+=smonth(i);
sum+=day;
int first=firstday(year);
cout<<"属于该年的第"<<sum<<"天"<<"星期"<<(first-1+sum%7)<<endl;
system("pause");
return 0;
}
int smonth(int m)
{
switch(m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:return 31;
case 4:
case 6:
case 9:
case 11:return 30;
case 2:if(((year%4==0&&year%100!=0)||year%400==0))
return 29;
else
return 28;
default:return 0;
}
}
int firstday(int y)
{
double s;
s=floor(year-1+(ye