c语言实现万年历具体代码
时间: 2023-09-07 21:05:04 浏览: 176
### 回答1:
下面是一个简单的 C 语言代码实现的万年历程序:
```
#include <stdio.h>
#include <stdlib.h>
int main()
{
int year, month, day, sum, leap;
int monthAdd[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
printf("请输入年份:");
scanf("%d", &year);
printf("请输入月份:");
scanf("%d", &month);
printf("请输入日期:");
scanf("%d", &day);
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
{
leap = 1;
}
else
{
leap = 0;
}
sum = monthAdd[month - 1] + day;
if (leap == 1 && month > 2)
{
sum++;
}
printf("这是%d年的第%d天。\n", year, sum);
return 0;
}
```
这份代码可以实现从键盘读入年月日,然后计算出这是该年的第几天。希望这份代码能帮到你。
### 回答2:
这是一个使用C语言实现万年历的具体代码:
```
#include <stdio.h>
int isLeapYear(int year) {
return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
}
void printCalendar(int year, int month) {
int daysInMonth[] = {31, 28 + isLeapYear(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char* monthName[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
int totalDays = 0;
for (int i = 1900; i < year; i++) {
totalDays += 365 + isLeapYear(i);
}
for (int i = 0; i < month - 1; i++) {
totalDays += daysInMonth[i];
}
int startingDay = (totalDays + 1) % 7;
int currentDay = 1;
printf("%s %d\n", monthName[month - 1], year);
printf("Sun Mon Tue Wed Thu Fri Sat\n");
for (int i = 0; i < startingDay; i++) {
printf(" ");
}
for (int i = startingDay; i < 7; i++) {
printf("%3d ", currentDay);
currentDay++;
}
printf("\n");
while (currentDay <= daysInMonth[month - 1]) {
for (int i = 0; i < 7 && currentDay <= daysInMonth[month - 1]; i++) {
printf("%3d ", currentDay);
currentDay++;
}
printf("\n");
}
}
int main() {
int year, month;
printf("请输入年份和月份(以空格分隔):");
scanf("%d %d", &year, &month);
printCalendar(year, month);
return 0;
}
```
以上代码实现了一个简单的万年历功能。通过输入年份和月份,程序会输出对应月份的日历。首先定义了一个判断闰年的函数isLeapYear,然后根据输入的年份和月份,计算出起始的星期几,然后根据月份的天数打印出对应的日历。最后在主函数中接收用户输入的年份和月份,并调用printCalendar函数进行打印。
### 回答3:
C语言实现万年历的具体代码如下:
```
#include<stdio.h>
int main() {
int year, month, day, days, i;
int daysOfEachMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char *weekdays[7] = {"日", "一", "二", "三", "四", "五", "六"};
printf("请输入年份:");
scanf("%d", &year);
printf("请输入月份:");
scanf("%d", &month);
days = 0;
for (i = 1; i < year; i++) {
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
days += 366;
} else {
days += 365;
}
}
for (i = 0; i < month - 1; i++) {
if (i == 1 && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)) {
days += 29;
} else {
days += daysOfEachMonth[i];
}
}
days += 1;
printf("\n%d年%d月的日历如下:\n", year, month);
printf("---------------------------\n");
printf(" 日 一 二 三 四 五 六\n");
printf("---------------------------\n");
for (i = 0; i < days % 7; i++) {
printf(" ");
}
for (day = 1; day <= daysOfEachMonth[month - 1]; day++) {
printf("%3d ", day);
if ((day + days) % 7 == 0) {
printf("\n");
}
}
printf("\n---------------------------\n");
return 0;
}
```
以上代码首先会要求用户输入年份和月份,然后通过循环计算出给定年份的从公元0年元旦到给定月份的总天数`days`。接着按照星期计算,循环输出日历表。首先输出星期几的空格数对齐,然后按照星期的顺序输出日期,当输出的日期是星期六时换行。
注意,在代码中,我们假设一年有365天,闰年有366天,将每个月的天数存储在数组`daysOfEachMonth`中,并将每个星期的名称存储在`weekdays`字符串数组中。
阅读全文