gpt4 book ai didi

c - if 语句代码 - 使其更简单 - 需要建议

转载 作者:行者123 更新时间:2023-11-30 15:26:07 26 4
gpt4 key购买 nike

我正在从《学习 C 编程:现代方法》一书中学习 C,我在必须计算关闭出发时间和到达时间的任务中遇到了一些麻烦。所以我在stackoverflow上发现了一个问题。

这个:If statement and expressions question advice (要完全理解我在说什么,请查看原始帖子)

这段代码让我很困扰,其中似乎有些不对劲。如果您运行此代码并尝试输入时间(例如 08:25),它将显示关闭出发时间为 08:00,但该时间已经过去。

   #include <stdio.h>

int main (int argc, const char * argv[]) {


// Flight departure times since midnight
// 8am, 9:45am, 11:19am, 12:47pm
// 2pm, 3:45pm, 7pm, 7:45pm
int a = 480, b = 585, c = 679, d = 767,
e = 840, f = 945, g = 1140, h = 1185;

// Flight arrival times for respective departure times.
int a1 = 616, b1 = 712, c1 = 811, d1 = 900,
e1 = 968, f1 = 1075, g1 = 1280, h1 = 1438;

int hours, minutes, time, t, u;

// Get the users time

printf("Enter a 24 hour time (hh:mm): \n");
scanf("%d:%d", &hours, &minutes);

time = hours * 60 + minutes;

printf("Closest departure time is ");

if (time <= a)
printf("8:00am");
else
if (time > a && time <= b) {
t = time - a;
u = b - time;
if (t < u) {
printf("%.2d:%.2d", a / 60, a % 60);
if (a / 60 == 0)
printf("am");
else if (a / 60 < 12)
printf("am");
else if (a / 60 == 12)
printf("pm");
else
printf("pm");
printf(", arriving at %d:%.2d", a1 / 60, a1 % 60);
if (a1 / 60 == 0)
printf("am");
else if (a1 / 60 < 12)
printf("am");
else if (a1 / 60 == 12)
printf("pm");
else
printf("pm");
}
else {
printf("%.2d:%.2d", b / 60, b % 60);
if (b / 60 == 0)
printf("am");
else if (b / 60 < 12)
printf("am");
else if (b / 60 == 12)
printf("pm");
else
printf("pm");
printf(", arriving at %d:%.2d", b1 / 60, b1 % 60);
if (b1 / 60 == 0)
printf("am");
else if (b1 / 60 < 12)
printf("am");
else if (b1 / 60 == 12)
printf("pm");
else
printf("pm");
}
}

如果代码看起来像这样,不是吗(这段代码短了 50%,并且没有任何不必要的计算):

         if (time_val < a)
{
printf("%.2d:%.2d", a / 60, a % 60);
if (a / 60 == 0)
printf("am");
else if (a / 60 < 12)
printf("am");
else if (a / 60 == 12)
printf("pm");
else
printf("pm");
printf(", arriving at %d:%.2d", a1 / 60, a1 % 60);
if (a1 / 60 == 0)
printf("am");
else if (a1 / 60 < 12)
printf("am");
else if (a1 / 60 == 12)
printf("pm");
else
printf("pm");

}
else if (time_val < b)
{
printf("%.2d:%.2d", b / 60, b % 60);
if (b / 60 == 0)
printf("am");
else if (b / 60 < 12)
printf("am");
else if (b / 60 == 12)
printf("pm");
else
printf("pm");
printf(", arriving at %d:%.2d", a2 / 60, a2 % 60);
if (a2 / 60 == 0)
printf("am");
else if (a2 / 60 < 12)
printf("am");
else if (a2 / 60 == 12)
printf("pm");
else
printf("pm");

你们能告诉我,我在这里遗漏了什么吗?

已添加让我具体说明一下最让我困惑的女巫部分

      if (time <= a)
printf("8:00am");
else
if (time > a && time <= b) {
//why in the hell this calculation and if condition(below) would be necessary?
//It messes up the whole program. try the previous script.
//(Don't worry about the time calculations, I made them way simpler, I just wanted to leave
//the original script)
t = time - a;
u = b - time;
if (t < u)

最佳答案

这段代码:

                if (b / 60 == 0)
printf("am");
else if (b / 60 < 12)
printf("am");
else if (b / 60 == 12)
printf("pm");
else
printf("pm");

相当于:

                if (b / 60 < 12)
printf("am");
else
printf("pm");

关于c - if 语句代码 - 使其更简单 - 需要建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27494713/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com