万年历 打印今天的日历 c语言
时间: 2024-02-22 15:00:26 浏览: 19
以下是一个简单的 C 语言程序,可以打印出今天的日历:
```c
#include<stdio.h>
#include<conio.h>
#include<time.h>
int main()
{
int i, j, k, day, month, year, total_days, space;
int days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char *month_names[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
time_t t = time(NULL);
struct tm tm = *localtime(&t);
day = tm.tm_mday;
month = tm.tm_mon;
year = tm.tm_year + 1900;
total_days = 0;
for (i = 1900; i < year; i++)
{
if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)
total_days += 366;
else
total_days += 365;
}
for (i = 0; i < month; i++)
{
total_days += days_in_month[i];
}
if (month > 1 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
total_days += 1;
total_days += day;
space = total_days % 7;
printf("\n\n\t\t\t%s %d\n\n", month_names[month], year);
printf(" Sun Mon Tue Wed Thu Fri Sat\n");
for (i = 0; i < space; i++)
printf(" ");
for (i = 1; i <= days_in_month[month]; i++)
{
printf(" %3d ", i);
if ((i + space) % 7 == 0)
printf("\n");
}
getch();
return 0;
}
```
这个程序会获取当前日期,并计算出今天是本年度的第几天。然后,它会计算出本月的第一天是星期几,并打印出一个对应的日历。