本帖最后由 一剑飘零 于 2022-5-23 23:31 编辑
太烧脑了,在学结构体。果然是掉头发。麻烦大佬一点点时间,帮小弟注释一下吧。最好是白话文最容易理解的,有时我也能理解通,有时又迷糊了。
[C] 纯文本查看 复制代码 #include <stdio.h>
#include <stdbool.h>
//输入年月日计算出明天是什么日子
struct date {
int month;
int day;
int year;
};
bool isLeap(struct date d);
int number0fDays(struct date d);
int main() {
struct date today, tomorrow;
printf("请输入年月日(年 月 日):");
scanf("%d %d %d", &today.year, &today.month, &today.day);
if (today.day != number0fDays(today)) { //这里有些烧脑
tomorrow.day = today.day + 1;
tomorrow.month = today.month;
tomorrow.year = today.year;
} else if (today.month == 12) { //这个好理解
tomorrow.day = 1;
tomorrow.month = 1;
tomorrow.year = today.year + 1;
} else { //这个也好理解
tomorrow.day = 1;
tomorrow.month = today.month + 1;
tomorrow.year = today.year;
}
// printf("%d",isLeap());
printf("明天的日期是%d年%d月%d日\n", tomorrow.year, tomorrow.month, tomorrow.day);
return 0;
}
int number0fDays(struct date d) {
int days;
const int daysPerMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (d.month == 2 && isLeap(d)) { //这里的d代表的是什么?是哪个变量?是不是就是我输入的today?也就是说number0fDays和isLeap其实都是main中我输入的today的值?
days = 29;
} else {
days = daysPerMonth[d.month - 1];//还有这里迷糊了。
}
return days;
}
bool isLeap(struct date d) { //这里返回值是不是就是true和false?还有这个参数都是形参,应该是main里面调用number0fDays这个函数中它再调用isLeap,我饶晕了
bool leap = false;
if ((d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400 == 0)
leap = true;
return leap;
} |