我正在尝试制作一个显示飞机到达和离开时间表的程序,然后要求用户在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);
}
但是它打印了错误的时间,我找不到错误。
答案 0 :(得分:0)
我认为问题在于代码中的逻辑:
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)然后,您比较g2
和g1
,但继续将g1
最小(精细)
3)然后,您比较g2
和g3
并将g3
选择为最小! (错误)
问题在于,您永远不会将g3
与g1
进行比较,因此即使实际上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;
}
。那不是你想要的。
您需要的是一个额外的变量,该变量保持当前的最小值。喜欢:
magnify
顺便说一句:我建议您尽快学习数组,因为这将简化您的代码