[英]I am writing a program which ask user to enter time in 24 hour format and diplay closest departute time of schedule flight
minutes,using the 24-hour clock) the pro gramme the displays the departure and arrival time for flight whose departure time is closet to that enter by user? 分钟,使用24小时制),程序将显示其出发时间与用户输入的时间相近的航班的出发和到达时间? departure time arrival time 8:00 am 10:16 am
出发时间到达时间8:00 am 10:16 am
9:43 am 11:52 am 11:19 am 1:31 pm 12:47 pm 3:00 pm 2:00 pm 4:08 pm 3:45 pm 5:55 pm 7:00 pm 9:20 pm 9:45 pm 11:58 pm 上午9:43上午11:52上午11:19下午1:31 12:47下午3:00 pm 2:00 pm 4:08 pm 3:45 pm 5:55 pm 7:00 pm 9:20 pm 9:下午45时11:58下午
#include<stdio.h>
int main (void){
int dept1,dept2,dept3,dept4,dept5,dept6,dept7,dept8,hh,mm,entertime;
printf("Enter a time in 24-hour format:");
scanf("%d:%d",&hh,&mm);
dept1=8*60;
dept2=9*60+43;
dept3=11*60+19;
dept4=12*60+47;
dept5=14*60;
dept6=15*60+45;
dept7=19*60;
dept8=21*60+45;
entertime=hh*60+mm;
if(entertime<=dept1){
printf("Closet Departure time is 8:00 A.M,arriving at 10:16 A.M");
}else if(entertime<=dept2){
printf("Closet Departure time is 9:43 A.M,arriving at 11:52 A.M");
}else if(entertime<=dept3){
printf("Closet Departure time is 11:19 A.M,arriving at 1:31 P.M");
}else if(entertime<=dept4){
printf("Closet Departure time is 12:47 P.M,arriving at 3:00 P.M");
}else if(entertime<=dept5){
printf("Closet Departure time is 02:00 P.M,arriving at 4:08 P.M");
}else if(entertime<=dept6){
printf("Closet Departure time is 03:45 P.M,arriving at 5:55 P.M");
}else if(entertime<=dept7){
printf("Closet Departure time is 07:00 P.M,arriving at 9:20 P.M");
}else if(entertime<=dept8){
printf("Closet Departure time is 09:45 P.M,arriving at 11:58 P.M");
}
return 0;
}
i have expressed hour and minute in to for eg 13:15=13*60+15=795 minute so it would be closer to 12:47 pm which is 12*60+47=767 minutes 我已将小时和分钟表示为例如13:15 = 13 * 60 + 15 = 795分钟,因此它将接近12:47 pm,即12 * 60 + 47 = 767分钟
but not getting any output 但没有得到任何输出
problem is that if i enter 13:15(13*60+15=795) then the program should display 12:47 pm departure time
问题是,如果我输入13:15(13 * 60 + 15 = 795),则程序应显示12:47 pm出发时间
Since you want the departure time closest to the entered time, you must not compare entertime
to the departure times, but to the times halfway between to successive departure times, eg instead of 既然你想最接近输入的时间出发的时间,你不能比较
entertime
到发车时间,但到了次中途之间连续的发车时间,如代替
if(entertime<=dept1)
write 写
if (entertime <= (dept1+dept2)/2)
and at last remove the 最后删除
if(entertime<=dept8)
to enable the user to get the latest flight when none of the earlier ones fits. 以使用户能够在较早的航班都不适合时获得最新的航班。
Using this way is much easier and you can also use a 2D array to make it even simpler. 使用这种方法要容易得多,您还可以使用2D阵列使其变得更加简单。
#include<stdio.h>
int main (void){
int hh,mm,entertime;
int dept[8] = {8*60,9*60+43,11*60+19,12*60+47,14*60,15*60+45,19*60,21*60+45};
printf("Enter a time in 24-hour format:");
scanf("%d:%d",&hh,&mm);
entertime=hh*60+mm;
if(entertime<=(dept[0]+dept[1])/2 || entertime>=dept[7]&&entertime<2*60){
printf("Closet Departure time is 8:00 A.M,arriving at 10:16 A.M");
}else if(entertime<=(dept[1]+dept[2])/2){
printf("Closet Departure time is 9:43 A.M,arriving at 11:52 A.M");
}else if(entertime<=(dept[2]+dept[3])/2){
printf("Closet Departure time is 11:19 A.M,arriving at 1:31 P.M");
}else if(entertime<=(dept[3]+dept[4])/2){
printf("Closet Departure time is 12:47 P.M,arriving at 3:00 P.M");
}else if(entertime<=(dept[4]+dept[5])/2){
printf("Closet Departure time is 02:00 P.M,arriving at 4:08 P.M");
}else if(entertime<=(dept[5]+dept[6])/2){
printf("Closet Departure time is 03:45 P.M,arriving at 5:55 P.M");
}else if(entertime<=(dept[6]+dept[7])/2){
printf("Closet Departure time is 07:00 P.M,arriving at 9:20 P.M");
}else if(entertime<=dept[7]){
printf("Closet Departure time is 09:45 P.M,arriving at 11:58 P.M");
}else{
printf("Not a valid time input");
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.