用C++编写万年历系统
时间: 2023-05-19 07:04:12 浏览: 113
c++课程设计
非常感谢您的提问!以下是一个简单的 C 代码,可以实现万年历系统:
```
#include <stdio.h>
int main()
{
int year, month, day, days, i, j;
int month_days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char week[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
printf("Please enter the year: ");
scanf("%d", &year);
printf("Please enter the month: ");
scanf("%d", &month);
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
month_days[1] = 29;
}
days = 0;
for (i = 0; i < month - 1; i++)
{
days += month_days[i];
}
days += 1;
day = 1;
printf("\n %d年%d月\n", year, month);
printf("--------------------\n");
for (i = 0; i < 7; i++)
{
printf("%s ", week[i]);
}
printf("\n");
for (i = 0; i < days % 7; i++)
{
printf(" ");
}
for (j = 1; j <= month_days[month - 1]; j++)
{
printf("%2d ", j);
if ((days + j) % 7 == 0)
{
printf("\n");
}
}
printf("\n");
return 0;
}
```
这个程序可以根据用户输入的年份和月份,输出该月的日历。程序中使用了一个月份天数的数组和一个星期几的数组,以及一些简单的循环和判断语句。
阅读全文