C语言题目5
//用C语言编写程序:今天是2012年4月12日星期四,编写程序, //输入今天开始到12月31日之间的任意日期,输出那一天是星期几。 #include"stdio.h" int getdays(int month, int day) { int months[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; int i = 0; int days = 0; month = month-1; if(month>11) { printf("月份%d过大,请输入合法的日期!", month+1); return -1; } for(i=0; i<12; i++) { if(i!=month) { days = days+months[i]; } else { if(day > months[i]) { printf("日期%d过大,请输入合法的日期!", day); return -1; } days = days+day; break; } } return days; } int main() { int x; int start; int end; int endmonth; int endday; int dayspan; int weekday; int startweekday; int result; char week[7][10]={{'M','o','n','d','a','y','\0'}, {'T','u','e','s','d','a','y','\0'}, {'W','e','d','n','e','s','d','a','y','\0'}, {'T','h','u','r','s','d','a','y','\0'}, {'F','r','i','d','a','y','\0'}, {'S','a','t','u','r','d','a','y','\0'}, {'S','u','n','d','a','y','\0'}}; start = getdays(4,12);//初始日期 startweekday = 4;//初始星期 while(1) { printf("请输入终止月:"); scanf("%d", &endmonth); printf("请输入终止日:"); scanf("%d", &endday); end = getdays(endmonth, endday); if(end < start) { printf("终止日期要大于4月12日!请重新输入...\n"); } else { dayspan = end - start; weekday = dayspan%7; result = (startweekday+weekday-1)%7; puts(week[result]); break; } } return 1; }