将下面的编程翻译为C语言# -*- coding:utf-8 -*- year, month, day = map(int, input().split()) # 判断是否为闰年 def is_leap_year(year): if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0): return True else: return False # 计算该年的天数 def days_of_year(year): if is_leap_year(year): return 366 else: return 365 # 计算该月的天数 def days_of_month(year, month): days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] if is_leap_year(year): days[1] = 29 return days[month - 1] # 计算从公元1年1月1日到该日期前一年的年末总共有多少天 days = 0 for i in range(1, year): days += days_of_year(i) days -= days_of_month(year, month) - day print(days) # 计算到该日期前一个月的月末总共有多少天 days = 0 for i in range(1, month): days += days_of_month(year, i) if month == 2 and is_leap_year(year): days += 1 days += day print(days) # 计算到这一天总共有多少天,并求出这一天是星期几 days = 0 for i in range(1, month): days += days_of_month(year, i) if month == 2 and is_leap_year(year): days += 1 days += day week = (days + 5) % 7 week_dict = {0: 'Sunday', 1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Saturday'} print(days) print(week_dict[week])
时间: 2023-12-28 14:06:02 浏览: 80
13818-3:1998
以下是将 Python 代码翻译成 C 代码的实现:
```c
#include <stdio.h>
// 判断是否为闰年
int is_leap_year(int year) {
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
return 1;
} else {
return 0;
}
}
// 计算该年的天数
int days_of_year(int year) {
if (is_leap_year(year)) {
return 366;
} else {
return 365;
}
}
// 计算该月的天数
int days_of_month(int year, int month) {
int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (is_leap_year(year)) {
days[1] = 29;
}
return days[month - 1];
}
int main() {
int year, month, day;
scanf("%d %d %d", &year, &month, &day);
// 计算从公元1年1月1日到该日期前一年的年末总共有多少天
int days = 0;
for (int i = 1; i < year; i++) {
days += days_of_year(i);
}
days -= days_of_month(year, month) - day;
printf("%d\n", days);
// 计算到该日期前一个月的月末总共有多少天
days = 0;
for (int i = 1; i < month; i++) {
days += days_of_month(year, i);
}
if (month == 2 && is_leap_year(year)) {
days += 1;
}
days += day;
printf("%d\n", days);
// 计算到这一天总共有多少天,并求出这一天是星期几
days = 0;
for (int i = 1; i < month; i++) {
days += days_of_month(year, i);
}
if (month == 2 && is_leap_year(year)) {
days += 1;
}
days += day;
int week = (days + 5) % 7;
char* week_dict[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
printf("%d\n", days);
printf("%s\n", week_dict[week]);
return 0;
}
```
阅读全文