我正在学习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");
你们能告诉我,我在这里错过了什么吗?
ADDED 让我指出女巫部分让我最困惑
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)
答案 0 :(得分:1)
这段代码:
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");
答案 1 :(得分:1)
首先,你可以做一些合并:
if (a1 / 60 == 0)
printf("am"); //same output am
else if (a1 / 60 < 12)
printf("am"); //same output am
else if (a1 / 60 == 12)
printf("pm"); //same output pm
else
printf("pm"); //same output pm
分为:
int hour = a1/60;
if (hour < 12)
printf("am");
else
printf("pm");
正如Edsger W. Dijkstra所说:
两个或更多,使用for。
(或者在这种情况下,是一种功能)。
您最好先定义一个函数:
int print_time (int time) {
int hour = time/60;
printf("%.2d:%.2d", hour, time % 60);
if (hour < 12)
printf("am");
else
printf("pm");
return 0;
}
然后最后的主要代码是:
if (time_val < a) {
print_time(a);
printf(", arriving at ");
print_time(a);
}
else if (time_val < b) {
print_time(b);
printf(", arriving at ");
print_time(b1);
}
组合函数和最后一段代码会得到结果。
答案 2 :(得分:0)
这段代码给我带来了很多麻烦,其中似乎并不正确。如果你 运行此代码并尝试输入时间,例如08:25它将显示 你关闭的出发时间是08:00,但那个时间已经到了 通过。 ...你能告诉我,我在这里错过了什么吗?
是的,您错过了所请求的出发时间最近的航班,无论是过去还是将来,也可以从示例中看到:
输入24小时时间: 13:15
最近的缩放时间是下午12:47。, 到达下午3点。
t = time - a; u = b - time; if (t < u)
这个计算完全按照要求进行计算 - 计算早期和晚期出发的绝对时间差,并决定哪一个更接近。
答案 3 :(得分:-1)
您可以查看使用
printf("arriving at ....%d...%s", ...., (b/16<12) ? "am":"pm")
<cond>?x:y
构造可以使这种“选择一件事”代码更清晰