利用c语言实现万年历
时间: 2024-02-18 22:57:19 浏览: 69
以下是使用C语言实现万年历的代码示例:
```c
#include <stdio.h>
// 判断是否为闰年
int isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return 1;
} else {
return 0;
}
}
// 计算某年某月的天数
int getDaysOfMonth(int year, int month) {
int days;
int run[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int ping[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (isLeapYear(year)) {
days = run[month - 1];
} else {
days = ping[month - 1];
}
return days;
}
// 计算某年某月某日是星期几
int getWeekday(int year, int month, int day) {
int i, sum = 0;
// 计算年份的天数
for (i = 1900; i < year; i++) {
if (isLeapYear(i)) {
sum += 366;
} else {
sum += 365;
}
}
// 加上月份的天数
for (i = 1; i < month; i++) {
sum += getDaysOfMonth(year, i);
}
// 加上当月的天数
sum += day;
// 计算星期几
return sum % 7;
}
int main() {
int year, month, day;
printf("请输入年份:");
scanf("%d", &year);
printf("请输入月份:");
scanf("%d", &month);
printf("请输入日期:");
scanf("%d", &day);
int weekday = getWeekday(year, month, day);
printf("%d年%d月%d日是星期%d\n", year, month, day, weekday);
return 0;
}
```
阅读全文