计算天数(progress)

题目:

请用循环语句进行程序设计(也可能用到if或switch语句),实现从键盘上输入一个日期,日期格式为:2018-7-9,判断这一天是这一年中的第几天。
注:本题涉及到闰年、平年的判断,已知符合下列条件之一者是润年:
(1)能被4整除,但不能被100整除;
(2)能被400整除。
输入提示信息:”Please Input the Date:\n”
输入格式:”%d-%d-%d”
输出格式:”Result=%d\n”
如果输入月份不在1到12月份之间输出错误提示信息”Input error!\n”
(注:为了便于实现,本题暂不考虑日的合法性)
输入输出用例1:
Please Input the Date:
2010-10-2
Result=275
输入输出用例2:
Please Input the Date:
2000-4-5
Result=96
输入输出用例3:
Please Input the date:
2017-17-2
Input error!

我写的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>

int main()
{
int year, month, day;
int result = 0;
int i = 1;
int uncertian;
printf("Please Input the Date:\n");
scanf_s("%d-%d-%d", &year, &month, &day);
if (month < 1 || month>12) {
printf("Input error!\n");
return 0;
}
else {
do {
if (i < month) {
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) {
result = result + 31;
}
else if (i == 2) {
uncertian = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ? 29 : 28;
result = result + uncertian;
}
else {
result = result + 30;
}
}
i++;
} while (i <= month);
result = result + day;
printf("Result=%d\n", result);
}

return 0;
}

仅仅用了30多行,但是还是感觉很麻烦

代码修改

于是我让豆包进行修改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>

int main() {
int year, month, day;
int day_of_year = 0; // 存储一年中的第几天
int i;

printf("Please Input the Date:\n");
scanf("%d-%d-%d", &year, &month, &day); // 标准编译器使用scanf即可

// 检查月份合法性
if (month < 1 || month > 12) {
printf("Input error!\n");
return 0;
}

// 累加前几个月的天数
for (i = 1; i < month; i++) {
switch (i) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
day_of_year += 31; // 大月
break;
case 4: case 6: case 9: case 11:
day_of_year += 30; // 小月
break;
case 2: // 二月,判断闰年
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
day_of_year += 29;
} else {
day_of_year += 28;
}
break;
}
}

// 加上当月的天数
day_of_year += day;

printf("Result=%d\n", day_of_year);
return 0;
}

使用for循环;使用switch替代多个if和if else
增加了代码的可读性,对比起来我写的已经很不错了

反思

我没有按照题目中的swich要求写代码,是因为不知道case还能并列

  • 在 C 语言的 switch 语句中,多个 case 可以并列使用,这是一种常见的语法特性,用于表示 “多个条件执行相同的操作”

例如代码中的:

1
2
3
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
total_days += 31;
break;

表示当 i 的值是 1、3、5、7、8、10、12 中的任意一个时,都会执行 total_days += 31; 这条语句。
原理是:case 标签本身只是一个 “入口点”,如果没有 break 语句,程序会从匹配的 case 开始往下执行所有后续代码。因此,将多个 case 并列且不写中间的 break,就能实现 “多个值对应同一操作” 的效果,这样写比每个 case 单独写一遍相同的代码更简洁。
这种写法完全符合 C 语言语法规范,且在处理 “多个条件对应同一逻辑” 的场景时非常实用(比如这里的 “大月都有 31 天”)。

总结

  1. 以后遇到月份的问题可以使用switch语法
  2. case可以并列使用,作用可以替代if条件判断句中的condition1 || condition2 || condition3 ||…….


AI写的代码

我直接把题目喂给AI:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>

int main() {
int year, month, day;
int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int total_days = 0;
int i;

printf("Please Input the Date:\n");
scanf("%d-%d-%d", &year, &month, &day);

// 检查月份是否合法
if (month < 1 || month > 12) {
printf("Input error!\n");
return 0;
}

// 判断是否为闰年,调整2月天数
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
days_in_month[1] = 29;
}

// 使用循环计算前几个月的总天数
for (i = 0; i < month - 1; i++) {
total_days += days_in_month[i];
}

// 加上当月的天数
total_days += day;

printf("Result=%d\n", total_days);

return 0;
}

密码的,竟然使用了禁忌的力量—数组!
看来数组是真好用