各位大哥大姐帮小弟做一个C语言程序,救命啊!
发布网友
发布时间:2022-04-20 03:54
我来回答
共2个回答
热心网友
时间:2023-12-07 16:16
#include<stdio.h>
#include<windows.h>
int month_day[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int CallWeek(int year, int month, int day); //计算某年某月某日是星期几
int CheckYearMonthDay(int year, int month, int day); //检查年-月-日是否有效
void ShowCalendar(int week, int month, int day);
void main()
{
// int week;
int year;
int month;
int day;
for(;;)
{
system("cls");
printf("请输入查询的日期(年-月-日):");
scanf("%d-%d-%d", &year, &month, &day);
if(CheckYearMonthDay(year, month, day))
{
printf("日期输入错误\n");
}
else
{
// week = CallWeek(year, month, day);
// printf("%s\n", WEEK[week]);
ShowCalendar(CallWeek(year, month, day), month, day);
}
system("pause");
}
}
//*************start CheckYearMonthDay()***************//
int CheckYearMonthDay(int year, int month, int day)
{
if(year < 0 || month < 0 || month > 12 || day < 0)
{
return 1;
}
else
{
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
if(day > (month_day[month] + 1))
{
return 1;
}
else
{
month_day[2] = 29;
return 0;
}
}
else
{
if(day > month_day[month])
return 1;
else
return 0;
}
}
}
//***********end CheckYearMonthDay()*****************//
int CallWeek(int year, int month, int day)
{
int week;
int century;
if(month == 1 || month == 2)
{
month += 12; //月份为1或2月当成13或14月
year -= 1; //提前一年
}
century = year / 100 + 1; //那年所处的世纪
year = year / 10 % 10 * 10 + year % 10; //那年的最后两位
week=(year+(int)(year/4)+(int)((century-1)/4)-2*(century-1)+(int)(26*(month+1)/10)+day-1);
week = week % 7;
printf("week == %d\n", week);
if(week >= 7)
{
week -= 7;
}
else
if(week >= -6 && week <= -1)
{
week += 7;
}
return week;
}
void ShowCalendar(int week, int month, int day)
{
char WEEK[7][7] = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
int i,j;
int wb;
int tmp_day;
for(i = day; i != 1; i--)
{
if(week == 0)
week = 6;
else
week--;
}
tmp_day = i;
for(i = 0; i < 7; i++)
{
printf("%s ", WEEK[i]);
}
printf("\n");
wb = week;
for(i = 0; i < 6; i++)
{
for(j = 0; j <= 6; j++)
{
if(wb == 0)
{
if(j == week)
{
if(tmp_day != day)
printf("%6d\t", tmp_day);
else
printf(" ->%2d\t", tmp_day); //特别显示查找的day
tmp_day++;
if(tmp_day > month_day[month])
break;
if(week == 6)
week = 0;
else
week++;
}
}
else
{
printf("\t");
wb--;
}
}
printf("\n");
if(tmp_day > month_day[month])
break;
}
}
//以前做练习写的 送你吧。。不过没有300-500行 不过这个没有农历, 只是查星期几而已 呵呵
热心网友
时间:2023-12-07 16:16
#include <stdio.h>
int main( void )
{
//W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7
int w, d, m, y;
char day[][10]={"一", "二", "三", "四", "五", "六","日"};
while(scanf("%d%d%d", &y, &m, &d)!=EOF){
if(m<=2) y--, m+=12;
w = (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) % 7;
printf("星期%s\n", day[w]);
}
}
注意输入的时候年和月之间要格开