『代码』··By/蜜汁炒酸奶

日期判断

/*
功能:日期判断
日期:2013-06-09
*/
#include<stdio.h>
#include<stdlib.h>

int isLeapYear(int iYear);
int dayofWeek(int iYear,int iMonth,int iDay);
int weekofYear(int iYear,int iMonth, int iDay);
int main(void)
{
  int iYear,iMonth,iDay;
  printf("请输入一个日期(“年-月-日”形式):");
  scanf("%d-%d-%d",&iYear,&iMonth,&iDay);

  printf("%d年%d月%d日属于%d年的第%d个星期。n",iYear,iMonth,iDay,iYear,weekofYear(iYear,iMonth,iDay));

  system("pause");
  return 0;

}

//参数:int iYear 待判断的一个年份
//返回:int 若iYear是闰年,则返回1,否则返回0;
int isLeapYear(int iYear)
{
     if((iYear%4)&&(iYear%100)||(iYear%400))
	 {
	     return 1;
	 }
	 else
	 {
	    return 0;
	 }

}

//功能:判断一个日期是一周中的星期几
//参数:int iYear 年份
//      int iMonth 月份
//      int iDay 日期
//返回:int 返回当前日期是一周中的星期几
int dayofWeek(int iYear,int iMonth,int iDay)
{
	int i,days=0,weekDay;
	int days1[12]={31,28,31,30,31,30,31,31,30,31,30,31};
	if(0==isLeapYear(iYear))
	{
	    for(i=0;i<iMonth-1;i++)
		{
		    days=+days1[i];
		}
	     days+=iDay;
	}
	else
	{
	   if (iMonth>2)
	   {
	      for(i=0;i<iMonth-1;i++)
		 {
		     days=+days1[i];
		 }
	      days+=iDay+1;
	   }
	   else
	   {
			for(i=0;i<iMonth-1;i++)
			{
				days=+days1[i];
			}
			days+=iDay;
	   }

	}
	weekDay=((iYear-1)+(iYear-1)/4 -( iYear-1 /100 )+( iYear-1 / 400 ) + days) % 7;
	switch (weekDay)
	{     case 0: weekDay = 1;break;
          case 1: weekDay = 2;break;
	      case 2: weekDay = 3;break;
		  case 3: weekDay = 4;break;
		  case 4: weekDay = 5;break;
		  case 5: weekDay = 6;break;
		  case 6: weekDay = 7;break;
	}
}

//功能:判断一个日期是这一年中的第几个星期
//参数:int iYear 年份
//      int iMonth 月份
//      int iDay 日期
//返回:第几个星期
int weekofYear(int iYear,int iMonth, int iDay)
{
	int weekOfYear,days=0,i;
	int days1[12]={31,28,31,30,31,30,31,31,30,31,30,31};
	if(0==isLeapYear(iYear))
	{
	    for(i=0;i<iMonth-1;i++)
		{
		    days=+days1[i];
		}
	     days+=iDay;
	}
	else
	{
	   if (iMonth>2)
	   {
	      for(i=0;i<iMonth-1;i++)
		 {
		     days=+days1[i];
		 }
	      days+=iDay+1;
	   }
	   else
	   {
			for(i=0;i<iMonth-1;i++)
			{
				days=+days1[i];
			}
			days+=iDay;
	   }

	}
	weekOfYear= (int) ((days-dayofWeek(iYear,1,1)+6.3)/7)+1;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

Preview
Loading comments...
0 条评论

No Data

example
Preview