gpt4 book ai didi

c - 到达/出发计划

转载 作者:行者123 更新时间:2023-11-30 21:27:14 30 4
gpt4 key购买 nike

我正在尝试制作一个程序,显示飞机到达和起飞的时间表,然后要求用户在 C 中输入时间。然后程序将找到最接近输入时间的到达时间用户。问题是它没有按预期工作,并且给我一个错误的到达时间,甚至不接近输入的时间。我对 C 编程非常陌生,所以请原谅我代码中困惑的编程/逻辑。这是:

#include <stdio.h>

int main(void) {

int time0, time1, time2, time3, time4, time5, time6, time7, time8, t, m, i1
= 1, i2 = 3, i3 =4, i4 =5, i5=6, i6=7, i7=8, i8=9, u1=10, u2=20, u3=30,
u4=40, u5=50, u6=59, u7=25, u8=45, y1=13, y2=14, y3=15, y4=16, y5=17, y6=18,
y7=19, y8=20, h1=17, h2=18, h3=19, h4=30, h5=40, h6=40, h7=35, h8=7, g1, g2,
g3, g4, g5, g6, g7, g8, c, v;

/* The timetable, in which the majority of the variables are used */

printf("Arrival\tDeparture\n");
printf("%3.2d:%.2d\t%3.2d:%.2d\n", i1, u1, y1, h1);
printf("%3.2d:%.2d\t%3.2d:%.2d\n", i2, u2, y2, h2);
printf("%3.2d:%.2d\t%3.2d:%.2d\n", i3, u3, y3, h3);
printf("%3.2d:%.2d\t%3.2d:%.2d\n", i4, u4, y4, h4);
printf("%3.2d:%.2d\t%3.2d:%.2d\n", i5, u5, y5, h5);
printf("%3.2d:%.2d\t%3.2d:%.2d\n", i6, u6, y6, h6);
printf("%3.2d:%.2d\t%3.2d:%.2d\n", i7, u7, y7, h7);
printf("%3.2d:%.2d\t%3.2d:%.2d\n", i8, u8, y8, h8);
printf("Enter your time for departure: ");
scanf("%d:%d", &t, &m);

/*Breaks down the entered time and the times from the timetable in minutes
since midnight for comparing, later in the program */

time0= 60*t +m;
time1= 60*i1 +u1;
time2= 60*i2 +u2;
time3= 60*i3 +u3;
time4= 60*i4 +u4;
time5= 60+i5 +u5;
time6= 60+i6 +u6;
time7= 60+i7 +u7;
time8= 60+i8 +u8;

/* subtracts the different arrival times from the entered time if the
arrival times are bigger, else the entered time is subtracted and saves the
difference in a variable for later use */

if(time0 > time1) {
g1 = time0 -time1;
} else {
g1 = time1 -time0;
}
if(time0 > time2) {
g2 = time0-time2;
} else {
g2 = time2 - time0;
}
if(time0 > time3) {
g3 = time0- time3;
} else {
g3 = time3 -time0;
}
if(time0 > time4) {
g4 = time0 - time4;
} else {
g4 = time4 - time0;
}
if(time0 > time5) {
g5=time0 -time5;
} else {
g5= time5 - time0;
}
if(time0 > time6) {
g6 = time0-time6;
} else {
g6 =time6-time0;
}
if(time0 > time7) {
g7 = time0-time7;
} else {
g7 =time7-time0;
}
if(time0 > time8) {
g8 = time0-time8;
} else {
g8 =time8-time0;
}
/* here, the program should compare all the differences, to check which one
is the smallest, and then save the smallest hour(s) and minute(s) in the v
and c variables */

v = i1;
c = u1;

if( g2 < g1 ) {
v = i2;
c = u2;
}
if( g3 < g2) {
v = i3;
c = u3;
}
if( g4 < g3) {
v = i4;
c = u4;
}
if( g5 < g4) {
v = i5;
c = u5;
}
if( g6 < g5) {
v = i6;
c = u6;
}
if( g7 < g6) {
v = i7;
c = u7;
}
if( g8 < g7) {
v = i8;
c = u8;
}
/* The time with the smallest difference to the entered time should be
printed*/
printf("The closest time is %d:%d", v, c);
}

但是它打印了错误的时间,而且我找不到错误。

最佳答案

我认为问题出在这段代码的逻辑上:

v = i1;
c = u2;

if( g2 < g1 ) {
v = i2;
c = u2;
}
if( g3 < g2) {
v = i3;
c = u3;
}
if( g4 < g3) {
v = i4;
c = u4;
}

考虑这个案例g1 < g3 < g2 。这里要选择g1但实际发生的情况是?

1) 首先你假设 g1成为最小的。 (很好)

2) 然后你比较g2g1但继续 g1最小(精细)

3) 然后你比较g2g3并选择g3最小的! (错误)

问题是你从不比较 g3g1因此您选择g3即使它实际上大于 g1 。这不是你想要的。

您需要的是一个额外的变量来保存当前的最小值。喜欢:

v = i1;
c = u2;
smallest = g1;

if( g2 < smallest ) {
v = i2;
c = u2;
smallest = g2;
}
if( g3 < smallest) {
v = i3;
c = u3;
smallest = g3;
}
if( g4 < smallest) {
v = i4;
c = u4;
smallest = g4;
}

顺便说一句:我建议您尽快了解数组,因为这将简化您的代码

关于c - 到达/出发计划,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53121225/

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